By using setOnClickItemListener() we can get the clicked item in listview.
To add a listview to your XML file check out my other thread....
Listview OnClick Example -
Your Java code should look like
***************************************
public class ListviewOnclickExample extends Activity |
04 | private String lv_arr[]={ "Android" , "iPhone" , "BlackBerry" , "AndroidPeople" , "J2ME" , "Listview" , "ArrayAdapter" , "ListItem" , "Us" , "UK" , "India" }; |
06 | public void onCreate(Bundle icicle) |
08 | super .onCreate(icicle); |
09 | setContentView(R.layout.main); |
10 | lv1=(ListView)findViewById(R.id.ListView01); |
11 | lv1.setAdapter( new ArrayAdapter( this ,android.R.layout.simple_list_item_1 , lv_arr)); |
12 | lv1.setTextFilterEnabled( true ); |
13 | lv1.setOnItemClickListener( new OnItemClickListener() { |
15 | public void onItemClick(AdapterView a, View v, int position, long id) { |
16 | Builder adb= new AlertDialog.Builder(ListviewOnclickExample. this ); |
17 | adb.setTitle( "LVSelectedItemExample" ); |
18 | adb.setMessage( "Selected Item is = " +lv1.getItemAtPosition(position)); |
19 | adb.setPositiveButton( "Ok" , null ); |
package in.wptrafficanalyzer.listviewwithimagesandtext;
ReplyDeleteimport java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
// Array of strings storing country names
String[] countries = new String[] {
"India",
"Pakistan",
"Sri Lanka",
"China",
"Bangladesh",
"Nepal",
"Afghanistan",
"North Korea",
"South Korea",
"Japan"
};
// Array of integers points to images stored in /res/drawable-ldpi/
int[] flags = new int[]{
R.drawable.india,
R.drawable.pakistan,
R.drawable.srilanka,
R.drawable.china,
R.drawable.bangladesh,
R.drawable.nepal,
R.drawable.afghanistan,
R.drawable.nkorea,
R.drawable.skorea,
R.drawable.japan
};
// Array of strings to store currencies
String[] currency = new String[]{
"Indian Rupee",
"Pakistani Rupee",
"Sri Lankan Rupee",
"Renminbi",
"Bangladeshi Taka",
"Nepalese Rupee",
"Afghani",
"North Korean Won",
"South Korean Won",
"Japanese Yen"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Each row in the list stores country name, currency and flag
List> aList = new ArrayList>();
for(int i=0;i<10;i++){
HashMap hm = new HashMap();
hm.put("txt", "Country : " + countries[i]);
hm.put("cur","Currency : " + currency[i]);
hm.put("flag", Integer.toString(flags[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt,R.id.cur};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = ( ListView ) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
}
}
how to add hyperlink in this code please help me