【问题标题】:Listview add item one at a timeListview 一次添加一项
【发布时间】:2019-02-15 15:45:54
【问题描述】:

我的主要活动中有一个列表视图和一个按钮,以及三个布局资源文件(right.xml、mid.xml 和 left.xml [它们是相对布局])。

我想创建一个 arrayList(带有字符串和可绘制(图像)),每次我按下 main.xml 中的按钮时,arrayList 的第一个内容将出现在屏幕底部(左、中或右) --> 取决于arrayList 的顺序),当我再次单击时,下一个项目(字符串或可绘制)将出现在它的下方,向上推动它。

更新

我做了一个模型和一个适配器

这是模型

public class ModelC1 {

public String C1Name;
public String C1Text;
public int id;
public boolean isSend;

public ModelC1(String C1Name, String C1Text, int id, boolean isSend){
    this.id = id;
    this.C1Name = C1Name;
    this.C1Text = C1Text;
    this.isSend = isSend;
}

public int getId(){
    return id;
}

public void setId(int id){
    this.id = id;
}

public String getC1Name() {
    return C1Name;
}

public void setC1Name(String C1Name){
    this.C1Name = C1Name;
}

public String getC1Text() {
    return C1Text;
}

public void setC1Text (String C1Text){
    this.C1Text = C1Text ;
}

public boolean isSend() {
    return isSend;
}

public void setIsSend(boolean send){
    isSend = send;
}

这是适配器

public class AdapterC1 extends BaseAdapter {

private List<ModelC1> listChat;
private LayoutInflater inflater;
private Context context;

public AdapterC1(List<ModelC1> listChat, Context context){
    this.listChat = listChat;
    this.context = context;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return listChat.size();
}

@Override
public Object getItem(int i) {
    return listChat.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
    View vi = convertView;
    if(convertView == null ){
        if(listChat.get(i).isSend() == 0)
            vi=inflater.inflate(R.layout.list_send,null);
        else if ((listChat.get(i).isSend() == 1))
            vi=inflater.inflate(R.layout.list_recv,null);
        else if ((listChat.get(i).isSend() == 2))
            vi=inflater.inflate(R.layout.list_mid,null);

    }else{
        if(listChat.get(i).isSend() == 0)
            vi=inflater.inflate(R.layout.list_send,null);
        else if ((listChat.get(i).isSend() == 1))
            vi=inflater.inflate(R.layout.list_recv,null);
        else if ((listChat.get(i).isSend() == 2))
            vi=inflater.inflate(R.layout.list_mid,null);
    }

    if(listChat.get(i).isSend() !=0 || listChat.get(i).isSend() !=1 ||  listChat.get(i).isSend() !=2 ){
        BubbleTextView bubbleTextView = (BubbleTextView) vi.findViewById(R.id.bubbleChat);
        if(bubbleTextView != null)
            bubbleTextView.setText(listChat.get(i).C1Text);
        TextView nameTextView = (TextView) vi.findViewById(R.id.nameChat);
        if(nameTextView != null)
            nameTextView.setText(listChat.get(i).C1Name);
    }else{
        vi=inflater.inflate(R.layout.list_mid,null);
        BubbleTextView bubbleTextView = (BubbleTextView) vi.findViewById(R.id.bubbleChat);
        bubbleTextView.setText("THE END");
    }

    return vi;
}

这里是活动

公共类 Chat1 扩展 AppCompatActivity { private static final String TAG = "Chat1";

private AdapterC1 adapter;
private List<ModelC1> listChat = new ArrayList<>();
private int count = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat1);

    RecyclerView chatContent1 = findViewById(R.id.chatContent1);
}

private ModelC1 setUpMessage(){
    Log.d(TAG, "setUpMessage: Exec");
    return();
}

///OnClick of the button in the activity_chat1.xml
public void nextClicked1(View view) {
    Log.d(TAG, "nextClicked: Is Clicked");

    ///After the limit of the arraylist is reached
    final int limit = 40;
    if(count == limit){
        Log.d(TAG, "nextClicked: Limit Reached");

        Intent i = new Intent(Chat1.this, MainActivity.class);
        startActivity(i);

    }else{
        ///Call the list
        loadList(null);
    }
}

///Load the list of arrays?
public void loadList(View view){
    ModelC1 chat = setUpMessage();
    listChat.add(chat);
    ///The ID of the recycleview in the activity_chat1.xml
    final RecyclerView recyclerview = findViewById(R.id.chatContent1);
    ///The adapter
    final AdapterC1 adapter = new AdapterC1(listChat, this);
    ///Make the recyclerview always scroll

    ///the adapter
         ///recyclerview.setAdapter(adapter);
}

我现在的问题是如何制作 ArrayList(包含字符串和可绘制对象)以及如何链接 ArrayList 以使其在单击按钮时一一显示?

至于 ArrayList,这样的东西会起作用吗?

private List<List<String>> textChat1 = new ArrayList<List<String>>();

ArrayList<String> textChat1 = new ArrayList<String>();
textChat1.add("This is message 1");
textChat1.add("This is message 2");
textChat1.add("This is message 2");
addresses.add(textChat1);

我如何添加图像以及如何说哪些字符串会膨胀哪种布局(左、中或右)?

【问题讨论】:

  • 请发布您的代码
  • 参考这个解决方案。它对 Listview ListviewRecyclview Recyclview 很有帮助
  • @AdamOstrožlík 在这里,我试图了解如何完成它,但我卡住了

标签: android listview arraylist adapter


【解决方案1】:

您可以像这样完成您的工作:在您的AdaptergetView 方法中,

@Override
    public View getView(int position, View convertView, ViewGroup container) {
        if (convertView == null) {
            if (position == 1) {
                convertView = getLayoutInflater().inflate(R.layout.left, container, false);
            } else if (position == 2) {
                convertView = getLayoutInflater().inflate(R.layout.mid, container, false);
            } else {
                convertView = getLayoutInflater().inflate(R.layout.right, container, false);
            }
        }
        //your code here
        return convertView;
    }

这将完成您的工作,但是,我建议您使用Recyclerview,因为它在外观和内存管理方面更高效且更好。

【讨论】:

  • 感谢您的回答,它帮助我开始了工作!我已经更新了我的帖子,你能再帮忙吗?
猜你喜欢
  • 1970-01-01
  • 2013-07-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2016-11-16
  • 1970-01-01
  • 2017-07-10
  • 2020-02-28
相关资源
最近更新 更多