Skip to content Skip to sidebar Skip to footer

How To Create A Listview With Custom Adapter

I have the following code: package com.example.myfirstapp; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.UnknownHost

Solution 1:

Perhaps this simple sample custom adapter listview will get on your feet with this stuff. The main activity

publicclassMainActivityextendsActivity {

    String [] children = {
            "Award 1",
            "Award 2",
            "Award 3",
            "Award 4",
            "Award 5",
            "Award 6",
            "Award 7",
            "Award 8",
            "Award 9",
            "Award 10",
            "Award 11",
            "Award 12",
            "Award 13",
            "Award 14",
            "Award 15"};

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListViewlist= (ListView) findViewById(R.id.listview);
        CustomAdapteradapter=newCustomAdapter(this, children);
        list.setAdapter(adapter);
    }

The custom adapter

publicclassCustomAdapterextendsBaseAdapter {
//  you could have instead extend ArrayAdapter if you wished, i find it less fickle but less flexible//  extends CursorAdapter is available too for listviews backed by cursorsprivate LayoutInflater inflator;
    private String[] children;

    publicCustomAdapter(Context context, String[] children) {
        super();
//      pass what you need into the constructor. in this case the string array and context.//      do as much as you can here and not in getView because getView acts for each row//      --> it will greatly help performancethis.children = children;
        inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @OverridepublicintgetCount() {
//      v----  your listview won't show anything if this is left default (at 0).  return children.length;
    }

    @Overridepublic Object getItem(int position) {
        returnnull;
    }

    @OverridepubliclonggetItemId(int position) {
        return0;
    }

    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
//      getView is where all the action takes place//      first inflate the xml that holds the row and somehow connect it to convertView, the parameter//      checking if null allows these views to be recycled when they go off-screen not just made one per row//      ---> it will greatly help performance if (convertView == null) {
            convertView = inflator.inflate(R.layout.row, parent, false);
        }
//      then find the individual views with this xml (everything just like onCreate)ImageViewimg= (ImageView) convertView.findViewById(R.id.imageView1);
        TextViewtv= (TextView) convertView.findViewById(R.id.textView1);
//      then perform your actions to the your views//      each textView is set to an element in the array based on position. this is my listview limiter here.//      each imageview is set to the same picture but you should now have an idea how to set different images (based on position)//      using listview position in correspondence with array/arraylist positions is a very useful technique.
        img.setImageResource(R.drawable.ic_launcher);
        tv.setText(children[position]);
//      v---- return your view, it's important.return convertView;
    }

}

row.xml

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content" ><ImageViewandroid:id="@+id/imageView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:src="@drawable/ic_launcher" /><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:text="TextView" /></RelativeLayout>

The result sshot

To get super acquainted with listview check out this video: http://www.youtube.com/watch?v=wDBM6wVEO70

As for your other issue, you're gonna have to post a logcat for better responses.

Post a Comment for "How To Create A Listview With Custom Adapter"