Click Listener on ListView?

This should allow you to interact with selected item the way you want MyListAdapter. Java package com.example. Simplelist; import java.util.

ArrayList; import java.util. HashMap; import java.util. List; import java.util.

Map; import java.util.regex. Matcher; import java.util.regex. Pattern; import android.app.

ListActivity; import android.content. ContentResolver; import android.content. Context; import android.database.

Cursor; import android.os. Bundle; import android.provider.Contacts. Groups; import android.view.

LayoutInflater; import android.view. View; import android.view. ViewGroup; import android.widget.

BaseAdapter; import android.widget. ListView; import android.widget. SimpleAdapter; import android.widget.

TextView; import android.widget. Toast; public class MyListAdapter extends ListActivity { List groups = new ArrayList(); @Override protected void onCreate(Bundle savedInstanceState){ super. OnCreate(savedInstanceState); setContentView(R.layout.

Main); this. Groups = getContacts(); ContactGroupAdapter cAdapter = new ContactGroupAdapter(this); setListAdapter(cAdapter); } private List getContacts(){ List grps = new ArrayList(); ContentResolver cr = getContentResolver(); Cursor groupCur = cr. Query(Groups.

CONTENT_URI, null, null, null, null); if (groupCur.getCount() > 0) { while (groupCur.moveToNext()) { ContactGroup newGroup = new ContactGroup(); newGroup. Id = groupCur. GetString(groupCur.

GetColumnIndex(Groups. _ID)); newGroup. Name = groupCur.

GetString(groupCur. GetColumnIndex(Groups. NAME)); grps.

Add(newGroup); } } return grps; } @Override protected void onListItemClick(ListView l, View v, int position, long id) { ContactGroup selectedGroup = new ContactGroup(); selectedGroup = groups. Get(position); Toast. MakeText(getBaseContext(), selectedGroup.Name + " ID #" + selectedGroup.

Id, 1).show(); } public class ContactGroupAdapter extends BaseAdapter{ public ContactGroupAdapter(Context c) { mContext = c; } @Override public int getCount() { return groups.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView == null){ LayoutInflater vi = LayoutInflater. From(this. MContext); convertView = vi.

Inflate(R.layout. Two_line_list_item, null); holder = new ViewHolder(); convertView. SetTag(holder); } else { //Get view holder back holder = (ViewHolder) convertView.getTag(); } ContactGroup c = groups.

Get(position); if (c! = null) { //Name holder. Toptext = (TextView) convertView.

FindViewById(R.id. Text1); holder.toptext. SetText(c.

Name); //ID holder. Bottomtext = (TextView) convertView. FindViewById(R.id.

Text2); holder.bottomtext. SetText(c.Id); } return convertView; } private Context mContext; } public static class ViewHolder { TextView toptext; TextView bottomtext; } public class ContactGroup{ public String Id; public String Name; } } two_line_list_item. Xml?

Xml version="1.0" encoding="utf-8"?

This should allow you to interact with selected item the way you want. MyListAdapter. Java package com.example.

Simplelist; import java.util. ArrayList; import java.util. HashMap; import java.util.

List; import java.util. Map; import java.util.regex. Matcher; import java.util.regex.

Pattern; import android.app. ListActivity; import android.content. ContentResolver; import android.content.

Context; import android.database. Cursor; import android.os. Bundle; import android.provider.Contacts.

Groups; import android.view. LayoutInflater; import android.view. View; import android.view.

ViewGroup; import android.widget. BaseAdapter; import android.widget. ListView; import android.widget.

SimpleAdapter; import android.widget. TextView; import android.widget. Toast; public class MyListAdapter extends ListActivity { List groups = new ArrayList(); @Override protected void onCreate(Bundle savedInstanceState){ super.

OnCreate(savedInstanceState); setContentView(R.layout. Main); this. Groups = getContacts(); ContactGroupAdapter cAdapter = new ContactGroupAdapter(this); setListAdapter(cAdapter); } private List getContacts(){ List grps = new ArrayList(); ContentResolver cr = getContentResolver(); Cursor groupCur = cr.

Query(Groups. CONTENT_URI, null, null, null, null); if (groupCur.getCount() > 0) { while (groupCur.moveToNext()) { ContactGroup newGroup = new ContactGroup(); newGroup. Id = groupCur.

GetString(groupCur. GetColumnIndex(Groups. _ID)); newGroup.Name = groupCur.

GetString(groupCur. GetColumnIndex(Groups.NAME)); grps. Add(newGroup); } } return grps; } @Override protected void onListItemClick(ListView l, View v, int position, long id) { ContactGroup selectedGroup = new ContactGroup(); selectedGroup = groups.

Get(position); Toast. MakeText(getBaseContext(), selectedGroup. Name + " ID #" + selectedGroup.

Id, 1).show(); } public class ContactGroupAdapter extends BaseAdapter{ public ContactGroupAdapter(Context c) { mContext = c; } @Override public int getCount() { return groups.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView == null){ LayoutInflater vi = LayoutInflater. From(this. MContext); convertView = vi.

Inflate(R.layout. Two_line_list_item, null); holder = new ViewHolder(); convertView. SetTag(holder); } else { //Get view holder back holder = (ViewHolder) convertView.getTag(); } ContactGroup c = groups.

Get(position); if (c! = null) { //Name holder. Toptext = (TextView) convertView.

FindViewById(R.id. Text1); holder.toptext. SetText(c.

Name); //ID holder. Bottomtext = (TextView) convertView. FindViewById(R.id.

Text2); holder.bottomtext. SetText(c. Id); } return convertView; } private Context mContext; } public static class ViewHolder { TextView toptext; TextView bottomtext; } public class ContactGroup{ public String Id; public String Name; } } two_line_list_item.xml.

Since you are using ListActivity you need to override onListItemClick(). Protected void onListItemClick(ListView l, View v, int position, long id) { // position is the position in the adapter and id is what adapter.getItemId() returns. // use one of them to get the group id from the data.

}.

No matter where I click that returns android.content. ContentResolver$CursorWrapperInner@45a04508. I made Toast ;) – Bill Mote Mar 2 at 17:20 +1 because that's 1/2 the answer.

Because I bound the data to a cursor no matter where I clicked I was getting the cursor rather than the item. If I generate the list by iterating through it and adding them with an adapter then your answer works perfectly. – Bill Mote Mar 4 at 11:55 1 @Bill Mote since you are using a cursor I assume that you'd have to do something like mCursor.

MoveToPosition(position), etc. Glad you got it working! – Robby Pond Mar 4 at 13:13.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions