Thursday, June 21, 2012

Android List View

Here i am going to explain about using list view in android. Please follow the steps given below front Step 1 Create a new project and open the activity class. The default code will be like this
public class BasicListViewActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
Step 2 Add list activity as given in the below 1 Step 3 Declare a string array as given below
static final String[]alphabets = new String[]{"A- Apple", "B- Blackberry", "C- Chat", "D- Download", "E- Email", "F- Facebook", "G- Google", "H- HewlettPackard", "I- Iphone", "J- Java", "K- Kingston", "L- Linkedin" };
Step 4 Instead of content view, add list adapter with the list item give the string array as input.
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, alphabets)); getListView().setTextFilterEnabled(true); }
Note: “setTextFilterEnabled(true)” is used to enable text filter option. Step 5 Here is the code for on click functionality for list items.
protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Object obj = this.getListAdapter().getItem(position); String selectedtext = obj.toString(); Toast.makeText(this, "You have clicked: " + selectedtext, Toast.LENGTH_LONG).show(); }
Step 6 Now run your application. Here is your output. 2 You can download the full project here
Arun Kumar Munusamy Web Developer

Morbi aliquam fringilla nisl. Pellentesque eleifend condimentum tellus, vel vulputate tortor malesuada sit amet. Aliquam vel vestibulum metus. Aenean ut mi aucto.

No comments:

Post a Comment