get value from a list view - android

I have a list view.when any one clicking the list it showing list content as
{first_name=abc, last_name=xyz, id=1, address=kolkata}
but I want those get those value individually from that string who to get that.
My onListItemClick Listner
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String return_data = o.toString();
Toast.makeText(this, ""+return_data, Toast.LENGTH_LONG).show();
}
Added total class including List adapter
public class showUserInfoListActivity extends ListActivity {
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
static final String KEY_ID = "id";
static final String KEY_FIRST_NAME = "first_name";
static final String KEY_LAST_NAME = "last_name";
static final String KEY_ADDRESS = "address";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_user_info_list);
//---get all Records---
DataBaseAdapter db = new DataBaseAdapter(this);
db.open();
Cursor c = db.getAllRecords();
if (c.moveToFirst())
{
do
{
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(KEY_ID, c.getString(0));
map.put(KEY_FIRST_NAME, c.getString(1));
map.put(KEY_LAST_NAME, " "+c.getString(2));
map.put(KEY_ADDRESS, c.getString(3));
// adding HashList to ArrayList
menuItems.add(map);
} while (c.moveToNext());
}
db.close();
// Adding menuItems to ListView
// All filed data are not shown in the list KEY_ID is hidden
ListAdapter adapter = new SimpleAdapter(this, menuItems,R.layout.user_info_list_item,
new String[] { KEY_FIRST_NAME, KEY_LAST_NAME, KEY_ADDRESS, KEY_ID },
new int[] {R.id.first_name , R.id.last_name, R.id.address});
setListAdapter(adapter);
}
//On select from the list show data
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
//String return_data = o.toString();
MyClass return_data = (MyClass)o;
Toast.makeText(this, ""+return_data, Toast.LENGTH_LONG).show();
}
class MyClass{
}
}

Instead of
String return_data = o.toString();
you need to cast o to whatever class of object it is.
MyClass return_data = (MyClass)o;
Then you can access its fields and call its methods as usual.
In your case:
HashMap<String, String> returndata = (HashMap<String, String>) o;

Related

java.lang.String cannot be cast to java.util.HashMap

When i run the following program:
ArrayList<HashMap<String, String>> AL = new ArrayList<HashMap<String, String>>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_outaccountinfo);
ListView lv = (ListView) findViewById(R.id.lvoutinfo);
ArrayList<HashMap<String, String>> AL = new ArrayList<HashMap<String, String>>();
DBOpen open = new DBOpen(Outaccountinfo.this);
SQLiteDatabase db = open.getWritableDatabase();
Cursor cursor = db.query("outaccount", null, null, null, null, null, null, null);
while (cursor.moveToNext()) {
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
String str_id = cursor.getString(cursor.getColumnIndex("id"));
String str_money = cursor.getString(cursor.getColumnIndex("outmoney"));
String str_time = cursor.getString(cursor.getColumnIndex("time"));
String str_type = cursor.getString(cursor.getColumnIndex("type"));
String str_mark = cursor.getString(cursor.getColumnIndex("mark"));
map.put("id", str_id);
map.put("outmoney", str_money);
map.put("time", str_time);
map.put("type", str_type);
map.put("mark", str_mark);
AL.add(map);
} while (cursor.moveToNext());
}
}
String[] str = new String[cursor.getCount()];
for (int i = 0; i < cursor.getCount(); i++) {
str[i] = AL.get(i).get("id").toString() + "|" + " " + "money:" + String.valueOf(AL.get(i).get("outmoney"))
+ " " + "time:" + AL.get(i).get("time").toString() + " " + "type:" + AL.get(i).get("type").toString()
+ " " + "mark:" + AL.get(i).get("mark").toString();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
str);
lv.setAdapter(arrayAdapter);
}
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
HashMap<String, String> map = (HashMap<String, String>) parent.getItemAtPosition(position);
Toast.makeText(Outaccountinfo.this, map.get("type"), Toast.LENGTH_SHORT).show();
}
});
}
I receive the following error:
java.lang.String cannot be cast to java.util.HashMap
Who can help me to achieve the purpose without changing the purpose of the program?
Thanks!
You are using ArrayAdapter<String> Means that you pass your adapter object of Strings.
Now, when you clicked on your item and asked for parent.getItemAtPosition(position); you get an Object.
If you will go inside this function you can see that the Object that you get is by position and by the type you sent to the Adapter (String).
So, when you try to cast that String to (HashMap<String, String>) you get the exception.
Try to do something like that after you clicked:
HashMap<String, String> map = new HashMap<String, String>(); // Parameter in outside the click listener
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
map.put("someKey", parent.getItemAtPosition(position));
Toast.makeText(Outaccountinfo.this, map.get("type"), Toast.LENGTH_SHORT).show();
}
});
*****Can you try this code It might be helpful.*****
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView) findViewById(R.id.lvoutinfo);
ArrayList<HashMap<String, String>> AL = new ArrayList<HashMap<String, String>>();
SQLiteDatabase db = openOrCreateDatabase("Account",MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS outaccount(id text,outmoney text,time text,type text,mark text);");
db.execSQL("INSERT INTO outaccount VALUES('1','100','10','saving','80');");
Cursor cursor = db.query("outaccount", null, null, null, null, null, null, null);
while (cursor.moveToNext()) {
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
String str_id = cursor.getString(cursor.getColumnIndex("id"));
String str_money = cursor.getString(cursor.getColumnIndex("outmoney"));
String str_time = cursor.getString(cursor.getColumnIndex("time"));
String str_type = cursor.getString(cursor.getColumnIndex("type"));
String str_mark = cursor.getString(cursor.getColumnIndex("mark"));
map.put("id", str_id);
map.put("outmoney", str_money);
map.put("time", str_time);
map.put("type", str_type);
map.put("mark", str_mark);
AL.add(map);
} while (cursor.moveToNext());
}
}
String[] str = new String[cursor.getCount()];
for (int i = 0; i < cursor.getCount(); i++) {
str[i] = AL.get(i).get("id").toString() + "|" + " " + "money:" + String.valueOf(AL.get(i).get("outmoney"))
+ " " + "time:" + AL.get(i).get("time").toString() + " " + "type:" + AL.get(i).get("type").toString()
+ " " + "mark:" + AL.get(i).get("mark").toString();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
str);
lv.setAdapter(arrayAdapter);
}
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
HashMap<String, String> map = (HashMap<String, String>) parent.getItemAtPosition(position);
Toast.makeText(MainActivity.this, map.get("type"), Toast.LENGTH_SHORT).show();
}
});
}
}

ListFragment in onItemClickListener() startActivity crashes app

I have problem with calling startActivity(intent) from onItemClickListener() on ListFragment. When I click on an item from listview the application crashes.
Code is here:
public class AndroidFragment extends SherlockListFragment{
static{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
static final String URL = "***";
// XML node keys
static final String KEY_ITEM = "novost"; // parent node
//static final String KEY_ID = "id";
static final String KEY_NAME = "naslov";
static final String KEY_COST = "datum";
static final String KEY_DESC = "text";
static final String KEY_LINK = "link";
static final String KEY_LINK1 = "doc";
ArrayList<HashMap<String, String>> menuItems;
String[] from = { KEY_NAME, KEY_DESC, KEY_COST,KEY_LINK,KEY_LINK1};
/** Ids of views in listview_layout */
int[] to = { R.id.naslov, R.id.novost, R.id.datum,R.id.link,R.id.link1};
ListView list;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new loadListView().execute();
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.naslov)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.datum)).getText().toString();
String description = ((TextView) view.findViewById(R.id.novost)).getText().toString();
String link = ((TextView) view.findViewById(R.id.link)).getText().toString();
String link1 = ((TextView) view.findViewById(R.id.link1)).getText().toString();
//String link_asd=link;
// Starting new intent
Intent in = new Intent(getActivity().getBaseContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_NAME, name);
in.putExtra(KEY_COST, cost);
in.putExtra(KEY_DESC, description);
if(link==null)
{
link=null;
}else{
in.putExtra(KEY_LINK, link);
in.putExtra(KEY_LINK1, link1);
}
//in.putExtra("link1", link_asd);
startActivity(in);
}
});
}
/*#Override
public void onListItemClick(ListView list, View v, int position, long id) {
/**
* Toast message will be shown when you click any list element
//Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
String name = ((TextView) v.findViewById(R.id.naslov)).getText().toString();
String cost = ((TextView) v.findViewById(R.id.datum)).getText().toString();
String description = ((TextView) v.findViewById(R.id.novost)).getText().toString();
String link = ((TextView) v.findViewById(R.id.link)).getText().toString();
String link1 = ((TextView) v.findViewById(R.id.link1)).getText().toString();
//String link_asd=link;
// Starting new intent
Intent in = new Intent(getActivity(), SingleMenuItemActivity.class);
in.putExtra(KEY_NAME, name);
in.putExtra(KEY_COST, cost);
in.putExtra(KEY_DESC, description);
if(link==null)
{
}else{
in.putExtra(KEY_LINK, link);
in.putExtra(KEY_LINK1, link1);
}
//in.putExtra("link1", link_asd);
startActivity(in);
Log.e("error",name);
super.onListItemClick(list, v, position, id);
}*/
#Override
public void onResume() {
super.onResume();
//Log.w("Aplikacija_resume","Startovana" );
//new loadListView().execute();
}
public class loadListView extends AsyncTask<Integer, String, String>
{
private final ProgressDialog dialog = new ProgressDialog(getActivity());
#Override protected void onPreExecute()
{
//Toast.makeText(getActivity(), "Ucitavanje...", Toast.LENGTH_LONG).show();
//
this.dialog.setMessage("Molimo da sačekate ...");
super.onPreExecute();
}
#Override protected String doInBackground(Integer... args)
{ // updating UI from Background Thread
menuItems = new ArrayList<HashMap<String, String>>();
final XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_COST, "Datum: " + parser.getValue(e, KEY_COST));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
map.put(KEY_LINK1, parser.getValue(e, KEY_LINK1));
// adding HashList to ArrayList
menuItems.add(map);
}
return null;
}
#Override
protected void onPostExecute(String args)
{
//Toast.makeText(getActivity(), "Ucitano", Toast.LENGTH_LONG).show();
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), menuItems, R.layout.list_row, from, to);
setListAdapter(adapter);
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
}
}
}
Add SingleMenuItemActivity to your manifest :)

The method get(String) is undefined for the type String error in adding the codes to the filter class of adapter in android

