List View in Android


Step 1. create a xml file name as “friend_detail.xml” in layout .

Step 2. Add a list View .

Step 3 . Create a class file in src name as FriendDetail.java .

Step4 . Write following Code in OnCreate method.

final ListView listview = (ListView) findViewById(R.id.listView1);

String[] values = new String[] { “Android”, “iPhone”, “WindowsMobile”,

“Blackberry”, “WebOS”, “Ubuntu”, “Windows7”, “Max OS X”,

“Linux”, “OS/2”, “Ubuntu”, “Windows7”, “Max OS X”, “Linux”,

“OS/2”, “Ubuntu”, “Windows7”, “Max OS X”, “Linux”, “OS/2”,

“Android”, “iPhone”, “WindowsMobile” };

final ArrayList<String> list = new ArrayList<String>();

for (int i = 0; i < values.length; ++i) {

list.add(values[i]);

}

final StableArrayAdapter adapter = new StableArrayAdapter(this,

android.R.layout.simple_list_item_1, list);

listview.setAdapter(adapter);

Step 5. Create a function after onCreate function .

privateclass StableArrayAdapter extends ArrayAdapter<String> {

HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

public StableArrayAdapter(Context context, int textViewResourceId,

List<String> objects) {

super(context, textViewResourceId, objects);

for (int i = 0; i < objects.size(); ++i) {

mIdMap.put(objects.get(i), i);

}

}

@Override

publiclong getItemId(int position) {

String item = getItem(position);

returnmIdMap.get(item);

}

@Override

publicboolean hasStableIds() {

returntrue;

}

}

Step 6 . Import required Class if any error occure.

Best of Luck.

Image

Advertisements

One thought on “List View in Android

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: