Listview Onclick Example : Getting started with Android
By using setOnClickItemListener() we can get the clicked item in android listview. To add a listview onclick to your XML file check out the previous post ‘Android Listview Example’
Listview OnClick Example –
Your Java code should look like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class ListviewOnclickExample extends Activity
{
private ListView lv1;
private String lv_arr[]={"Android","iPhone","BlackBerry","ArrayAdapter","J2ME", "Listview","ArrayAdapter","ListItem","Us","UK","India"};
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
lv1.setAdapter(new ArrayAdapter
lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(ListviewOnclickExample.this);
adb.setTitle("LVSelectedItemExample");
adb.setMessage("Selected Item is = "+lv1.getItemAtPosition(position));
adb.setPositiveButton("Ok", null);
adb.show();
}
});
}
}
|
0
0