【问题标题】:listview not getting update [duplicate]listview没有得到更新[重复]
【发布时间】:2016-06-14 11:46:39
【问题描述】:

我一直在使用简单的适配器列表视图,但我的列表视图没有得到更新,尽管调用了 adapter.notifyDataSetChanged();那么它在简单的适配器中工作吗?下面是我的简单适配器代码。 我已经在全球范围内声明了简单的适配器

adapter = new SimpleAdapter(
                        getActivity(), ssLst,
                        R.layout.surgerysch_item, new String[]{
                        TAG_SIMRDNO, TAG_SIPNME, TAG_SISEX,
                        TAG_SIDOB, TAG_SIPROC, TAG_SIOTNME,
                        TAG_SIOTME, TAG_DRNAME}, new int[]{R.id.txtsimrdNo,
                        R.id.txtsiptnNme, R.id.txtsiSex,
                        R.id.txtsiDob, R.id.txtsiProc,
                        R.id.txtsiotNme, R.id.txtsiotTme, R.id.txtDrnme});

                lstViw.setAdapter(adapter);
                adapter.notifyDataSetChanged();

【问题讨论】:

  • 在将其设置为列表视图之前调用adapter.notifyDataSetChanged();
  • @Enlightened 你如何在SimpleAdapter 中获取arrayList?你是在创建新对象还是只是一个引用?
  • 调用adapter.notifyDataSetChanged()没有意义;设置适配器后立即。发布 SimpleAdapter 类。
  • 发送适配器类和活动类
  • @mahadev,@janki 我已经通过使用 BaseAdapter 解决了它,并且我调用了 adapter.notifyDataSetChanged();在将适配器设置为列表视图之前

标签: android listview


【解决方案1】:

我已经解决了这个问题,因为我为适配器创建了单独的类,并且我已经通过 BaseAdapter 进行了扩展,所以下面是我的代码

公共类 SurgeryAdapter 扩展 BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;

public SurgeryAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data = d;
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.surgerysch_item, null);

    TextView txt_Mrdno = (TextView) vi.findViewById(R.id.txtsimrdNo);
    TextView txt_Ptnnme = (TextView) vi.findViewById(R.id.txtsiptnNme);
    TextView txt_Sex = (TextView) vi.findViewById(R.id.txtsiSex);
    TextView txt_Dob = (TextView) vi.findViewById(R.id.txtsiDob);
    TextView txt_Proce = (TextView) vi.findViewById(R.id.txtsiProc);
    TextView txt_Otnme = (TextView) vi.findViewById(R.id.txtsiotNme);
    TextView txt_Ottme = (TextView) vi.findViewById(R.id.txtsiotTme);
    TextView txtdrNme = (TextView) vi.findViewById(R.id.txtDrnme);
    TextView txtanstetic = (TextView) vi.findViewById(R.id.txtAnesthestist);


    HashMap<String, String> item = new HashMap<String, String>();
    item = data.get(position);

    //Setting all values in listview
    txt_Mrdno.setText(item.get("mrd_no"));
    txt_Ptnnme.setText(item.get("pname"));
    txt_Sex.setText(item.get("sex"));
    txt_Dob.setText(item.get("dob"));
    txt_Proce.setText(item.get("procedure"));
    txt_Otnme.setText(item.get("otName"));
    txt_Ottme.setText(item.get("otTime"));
    txtdrNme.setText(item.get("docName"));
    txtanstetic.setText(item.get("anesthetists"));
    return vi;
}

}

我已经调用了 adapter.notifyDataSetChanged();在将适配器设置为列表视图之前在我的片段中

【讨论】:

  • 您好 Lakshman,请使用 pojo 类获取有效代码
  • k mahadev 我会用它谢谢你的建议
猜你喜欢
  • 1970-01-01
  • 2017-11-25
  • 2011-02-24
  • 2021-07-10
  • 2017-08-17
  • 2018-04-25
  • 1970-01-01
  • 2013-06-04
相关资源
最近更新 更多