Re: [android-developers] Re: how to deal with button clickevent in a listview?
hi: I‘m reslove this problem, the code is right always, but the log cheat me. 2010/7/14 Jeruliu > Well, that will be similar with handling the checkbox in the item. > > I handled it in my customized adapter with a dirty approach. > > On Jul 12, 4:55 pm, 苗忠良 wrote: > > hi,Jeruliu: > > I need not that how to handle the item, in my code, I wrote it , I > want > > to know is that how to handle the button in the item. > > > > but still thank you! > > > > 2010/7/12 Jeruliu > > > > > Please find my working code snippet below: > > > > > // get a reference to the ListView > > > listView = (ListView) findViewById(android.R.id.list); > > > ... > > > // single click event > > > listView.setOnItemClickListener(new OnItemClickListener() { > > > public void onItemClick(AdapterView arg0, View arg1, int > arg2, > > > long arg3) { > > > > > My list view works with the customized adapter. > > > > > On Jul 11, 9:18 pm, 苗忠良 wrote: > > > > hi,all: > > > > > > I write a listactivty,the item including a textview and a > button,the > > > data > > > > came from a sqlite。but in handling the clickevent , i meet some > problem, > > > in > > > > the CursorAdapter's bindView() function,i cann't get the current > > > position。in > > > > log,i find position of Cursor is the position I last click the item > of > > > > listview. > > > > the code as follows: > > > > package miaozl.hello; > > > > > > import android.app.ListActivity; > > > > import android.content.Context; > > > > import android.database.Cursor; > > > > import android.os.Bundle; > > > > import android.util.Log; > > > > import android.view.LayoutInflater; > > > > import android.view.View; > > > > import android.view.ViewGroup; > > > > import android.view.View.OnClickListener; > > > > import android.widget.AdapterView; > > > > import android.widget.Button; > > > > import android.widget.CursorAdapter; > > > > import android.widget.ImageButton; > > > > import android.widget.ListView; > > > > import android.widget.SimpleCursorAdapter; > > > > import android.widget.TextView; > > > > > > public class helloActivity extends ListActivity{ > > > > private helloAdapter mDbHelper; > > > > private final static String TAG ="helloActivity"; > > > > private static LayoutInflater mFactory; > > > > ListView lv; > > > > /** Called when the activity is first created. */ > > > > @Override > > > > public void onCreate(Bundle savedInstanceState) { > > > > super.onCreate(savedInstanceState); > > > > mDbHelper = new helloAdapter(this); > > > > mDbHelper.open(); > > > > mFactory = LayoutInflater.from(this); > > > > lv= getListView(); > > > > lv.setChoiceMode(1); > > > > lv.setOnItemClickListener(new > AdapterView.OnItemClickListener(){ > > > > > > @Override > > > > public void onItemClick(AdapterView arg0, View arg1, > int > > > > arg2, > > > > long arg3) { > > > > // TODO Auto-generated method stub > > > > Log.d(TAG,"onItemClick===arg3 is ==="+arg3); > > > > } > > > > > > }); > > > > fillData(); > > > > } > > > > private void fillData() { > > > > // TODO Auto-generated method stub > > > > Cursor c = mDbHelper.fetchAllNotes(); > > > > startManagingCursor(c); > > > > > > String[] from = new String[] { > > > > helloAdapter.KEY_WORD,helloAdapter.KEY_ID}; > > > > int[] to = new int[] { R.id.word,R.id.button}; > > > > lv.setAdapter(new ListAdapter(this, c)); > > > > } > > > > private static class ListAdapter extends CursorAdapter { > > > > > > public ListAdapter(Context context, Cursor c) { > > > > super(context, c); > > > > // TODO Auto-generated constructor stub > > > > } > > > > > > @Override > > > > public void bindView(View view, Context context, final Cursor > > > > cursor) { > > > > // TODO Auto-generated method stub > > > > Button onButton=(Button)view.findViewById(R.id.button); > > > > onButton.setOnClickListener(new OnClickListener(){ > > > > > > @Override > > > > public void onClick(View arg0) { > > > > // TODO Auto-generated method stub > > > > Log.d(TAG, > > > > "=cursor.getPosition()="+cursor.getPosition()); > > > > } > > > > }); > > > > > > } > > > > > > @Override > > > > public View newView(Context context, Cursor cursor, ViewGroup > > > > parent) { > > > > // TODO Auto-generated method stub > > > > View ret= > mFactory.inflate(R.layout.list_item,parent,false); > > > > TextView word=(TextView)ret.findViewById(R.id.word); > > > > Button onButton=(Button)ret.findViewById(R.id.button); > > > > onButton.setFocusable(false); > > > > > > word.setText
[android-developers] Re: how to deal with button clickevent in a listview?
Well, that will be similar with handling the checkbox in the item. I handled it in my customized adapter with a dirty approach. On Jul 12, 4:55 pm, 苗忠良 wrote: > hi,Jeruliu: > I need not that how to handle the item, in my code, I wrote it , I want > to know is that how to handle the button in the item. > > but still thank you! > > 2010/7/12 Jeruliu > > > Please find my working code snippet below: > > > // get a reference to the ListView > > listView = (ListView) findViewById(android.R.id.list); > > ... > > // single click event > > listView.setOnItemClickListener(new OnItemClickListener() { > > public void onItemClick(AdapterView arg0, View arg1, int arg2, > > long arg3) { > > > My list view works with the customized adapter. > > > On Jul 11, 9:18 pm, 苗忠良 wrote: > > > hi,all: > > > > I write a listactivty,the item including a textview and a button,the > > data > > > came from a sqlite。but in handling the clickevent , i meet some problem, > > in > > > the CursorAdapter's bindView() function,i cann't get the current > > position。in > > > log,i find position of Cursor is the position I last click the item of > > > listview. > > > the code as follows: > > > package miaozl.hello; > > > > import android.app.ListActivity; > > > import android.content.Context; > > > import android.database.Cursor; > > > import android.os.Bundle; > > > import android.util.Log; > > > import android.view.LayoutInflater; > > > import android.view.View; > > > import android.view.ViewGroup; > > > import android.view.View.OnClickListener; > > > import android.widget.AdapterView; > > > import android.widget.Button; > > > import android.widget.CursorAdapter; > > > import android.widget.ImageButton; > > > import android.widget.ListView; > > > import android.widget.SimpleCursorAdapter; > > > import android.widget.TextView; > > > > public class helloActivity extends ListActivity{ > > > private helloAdapter mDbHelper; > > > private final static String TAG ="helloActivity"; > > > private static LayoutInflater mFactory; > > > ListView lv; > > > /** Called when the activity is first created. */ > > > @Override > > > public void onCreate(Bundle savedInstanceState) { > > > super.onCreate(savedInstanceState); > > > mDbHelper = new helloAdapter(this); > > > mDbHelper.open(); > > > mFactory = LayoutInflater.from(this); > > > lv= getListView(); > > > lv.setChoiceMode(1); > > > lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){ > > > > @Override > > > public void onItemClick(AdapterView arg0, View arg1, int > > > arg2, > > > long arg3) { > > > // TODO Auto-generated method stub > > > Log.d(TAG,"onItemClick===arg3 is ==="+arg3); > > > } > > > > }); > > > fillData(); > > > } > > > private void fillData() { > > > // TODO Auto-generated method stub > > > Cursor c = mDbHelper.fetchAllNotes(); > > > startManagingCursor(c); > > > > String[] from = new String[] { > > > helloAdapter.KEY_WORD,helloAdapter.KEY_ID}; > > > int[] to = new int[] { R.id.word,R.id.button}; > > > lv.setAdapter(new ListAdapter(this, c)); > > > } > > > private static class ListAdapter extends CursorAdapter { > > > > public ListAdapter(Context context, Cursor c) { > > > super(context, c); > > > // TODO Auto-generated constructor stub > > > } > > > > @Override > > > public void bindView(View view, Context context, final Cursor > > > cursor) { > > > // TODO Auto-generated method stub > > > Button onButton=(Button)view.findViewById(R.id.button); > > > onButton.setOnClickListener(new OnClickListener(){ > > > > @Override > > > public void onClick(View arg0) { > > > // TODO Auto-generated method stub > > > Log.d(TAG, > > > "=cursor.getPosition()="+cursor.getPosition()); > > > } > > > }); > > > > } > > > > @Override > > > public View newView(Context context, Cursor cursor, ViewGroup > > > parent) { > > > // TODO Auto-generated method stub > > > View ret= mFactory.inflate(R.layout.list_item,parent,false); > > > TextView word=(TextView)ret.findViewById(R.id.word); > > > Button onButton=(Button)ret.findViewById(R.id.button); > > > onButton.setFocusable(false); > > > word.setText(cursor.getString(cursor.getColumnIndex(helloAdapter.KEY_WORD))); > > > onButton.setBackgroundResource(R.drawable.icon); > > > return ret; > > > } > > > > } > > > > } > > > > the ListAdapter as fallows: > > > > package miaozl.hello; > > > > import android.content.ContentValues; > > > import android.content.Context; > > > import andro
Re: [android-developers] Re: how to deal with button clickevent in a listview?
hi,Jeruliu: I need not that how to handle the item, in my code, I wrote it , I want to know is that how to handle the button in the item. but still thank you! 2010/7/12 Jeruliu > Please find my working code snippet below: > > // get a reference to the ListView > listView = (ListView) findViewById(android.R.id.list); > ... > // single click event > listView.setOnItemClickListener(new OnItemClickListener() { > public void onItemClick(AdapterView arg0, View arg1, int arg2, > long arg3) { > > My list view works with the customized adapter. > > On Jul 11, 9:18 pm, 苗忠良 wrote: > > hi,all: > > > > I write a listactivty,the item including a textview and a button,the > data > > came from a sqlite。but in handling the clickevent , i meet some problem, > in > > the CursorAdapter's bindView() function,i cann't get the current > position。in > > log,i find position of Cursor is the position I last click the item of > > listview. > > the code as follows: > > package miaozl.hello; > > > > import android.app.ListActivity; > > import android.content.Context; > > import android.database.Cursor; > > import android.os.Bundle; > > import android.util.Log; > > import android.view.LayoutInflater; > > import android.view.View; > > import android.view.ViewGroup; > > import android.view.View.OnClickListener; > > import android.widget.AdapterView; > > import android.widget.Button; > > import android.widget.CursorAdapter; > > import android.widget.ImageButton; > > import android.widget.ListView; > > import android.widget.SimpleCursorAdapter; > > import android.widget.TextView; > > > > public class helloActivity extends ListActivity{ > > private helloAdapter mDbHelper; > > private final static String TAG ="helloActivity"; > > private static LayoutInflater mFactory; > > ListView lv; > > /** Called when the activity is first created. */ > > @Override > > public void onCreate(Bundle savedInstanceState) { > > super.onCreate(savedInstanceState); > > mDbHelper = new helloAdapter(this); > > mDbHelper.open(); > > mFactory = LayoutInflater.from(this); > > lv= getListView(); > > lv.setChoiceMode(1); > > lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){ > > > > @Override > > public void onItemClick(AdapterView arg0, View arg1, int > > arg2, > > long arg3) { > > // TODO Auto-generated method stub > > Log.d(TAG,"onItemClick===arg3 is ==="+arg3); > > } > > > > }); > > fillData(); > > } > > private void fillData() { > > // TODO Auto-generated method stub > > Cursor c = mDbHelper.fetchAllNotes(); > > startManagingCursor(c); > > > > String[] from = new String[] { > > helloAdapter.KEY_WORD,helloAdapter.KEY_ID}; > > int[] to = new int[] { R.id.word,R.id.button}; > > lv.setAdapter(new ListAdapter(this, c)); > > } > > private static class ListAdapter extends CursorAdapter { > > > > public ListAdapter(Context context, Cursor c) { > > super(context, c); > > // TODO Auto-generated constructor stub > > } > > > > @Override > > public void bindView(View view, Context context, final Cursor > > cursor) { > > // TODO Auto-generated method stub > > Button onButton=(Button)view.findViewById(R.id.button); > > onButton.setOnClickListener(new OnClickListener(){ > > > > @Override > > public void onClick(View arg0) { > > // TODO Auto-generated method stub > > Log.d(TAG, > > "=cursor.getPosition()="+cursor.getPosition()); > > } > > }); > > > > } > > > > @Override > > public View newView(Context context, Cursor cursor, ViewGroup > > parent) { > > // TODO Auto-generated method stub > > View ret= mFactory.inflate(R.layout.list_item,parent,false); > > TextView word=(TextView)ret.findViewById(R.id.word); > > Button onButton=(Button)ret.findViewById(R.id.button); > > onButton.setFocusable(false); > > > > > word.setText(cursor.getString(cursor.getColumnIndex(helloAdapter.KEY_WORD))); > > onButton.setBackgroundResource(R.drawable.icon); > > return ret; > > } > > > > } > > > > } > > > > the ListAdapter as fallows: > > > > package miaozl.hello; > > > > import android.content.ContentValues; > > import android.content.Context; > > import android.database.Cursor; > > import android.database.SQLException; > > import android.database.sqlite.SQLiteDatabase; > > import android.database.sqlite.SQLiteOpenHelper; > > import android.util.Log; > > > > public class helloAdapter{ > > public static final String KEY_WORD = "word"; > > public static final String KEY_ID = "_id"; > > private stat
[android-developers] Re: how to deal with button clickevent in a listview?
Please find my working code snippet below: // get a reference to the ListView listView = (ListView) findViewById(android.R.id.list); ... // single click event listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { My list view works with the customized adapter. On Jul 11, 9:18 pm, 苗忠良 wrote: > hi,all: > > I write a listactivty,the item including a textview and a button,the data > came from a sqlite。but in handling the clickevent , i meet some problem, in > the CursorAdapter's bindView() function,i cann't get the current position。in > log,i find position of Cursor is the position I last click the item of > listview. > the code as follows: > package miaozl.hello; > > import android.app.ListActivity; > import android.content.Context; > import android.database.Cursor; > import android.os.Bundle; > import android.util.Log; > import android.view.LayoutInflater; > import android.view.View; > import android.view.ViewGroup; > import android.view.View.OnClickListener; > import android.widget.AdapterView; > import android.widget.Button; > import android.widget.CursorAdapter; > import android.widget.ImageButton; > import android.widget.ListView; > import android.widget.SimpleCursorAdapter; > import android.widget.TextView; > > public class helloActivity extends ListActivity{ > private helloAdapter mDbHelper; > private final static String TAG ="helloActivity"; > private static LayoutInflater mFactory; > ListView lv; > /** Called when the activity is first created. */ > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > mDbHelper = new helloAdapter(this); > mDbHelper.open(); > mFactory = LayoutInflater.from(this); > lv= getListView(); > lv.setChoiceMode(1); > lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){ > > @Override > public void onItemClick(AdapterView arg0, View arg1, int > arg2, > long arg3) { > // TODO Auto-generated method stub > Log.d(TAG,"onItemClick===arg3 is ==="+arg3); > } > > }); > fillData(); > } > private void fillData() { > // TODO Auto-generated method stub > Cursor c = mDbHelper.fetchAllNotes(); > startManagingCursor(c); > > String[] from = new String[] { > helloAdapter.KEY_WORD,helloAdapter.KEY_ID}; > int[] to = new int[] { R.id.word,R.id.button}; > lv.setAdapter(new ListAdapter(this, c)); > } > private static class ListAdapter extends CursorAdapter { > > public ListAdapter(Context context, Cursor c) { > super(context, c); > // TODO Auto-generated constructor stub > } > > @Override > public void bindView(View view, Context context, final Cursor > cursor) { > // TODO Auto-generated method stub > Button onButton=(Button)view.findViewById(R.id.button); > onButton.setOnClickListener(new OnClickListener(){ > > @Override > public void onClick(View arg0) { > // TODO Auto-generated method stub > Log.d(TAG, > "=cursor.getPosition()="+cursor.getPosition()); > } > }); > > } > > @Override > public View newView(Context context, Cursor cursor, ViewGroup > parent) { > // TODO Auto-generated method stub > View ret= mFactory.inflate(R.layout.list_item,parent,false); > TextView word=(TextView)ret.findViewById(R.id.word); > Button onButton=(Button)ret.findViewById(R.id.button); > onButton.setFocusable(false); > > word.setText(cursor.getString(cursor.getColumnIndex(helloAdapter.KEY_WORD))); > onButton.setBackgroundResource(R.drawable.icon); > return ret; > } > > } > > } > > the ListAdapter as fallows: > > package miaozl.hello; > > import android.content.ContentValues; > import android.content.Context; > import android.database.Cursor; > import android.database.SQLException; > import android.database.sqlite.SQLiteDatabase; > import android.database.sqlite.SQLiteOpenHelper; > import android.util.Log; > > public class helloAdapter{ > public static final String KEY_WORD = "word"; > public static final String KEY_ID = "_id"; > private static final String TAG = "helloAdapter"; > private DatabaseHelper mDbHelper; > private SQLiteDatabase mDb; > > private static final String DATABASE_CREATE = > "create table notes (_id integer primary key autoincrement, " > + "word text not null);"; > private static final String DATABASE_NAME = "list"; > private static final String DATABASE_TABLE = "notes"; > private static final int DATABASE_VERSION = 1; > private final