Question

im making project with a custom listview. When i run the project i just get a blank activity. I wrote the BaseAdapter class to put the data in the listview. I pass a String ArrayList to the baseadapter to fill the listview. Any help? Thanks

  public class MainActivity extends Activity {

  ListView L;
 ArrayList<String> data= new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    data.add("two");
    data.add("Three");
    data.add("Four");
    L=(ListView) findViewById(R.id.listView1);

    MyAdapter Ada = new MyAdapter(MainActivity.this,data);
    L.setAdapter(Ada);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

public class MyAdapter extends BaseAdapter {

Context context;
private ArrayList<String> data=  new ArrayList<String>();

 public MyAdapter(Context context ,ArrayList<String> data )
 {
     this.context=context;
     this.data=data;
 }


@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder; 
    convertView = null;
    if (convertView == null) {
        convertView = LayoutInflater.from(context)
          .inflate(R.layout.listlayout, parent, false);

        holder = new ViewHolder(); 

        holder.mainpic = (ImageView)convertView.findViewById(R.id.imageView1);
        holder.text= (TextView)convertView.findViewById(R.id.textView1);
        convertView.setTag(holder); 
    } 
    else
    {
        holder = (ViewHolder) convertView.getTag();

    }

    holder.text.setText(this.data.get(position));


    return convertView;
}







static class ViewHolder {            

    ImageView mainpic; 
    TextView text;
} 

}

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
 tools:context=".MainActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

 </ListView>

</RelativeLayout>
Was it helpful?

Solution

Change getCount() to this:

@Override
public int getCount() {
    return data.size();
}

Currently, you always return 0, which tells the ListView that there are no items to display.

Also, you might consider actually implementing getItem():

@Override
public Object getItem(int position) {
    return data.get(position);
}

And use that to get your items anywhere else.

OTHER TIPS

Don't set convertView null explicitly it will come null only for first time your getView will be called, else it will result in performance issue

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

     ViewHolder holder; 

     if (convertView == null) {
       convertView = LayoutInflater.from(context)
      .inflate(R.layout.listlayout, parent, false);

       holder = new ViewHolder(); 

        holder.mainpic = (ImageView)convertView.findViewById(R.id.imageView1);
        holder.text= (TextView)convertView.findViewById(R.id.textView1);
        convertView.setTag(holder); 
     } else {
        holder = (ViewHolder) convertView.getTag();
     }

     holder.text.setText(this.data.get(position));

     return convertView;
  }

Also make these changes in your code :

@Override
public int getCount() {

    return data.size();
}

@Override
public Object getItem(int position) {

    return data.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top