how to use notifyDataSetChanged()

安稳与你 提交于 2020-04-30 06:13:10

问题


Hi i have problem with my adapter, i don't know how to refresh my data in ListView when i use adapter.notifyDataSetChanged() after data changing nothing happen. I wonder if i have to override notifyDataSetChanged() in this class. I hope you will help me. Here is my adapter class:

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;
    }

}

here im adding my fragment class which use this adapter:

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;
    }

回答1:


Not exactly, but you're probably not actually updating the adapter's list. Make a method in your adapter that looks like this

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

Then call this from wherever you have the adapter set up

mAdapter.updateItems(newList)



回答2:


Your problem might be because you don't have the right Adapter class. Try this one, I didn't had time to test it but it should work, also call notifiyDataSetChanged() after adding new elements to that list.

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;
}

}



回答3:


Ok, im sorry for wasting your time it was my fault i missed that i was changing source of data but no exacly ListView I should change list.get(0).put("Fourth", "5"); but i was changing MainActivity.zmienne.lista_moich_spolek.get(0).cena_kupna++; and I wondered why it didn't work.



来源:https://stackoverflow.com/questions/33599674/how-to-use-notifydatasetchanged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!