For Finding the key items fromt he listview i had not used filter in the adapter, but now by following this link aded the filter but in this line String playerName=songsList.get(i).get("title").toString(); I am getting the error
The method get(String) is undefined for the type String
and also in searchResults.add(songsList.get(i)); as
The method add(HashMap<String,String>) in the type ArrayList<HashMap<String,String>> is not applicable for the arguments (String)
Here's is my entire code
public class Home extends ListActivity {
//how many to load on reaching the bottom
int itemsPerPage = 15;
boolean loadingMore = false;
//For test data :-)
Calendar d = Calendar.getInstance();
ArrayList<String> songsList;
ListView list;
LazyAdapter adapter;
JSONArray posts;
//ArrayList thats going to hold the search results
ArrayList<HashMap<String, String>> searchResults;
LayoutInflater inflater;
// All static variables
static final String URL = "http://india.abc.net/ads/?json=get_recent_posts";
static final String KEY_POSTS = "posts";
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_DATE = "date";
static final String KEY_CONTENT = "content";
static final String KEY_AUTHOR = "author";
static final String KEY_NAME = "name";
static final String KEY_ATTACHMENTS = "attachments";
static final String KEY_SLUG = "slug";
static final String KEY_THUMB_URL = "thumbnail";
static final String KEY_IMAGES = "images";
static final String KEY_URL = "url";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText searchBox=(EditText) findViewById(R.id.search);
final ListView list=(ListView)findViewById(android.R.id.list);
//get the LayoutInflater for inflating the customomView
//this will be used in the custom adapter
inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
final JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(URL);
try {
posts = json.getJSONArray(KEY_POSTS);
// looping through all song nodes <song>
for(int i = 0; i < posts.length(); i++){
JSONObject c = posts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(KEY_ID);
String title = c.getString(KEY_TITLE);
String date = c.getString(KEY_DATE);
String content = c.getString(KEY_CONTENT);
// to remove all <P> </p> and <br /> and replace with ""
content = content.replace("<br />", "");
content = content.replace("<p>", "");
content = content.replace("</p>", "");
//authornumber is agin JSON Object
JSONObject author = c.getJSONObject(KEY_AUTHOR);
String name = author.getString(KEY_NAME);
String url = null;
String slug = null;
try {
JSONArray atta = c.getJSONArray("attachments");
for(int j = 0; j < atta.length(); j++){
JSONObject d = atta.getJSONObject(j);
slug = d.getString(KEY_SLUG);
JSONObject images = d.getJSONObject(KEY_IMAGES);
JSONObject thumbnail = images.getJSONObject(KEY_THUMB_URL);
url = thumbnail.getString(KEY_URL);
}
} catch (Exception e) {
e.printStackTrace();
}
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(KEY_ID, id);
map.put(KEY_TITLE, title);
map.put(KEY_DATE, date);
map.put(KEY_NAME, name);
map.put(KEY_CONTENT, content);
map.put(KEY_SLUG, slug);
map.put(KEY_URL, url);
// adding HashList to ArrayList
songsList.add(map);
}
}catch (JSONException e) {
e.printStackTrace();
}
//searchResults=OriginalValues initially
searchResults=new ArrayList<HashMap<String, String>>(songsList);
// Getting adapter by passing json data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
searchBox.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
// Launching new screen on Selecting Single ListItem
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map = songsList.get(position);
Intent in = new Intent(Home.this, Singlemenuitem.class);
in.putExtra(KEY_TITLE, map.get(KEY_TITLE));
in.putExtra(KEY_DATE, map.get(KEY_DATE));
in.putExtra(KEY_NAME, map.get(KEY_NAME));
in.putExtra(KEY_CONTENT, map.get(KEY_CONTENT));
in.putExtra(KEY_URL, map.get(KEY_URL));
startActivity(in);
}
});
and the adapter class is
public class LazyAdapter extends BaseAdapter implements Filterable{
TextView title;
private Activity activity;
// private TextWatcher textWatcher;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
final EditText searchBox=(EditText) findViewById(R.id.search);
ArrayList<HashMap<String, String>> searchResults;
ArrayList<String> songsList;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
private EditText findViewById(int search) {
// TODO Auto-generated method stub
return null;
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.activity_home, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView date = (TextView)vi.findViewById(R.id.date); // artist name
TextView content = (TextView)vi.findViewById(R.id.content); // duration
TextView name = (TextView)vi.findViewById(R.id.name);
// duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(Home.KEY_TITLE));
date.setText(song.get(Home.KEY_DATE));
content.setText(song.get(Home.KEY_CONTENT));
name.setText(song.get(Home.KEY_NAME));
imageLoader.DisplayImage(song.get(Home.KEY_URL), thumb_image);
return vi;
}
public void add(String string) {
// TODO Auto-generated method stub
}
#Override
public Filter getFilter() {
Filter filter = new Filter() {
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence data,FilterResults searchResults) {
songsList = (ArrayList<String>) searchResults.values; // has the filtered values
notifyDataSetChanged(); // notifies the data with new filtered values
}
#Override
protected FilterResults performFiltering(CharSequence playerName) {
// TODO Auto-generated method stub
return null;
}
};
String searchString=searchBox.getText().toString();
int textLength=searchString.length();
//clear the initial data set
searchResults.clear();
for(int i=0;i<songsList.size();i++)
{
String playerName=songsList.get(i).get("title").toString();
if(textLength<=playerName.length()){
//compare the String in EditText with Names in the ArrayList
if(searchString.equalsIgnoreCase(playerName.substring(0,textLength)))
searchResults.add(songsList.get(i));
}
}
return filter;
}
}
ArrayList<String> songsList;
so songsList is an ArrayList of Strings... if you do songsList.get(i) you'll have a String. The String object has not a "get" method, sou you cannot go further with
songsList.get(i).get("title").toString().
The same thing happens here:
searchResults=new ArrayList<HashMap<String, String>>
so if you call searchResults.add you have to pass a hashmap, not a String.
Please check your type definitions again and it will work.
songsList is a list of strings so you cannot do a get() on its item

Changing ListView selected item

