我正在开发一个Android应用程序,我正在使用Android ListView。我从Web服务获取数据并将其填入Arraylist。它的大小是37.然后我尝试用Arraylist填充listview,但它总是得到相同的元素(最后一个)。您可以在下面看到代码部分:
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;
}
}
答案 0 :(得分: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);
}
答案 1 :(得分:0)
首先将新数据添加到ctvList
然后调用adapter.notifyDataSetChanged();
然后新的数据将反映在列表视图中,请检查