[英]android custom list not displaying
我在檢查數組時顯示自定義列表時遇到問題,它不為空,但列表未顯示:
適配器類:
public class ReportAdapter extends BaseAdapter{
private Context context;
Report data[] = null;
public ReportAdapter(Context context, Report[] d) {
data = d;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
if(convertView == null){
LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
row = inf.inflate(R.layout.list_row, null);
}
ImageView imageUrl = (ImageView)row.findViewById(R.id.imageUrl);
TextView reportName = (TextView)row.findViewById(R.id.reportName);
TextView reportState = (TextView)row.findViewById(R.id.reportState);
TextView reportTime = (TextView)row.findViewById(R.id.reportTime);
//Report report = data[position];
imageUrl.setImageDrawable(data[position].Image);
reportName.setText(data[position].Name);
reportState.setText(data[position].State);
reportTime.setText(data[position].Time);
return row;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
}
報告類別:
public class Report {
public String Name;
public String Time;
public Drawable Image;
public String State;
public Report(){
super();
}
public Report(String Name, String Time,Drawable Image,String State) {
super();
this.Name = Name;
this.Time = Time;
this.Image = Image;
this.State = State;
}
}
主班:
Report report_data[] = new Report[reports.length()];
//start loop
report_data[i] = new Report(c.getString(TAG_TITLE),c.getString(TAG_TIME),drawable,"Pidding");
// end loop
list = (ListView)findViewById(R.id.list);
ReportAdapter adapter = new ReportAdapter(Display_Reports.this,report_data);
list.setAdapter(adapter);
我確定代碼是正確的,但還有一個我沒有發現的小錯誤。 謝謝
在適配器的getCount()
, return 0
。 您應該返回列表的大小。
做這個:
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
另外,請嘗試使用有效的ListView
技術(例如“視圖持有者”模式)。
試試吧
Report report_data[] = new Report[reports.length()];
report_data[i] = new Report(c.getString(TAG_TITLE),c.getString(TAG_TIME),drawable,"Pidding");
list = (ListView)findViewById(R.id.list);
ReportAdapter adapter = new ReportAdapter(Display_Reports.this,report_data);
list.setAdapter(adapter);
adapter.clear();
adapter.addAll(report_data[]); //if API >= 11
for(int i=0; i<report_data.length, i++) //else))
{adapter.add(report_data[i]);}
adapter.notifyDataSetChanged();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.