tlgtmc
tlgtmc

Reputation: 127

Android Listview Adds The Same Item

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

Upvotes: 1

Views: 1009

Answers (2)

OMAK
OMAK

Reputation: 1031

First Add new data to ctvList

then call adapter.notifyDataSetChanged();

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

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157487

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

Upvotes: 5

Related Questions