1

我正在开发一个 Android 应用程序,我正在使用 Android ListView。我正在从 Web 服务获取数据并将它们填充到 Arraylist 中。它的大小是 37。然后我尝试用 Arraylist 填充列表视图,但它总是得到相同的元素(最后一个)。下面你可以看到代码部分:

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

2 回答 2

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

您忘记在每次迭代时初始化您的 cvt 对象。因此,您始终向列表添加相同的引用。

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);
    }
于 2013-05-15T12:29:13.650 回答
0

首先向 ctvList 添加新数据

然后调用 adapter.notifyDataSetChanged();

然后新数据将反映在列表视图中,请检查此

于 2013-05-15T12:33:42.180 回答