Android Listview Adds The Same Item

主宰稳场 提交于 2019-12-02 09:19:25

问题


I'm developing an Android Application and I'm using Android ListView. I'm getting datas from the web service and fill them into the Arraylist. It's size is 37. Then i try to fill the listview with Arraylist but it always get the same element(last one). Below you can see the code part:

private void ctv_listele(String res){
ArrayList<CTV> ctvList = new ArrayList<CTV>();

try {
    JSONObject jsonObject = new JSONObject(res);
    JSONArray jsonArray = jsonObject.getJSONArray("tarife");
    int max = jsonArray.length();
    for(int i = 0; i < max; i++) {
        JSONObject tmp = jsonArray.getJSONObject(i);
        ctv.setYear(tmp.getString("Year"));
        ctv.setYearlyCost(tmp.getString("YearlyCost"));
        ctv.setMonthlyCost(tmp.getString("MonthlyCost"));
        ctv.setGroup(tmp.getString("Group"));
        ctv.setDegree(tmp.getString("Degree"));
        Log.e("Added",tmp.getString("YearlyCost"));
        ctvList.add(ctv);
    }
    Log.e("End",String.valueOf(ctvList.size()));
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
  ListView productList = (ListView) findViewById(R.id.listView_ctv);
  MyCustomAdapter adapter = new MyCustomAdapter(this, R.layout.ctv_row, ctvList);
  productList.setAdapter(adapter);

}


public class MyCustomAdapter extends ArrayAdapter<CTV>{

private Activity context;
private ArrayList<CTV> liste;
private LayoutInflater layoutInflater;
private AdapterSatir adaSatir;

public MyCustomAdapter(Activity context, int ctvRow, ArrayList<CTV> objects) {
    super(context, R.layout.ctv_row, objects);
    this.context=context;
    this.liste=objects;
    Log.e("liste",String.valueOf(liste.size()));
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View view_satir=convertView;
    if(view_satir==null) {
        adaSatir=new AdapterSatir();
        layoutInflater=context.getLayoutInflater();
        view_satir=layoutInflater.inflate(R.layout.cevretemizliktarifeleri_row, null,true);
        adaSatir.textView1=(TextView) view_satir.findViewById(R.id.textView_ctvlist1);
        adaSatir.textView2=(TextView) view_satir.findViewById(R.id.textView_ctvlist2);
        adaSatir.textView3=(TextView) view_satir.findViewById(R.id.textView_ctvlist3);
        adaSatir.textView4=(TextView) view_satir.findViewById(R.id.textView_ctvlist4);
        view_satir.setTag(adaSatir);
    } else {
        adaSatir = (AdapterSatir) view_satir.getTag();
    }
    adaSatir.textView1.setText(liste.get(position).getDegree());
    adaSatir.textView2.setText(liste.get(position).getGroup());
    adaSatir.textView3.setText(liste.get(position).getMonthlyCost());
    adaSatir.textView4.setText(liste.get(position).getYearlyCost());
    return view_satir;
}

private class AdapterSatir
{
    public TextView textView1;
    public TextView textView2;
    public TextView textView3;
    public TextView textView4;
}
}

回答1:


for(int i = 0; i < max; i++) {
        JSONObject tmp = jsonArray.getJSONObject(i);
        ctv.setYear(tmp.getString("Year"));
        ctv.setYearlyCost(tmp.getString("YearlyCost"));
        ctv.setMonthlyCost(tmp.getString("MonthlyCost"));
        ctv.setGroup(tmp.getString("Group"));
        ctv.setDegree(tmp.getString("Degree"));
        Log.e("Added",tmp.getString("YearlyCost"));
        ctvList.add(ctv);
    }

you forget to init your cvt object at every iteration. So you add always the same reference to the list.

for(int i = 0; i < max; i++) {
        JSONObject tmp = jsonArray.getJSONObject(i);
        CTV cvt = new CVT();
        ctv.setYear(tmp.getString("Year"));
        ctv.setYearlyCost(tmp.getString("YearlyCost"));
        ctv.setMonthlyCost(tmp.getString("MonthlyCost"));
        ctv.setGroup(tmp.getString("Group"));
        ctv.setDegree(tmp.getString("Degree"));
        Log.e("Added",tmp.getString("YearlyCost"));
        ctvList.add(ctv);
    }



回答2:


First Add new data to ctvList

then call adapter.notifyDataSetChanged();

Then new data will reflect in the List view please check this



来源:https://stackoverflow.com/questions/16564986/android-listview-adds-the-same-item

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