0

我在显示自定义列表时遇到问题我检查了数组,它不是空的,但列表仍然没有显示:

适配器类:

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

我确定代码是正确的,但是我还没有发现一个小错误。谢谢。

4

3 回答 3

3

在您的适配器中,在getCount()您中return 0。您应该返回列表的大小。

于 2013-04-15T12:57:54.373 回答
1

做这个:

@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技术,如 View Holder 模式。

于 2013-04-15T13:01:34.207 回答
0

尝试一下

 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();
于 2013-04-15T12:59:22.970 回答