I have a list view, each item is composed of three elements: picture view and 3 text view, the latter contains the name of a product, product price and quantity, i want when I click on the item it correspanding quantity increases by one.
Here's the code:
public class BoissonActivity extends Activity {
ListView maListViewPerso;
BaseDeDonne db = new BaseDeDonne(this);
private static String choix="boisson";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.boisson_layout);
maListViewPerso = (ListView) findViewById(R.id.listviewperso);
Log.d("Lire: ", "Lire tous les produits..");
ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map;
listItem.clear();
List<Produit> produit = db.getSelectProduit(choix);
for (Produit cn : produit) {
map = new HashMap<String, Object>();
map.put("titre",String.valueOf(cn.getNom()));
map.put("description","Prix:"+cn.getPrix_produit());
map.put("quantite", 0);
String url="/sdcard/Image_Produits/"+cn.getImage_produit()+".jpg";
URL pictureURL = null;
try {
pictureURL = new URL(url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeFile(url);
map.put("img", bitmap);
listItem.add(map);
String log = "Id: "+cn.getId()+" ,Nom: " + cn.getNom()+ " ,Image: " + cn.getImage_produit() +
" ,Prix: " + cn.getPrix_produit()+" ,Catégorie: " + cn.getCategorie();
Log.d("produits: ", log);
}
SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.affichageitem,
new String[] {"img", "titre", "description","quantite"}, new int[] {R.id.img, R.id.titre, R.id.description,R.id.quantite});
mSchedule.setViewBinder(new MyViewBinder());
maListViewPerso.setAdapter(mSchedule);
i tried this but only the first textview is changed :
maListViewPerso.setOnItemClickListener(new OnItemClickListener()
{ int compteur=0;
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
TextView t = (TextView)findViewById(R.id.quantite);
t.setText(String.valueOf(compteur));
compteur++;
}
});
Add an onListItemClick..
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Your code here to increment the qty
}
EDIT
Use the View parameter passed in to access the proper view (i'm taking values from the code you added above, so it doesn't exactly match what I show above).
TextView t = (TextView) arg1.findViewById(R.id.quantite);
t.setText(String.valueOf(compteur));

Android Muticolum listview onListItemClick

I have done Muticolum listview onListItemClick .Its working fine.Problem is
This line String selection = l.getItemAtPosition(position).toString(); return this
{routeName=TestRoute2, outlets=5, routeCode=VRT002} .I want to get it selected row's 3rd column value.How to get it.
Please help me..
Thanks in advance..
What kind of Adapter? E.g. for a SimpleCursorAdapter you would do the following:
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
long aLongValue = cursor.getLong(cursor.getColumnIndexOrThrow("anintegercolumn"));
ArrayList<HashMap<String, String>> historyArrayList;
SimpleAdapter histroyListAdapter;
HashMap<String, String> historyObjectMap;
for (CheckInCheckOutHistory checkOutHistoryObj : checkInCheckOutHistoryList) {
historyObjectMap = new HashMap<String, String>();
historyObjectMap.put("assetTag", checkOutHistoryObj.getAssetTag());
historyObjectMap.put("action", checkOutHistoryObj.getAction());
historyObjectMap.put("actionTime", checkOutHistoryObj.getActionDate());
if (checkOutHistoryObj.getAction().equals("Checked out")) {
historyObjectMap.put("gif", R.drawable.radio_button_yellow+ "");
} else {
historyObjectMap.put("gif", R.drawable.radio_button_green+ "");
}
historyArrayList.add(historyObjectMap);
}
histroyListAdapter = new SimpleAdapter(
ViewCheckInCheckOutHistory.this, historyArrayList,
R.layout.multi_colummn_list_text_style_small, new String[] {
"assetTag", "gif" , "action", "actionTime"},
new int[] { R.id.list_content_column1,
R.id.list_content_imagecolumn,
R.id.list_content_column3,
R.id.list_content_column4});
historyListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> historyObjectMapLocal = historyArrayList
.get(position);
final String assetTag = historyObjectMapLocal
.get("assetTag");
System.out.println("assetTag : " + assetTag);
}
});
In the above code the listView contents are populated using an ArrayList historyArrayList
and so the items at any column could be accessed using key ("assetTag", "gif" , "action", "actionTime").

Categories

Resources