【问题标题】:adding view on the bottom of listview在列表视图底部添加视图
【发布时间】:2019-11-01 13:54:36
【问题描述】:

为我的 ListView 初始化自定义适配器后,它会绘制数据集的第一部分。现在我想在列表视图的底部添加一些其他视图。但是当我使用 notifyDataSetChanged 它不会刷新列表视图。

设置

                ListView messages = findViewById(R.id.latest_messages);


                adapter = new AdapterChat(getApplicationContext(),
                        R.layout.element_chat_other, inputItems);


                messages = (ListView) findViewById(R.id.messages);
                messages.setAdapter(adapter);
                messages.setSmoothScrollbarEnabled(true);

更新不起作用:

我在数组列表中添加了一个新对象,然后调用适配器以刷新数据。 但是视图仍然没有显示:

                inputItems.add(new DTOChat(fromCC,messageCC, tsCC, idCC));
                adapter = new AdapterChat(getApplicationContext(),
                        R.layout.element_chat_other, inputItems);


                adapter.notifyDataSetChanged();

适配器

public class AdapterChat extends ArrayAdapter<DTOChat > {

Context context;
int rand = 0;

public AdapterChat(Context context, int risorsaId,
                   List<DTOChat> items) {
    super(context, risorsaId, items);
    this.context = context;
}




public View getView(int position, View convertView, ViewGroup parent) {

    //controllo se c'è gia un layout
    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    final DTOChat content = getItem(position);

    SharedPreferences preferences = getContext().getSharedPreferences("pref", Context.MODE_PRIVATE);
    String username = preferences.getString("username", "");

    if(content.getFrom().toLowerCase().equals(username.toLowerCase())) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.element_chat_other, null);
        }
    } else {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.element_chat_me, null);
        }
    }

    final TextView msg = convertView.findViewById(R.id.msg);
    msg.setText(content.getMessage());


    //data
    TextView ts = convertView.findViewById(R.id.ts);
    String[] chunks = content.getTs().split(" ");
    String[] chunks2 = chunks[1].split(":");
    ts.setText(chunks2[0]+":"+chunks2[1]);




    return convertView;

}

}

【问题讨论】:

    标签: android listview adapter


    【解决方案1】:

    通过以下语句,您将AdapterChat 的实例分配给适配器

    adapter = new AdapterChat(getApplicationContext(),
                        R.layout.element_chat_other, inputItems);
    

    然后将此实例设置为ListView 的适配器

    messages.setAdapter(adapter);
    

    一段时间后,您将一个新项目添加到数据列表inputItems,并将AdapterChat 的另一个实例分配给适配器

    inputItems.add(new DTOChat(fromCC,messageCC, tsCC, idCC));
    adapter = new AdapterChat(getApplicationContext(),
                        R.layout.element_chat_other, inputItems);
    

    现在,旧实例仍设置为当前的 ListView 适配器,但当您调用 adapter.notifyDataSetChanged(); 时,您会通知您分配给 适配器 的最近创建的实例。

    您可以保持 adapter 原样(不要创建新实例),只需将新项目添加到 inputItems,然后直接调用

    adapter.notifyDataSetChanged();
    

    【讨论】:

    • 像你建议的那样做我得到了这个错误:java.lang.ArrayIndexOutOfBoundsException: length=1;索引=1
    • @LorenzoCatalano - 那么你已经更进一步(适配器尝试使用修改后的列表),所以任务至少部分完成(在你的问题范围内)。但是,您的适配器实现似乎存在问题。如果您愿意,可以将您的 AdapterChat 代码以及堆栈跟踪粘贴到您的问题中,我们可以对其进行调查
    • 已添加到帖子中
    • @LorenzoCatalano - 和堆栈跟踪?发生异常的行是哪一行?
    【解决方案2】:

    这样试试

    inputItems.add(new DTOChat(fromCC,messageCC, tsCC, idCC));
    // here no need to create new adapter instance only add below line
    adapter.notifyDataSetChanged();
    

    【讨论】:

    • 这个错误:java.lang.ArrayIndexOutOfBoundsException:length=1;索引=1
    • 我使用数组列表:ArrayList inputItems = new ArrayList();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多