【问题标题】:How can I auto refresh my listview in Fragment? [duplicate]如何在 Fragment 中自动刷新我的列表视图? [复制]
【发布时间】:2019-09-25 05:14:08
【问题描述】:

输入数据后,我正在使用 Base Adapter 实现自定义列表视图,我的列表不会自动刷新。我需要重新打开片段才能看到更新。

public class Lead_Adapter extends BaseAdapter {
private Context context;
private ArrayList<Lead_Model> leadarrayList;

public Lead_Adapter(Context context,ArrayList<Lead_Model>arrayList){

    this.context = context;
    this.leadarrayList = arrayList;

}


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

@Override
public Object getItem(int position) {
    return leadarrayList.get(position);
}

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

@Override
public View getView(int position, View convert_view, ViewGroup parent) {
    LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convert_view = layoutInflater.inflate(R.layout.lead_listview, null);
    TextView Company_Name = convert_view.findViewById(R.id.company_name_listview);
    TextView Lead_Status = convert_view.findViewById(R.id.lead_status_listview);
    TextView Lead_Date = convert_view.findViewById(R.id.lead_date_listview);

    Lead_Model leadModel = leadarrayList.get(position);
    Company_Name.setText(leadModel.getCompany_name());
    Lead_Status.setText(leadModel.getLead_status());
    Lead_Date.setText(leadModel.getDate());
    return convert_view;
}

【问题讨论】:

  • after entering the data你是怎么做到的\/
  • 设置滑动刷新​​以刷新列表
  • @VladyslavMatviienko 要查看更新,我需要重新打开片段
  • @VladyslavMatviienko 要查看更新,我需要重新打开片段
  • 将数据添加到leadarrayList后,需要notifyDataSetChanged()

标签: java android android-listview


【解决方案1】:

将数据添加到片段中的适配器后,这样做

adapter.notifyDataSetChanged();

【讨论】:

  • @TARUNSHARMA 如果有帮助,请接受答案并点赞。
  • 请不要要求为您的答案投票。好不好由你来选择。好的话自然会被社区点赞
【解决方案2】:

添加完数据后,您可以致电notifyDataSetChanged()

【讨论】:

    【解决方案3】:

    当您将项目添加到片段中的 arrayList 时,您必须执行 adapter.notifyDataSetChanged()

    【讨论】:

      【解决方案4】:

      输入数据后只需通知列表,无需重新打开片段。

      items.clear();
      items = db.getItems(); // fetch items from database
      listAdapter.notifyDataSetChanged();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-31
        • 1970-01-01
        • 1970-01-01
        • 2018-07-27
        • 2014-12-14
        • 1970-01-01
        相关资源
        最近更新 更多