سؤال

I'm having a problem in displaying the custom list i checked the array and it was not null but still the list is not displaying:

Adaptor class:

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

Report class:

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

}

Main class:

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

i'm sure the code is right but there is small mistake i didn't find yet. and thanks.

هل كانت مفيدة؟

المحلول 2

Do this:

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

Also please try to use efficient ListView techniques like View Holder pattern.

نصائح أخرى

In your adapter, in getCount() you return 0. You should return the size of your list.

Try it

 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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top