Android Listview adds the same item

I am developing an Android application and I am using Android ListView. I get data from a web service and populate it with Arraylist. Its size is 37. Then I try to populate the listview with an Arraylist, but it always gets the same element (the last). Below you can see part of the code:

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
source share
2 answers
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 will forget to start your cvt object at each iteration. This way you always add the same link 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);
    }
+5
source

First add new data to ctvList

then call adapter.notifyDataSetChanged ();

"", ,

0

Source: https://habr.com/ru/post/1658662/


All Articles