如何使用notifyDataSetChanged()

时间:2015-11-08 22:18:59

标签: java android adapter

您好我的适配器有问题,我不知道如何在数据更改后使用adapter.notifyDataSetChanged()时在ListView中刷新我的数据。我想知道我是否必须在此课程中覆盖notifyDataSetChanged()。我希望你能帮助我。 这是我的适配器类:

public class ListViewAdapter extends BaseAdapter{

    public ArrayList<HashMap<String, String>> list;
    Activity activity;
    TextView txtFirst;
    TextView txtSecond;
    TextView txtThird;
    TextView txtFourth;
    public static final String FIRST_COLUMN="First";
    public static final String SECOND_COLUMN="Second";
    public static final String THIRD_COLUMN="Third";
    public static final String FOURTH_COLUMN="Fourth";


    public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
        super();
        this.activity=activity;
        this.list=list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub



        LayoutInflater inflater=activity.getLayoutInflater();

        if(convertView == null){

            convertView=inflater.inflate(R.layout.lista_wlasnych_spolek, null);

            txtFirst=(TextView) convertView.findViewById(R.id.nazwa_spolki);
            txtSecond=(TextView) convertView.findViewById(R.id.wartosc_akt);
            txtThird=(TextView) convertView.findViewById(R.id.wartosc_kupna);
            txtFourth=(TextView) convertView.findViewById(R.id.wartosc_calosci);

        }

        HashMap<String, String> map=list.get(position);
        txtFirst.setText(map.get(FIRST_COLUMN));
        txtSecond.setText(map.get(SECOND_COLUMN));
        txtThird.setText(map.get(THIRD_COLUMN));
        txtFourth.setText(map.get(FOURTH_COLUMN));

        return convertView;
    }

}

这里我添加了使用此适配器的片段类:

public class Tab2Fragment extends Fragment {


    private ArrayList<HashMap<String, String>> list;

    public static final String FIRST_COLUMN="First";
    public static final String SECOND_COLUMN="Second";
    public static final String THIRD_COLUMN="Third";
    public static final String FOURTH_COLUMN="Fourth";
    public ListViewAdapter adapter;


    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.spolki_portfel, container, false);


            ListView listView=(ListView) v.findViewById(R.id.listView1);
            list=new ArrayList<HashMap<String,String>>();
            LayoutInflater inflater1 = getActivity().getLayoutInflater();
            View header = inflater1.inflate(R.layout.naglowek_wlasne_spolki, listView, false);
            listView.addHeaderView(header);







        for(int i = 0 ; i < MainActivity.zmienne.lista_moich_spolek.size();i++){
            dodaj_spolke(MainActivity.zmienne.lista_moich_spolek.get(i).nazwa,MainActivity.zmienne.lista_moich_spolek.get(i).wartosc_aktualna,MainActivity.zmienne.lista_moich_spolek.get(i).ilosc,MainActivity.zmienne.lista_moich_spolek.get(i).cena_kupna);
        }


            adapter=new ListViewAdapter(getActivity(), list);
            listView.setAdapter(adapter);












            //THIS INCREASE VALUE
            MainActivity.zmienne.lista_moich_spolek.get(0).cena_kupna++;
            adapter.notifyDataSetChanged();


            //CHECKING IF VALUE IS HIGHER
            System.out.println("cena " + MainActivity.zmienne.lista_moich_spolek.get(0).cena_kupna);


        return v;
    }

3 个答案:

答案 0 :(得分:2)

不完全是,但您可能实际上并未实际更新适配器列表。在适配器中创建一个看起来像这样的方法

public void updateItems(ArrayList<HashMap<String, String>> newList) {
    list.clear();
    list.addAll(newList);
    this.notifyDataSetChanged();
}

然后从适配器设置的任何地方调用它

mAdapter.updateItems(newList)

答案 1 :(得分:0)

您的问题可能是因为您没有合适的适配器类。尝试这个,我没有时间测试它,但它应该工作,也可以在向该列表添加新元素后调用notifiyDataSetChanged()。

public class ListViewAdapter extends ArrayAdapter<HashMap<String, String>> {

public ArrayList<HashMap<String, String>> list;
private Activity activity;
public static final String FIRST_COLUMN="First";
public static final String SECOND_COLUMN="Second";
public static final String THIRD_COLUMN="Third";
public static final String FOURTH_COLUMN="Fourth";


public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
    super(activity, R.layout.lista_wlasnych_spolek, list);
    this.activity=activity;
    this.list=list;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public HashMap<String, String> getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ListViewHolder listViewHolder;
    if(convertView == null){
        listViewHolder = new ListViewHolder();
        convertView = activity.getLayoutInflater().inflate(R.layout.lista_wlasnych_spolek, null);
        listViewHolder.txtFirst = (TextView) convertView.findViewById(R.id.nazwa_spolki);
        listViewHolder.txtSecond = (TextView) convertView.findViewById(R.id.wartosc_akt);
        listViewHolder.txtThird = (TextView) convertView.findViewById(R.id.wartosc_kupna);
        listViewHolder.txtFourth = (TextView) convertView.findViewById(R.id.wartosc_calosci);
        convertView.setTag(listViewHolder);
    } else {
        listViewHolder = (ListViewHolder) convertView.getTag();
    }

    HashMap<String, String> map=list.get(position);
    listViewHolder.txtFirst.setText(map.get(FIRST_COLUMN));
    listViewHolder.txtSecond.setText(map.get(SECOND_COLUMN));
    listViewHolder.txtThird.setText(map.get(THIRD_COLUMN));
    listViewHolder.txtFourth.setText(map.get(FOURTH_COLUMN));

    return convertView;
}

public class ListViewHolder {
    TextView txtFirst;
    TextView txtSecond;
    TextView txtThird;
    TextView txtFourth;
}

}

答案 2 :(得分:0)

好的,我很抱歉浪费你的时间这是我的错,我错过了我正在改变数据来源但没有exacly ListView我应该改变list.get(0).put("Fourth", "5");但我正在改变MainActivity.zmienne.lista_moich_spolek.get(0).cena_kupna++;我想知道为什么它没有工作。