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
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.
You can download the full project here
No comments:
Post a Comment