listview没有获得更新

时间:2016-06-14 11:46:40

标签: android listview

我一直在使用简单的适配器列表视图,我遇到问题,即尽管调用了adapter.notifyDataSetChanged(),但我的listview没有得到更新。它在简单的适配器中工作吗?下面是我的简单适配器代码。 我已经全局声明了简单的适配器

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();

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,因为我已经为适配器创建了单独的类,并且我已经通过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();在将适配器设置为listview之前的片段中