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, 苗忠良 <mzl...@gmail.com> 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 <jeru....@gmail.com>
>
> > 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, 苗忠良 <mzl...@gmail.com> 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 Context mCtx;
> > >     private static class DatabaseHelper extends SQLiteOpenHelper {
>
> > >         DatabaseHelper(Context context) {
> > >             super(context, DATABASE_NAME, null, DATABASE_VERSION);
> > >         }
>
> > >         @Override
> > >         public void onCreate(SQLiteDatabase db) {
>
> > >             db.execSQL(DATABASE_CREATE);
> > >         }
>
> > >         @Override
> > >         public void onUpgrade(SQLiteDatabase db, int oldVersion, int
> > > newVersion) {
> > >             Log.w(TAG, "Upgrading database from version " + oldVersion +
> > "
> > > to "
> > >                     + newVersion + ", which will destroy all old data");
> > >             db.execSQL("DROP TABLE IF EXISTS notes");
> > >             onCreate(db);
> > >         }
> > >     }
>
> > >     /**
> > >      * Constructor - takes the context to allow the database to be
> > >      * opened/created
> > >      *
> > >      * @param ctx the Context within which to work
> > >      */
> > >     public helloAdapter(Context ctx) {
> > >         this.mCtx = ctx;
> > >     }
>
> > >     /**
> > >      * Open the notes database. If it cannot be opened, try to create a
> > new
> > >      * instance of the database. If it cannot be created, throw an
> > exception
> > > to
> > >      * signal the failure
> > >      *
> > >      * @return this (self reference, allowing this to be chained in an
> > >      *         initialization call)
> > >      * @throws SQLException if the database could be neither opened or
> > > created
> > >      */
> > >     public helloAdapter open() throws SQLException {
> > >         mDbHelper = new DatabaseHelper(mCtx);
> > >         mDb = mDbHelper.getWritableDatabase();
> > >         return this;
> > >     }
>
> > >     public void close() {
> > >         mDbHelper.close();
> > >     }
>
> > >     /**
> > >      * Create a new note using the title and body provided. If the note
> > is
> > >      * successfully created return the new rowId for that note, otherwise
> > > return
> > >      * a -1 to indicate failure.
> > >      *
> > >      * @param title the title of the note
> > >      * @param body the body of the note
> > >      * @return rowId or -1 if failed
> > >      */
> > >     public long createNote(String word) {
> > >         ContentValues initialValues = new ContentValues();
> > >         initialValues.put(KEY_WORD, word);
>
> > >         return mDb.insert(DATABASE_TABLE, null, initialValues);
> > >     }
>
> > >     /**
> > >      * Delete the note with the given rowId
> > >      *
> > >      * @param rowId id of note to delete
> > >      * @return true if deleted, false otherwise
> > >      */
> > >     public boolean deleteNote(long rowId) {
>
> > >         return mDb.delete(DATABASE_TABLE, KEY_ID + "=" + rowId, null) >
> > 0;
> > >     }
>
> > >     /**
> > >      * Return a Cursor over the list of all notes in the database
> > >      *
> > >      * @return Cursor over all notes
> > >      */
> > >     public Cursor fetchAllNotes() {
>
> > >         return mDb.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_WORD
> > >                 }, null, null, null, null, null);
> > >     }
>
> > >     /**
> > >      * Return a Cursor positioned at the note that matches the given
> > rowId
> > >      *
> > >      * @param rowId id of note to retrieve
> > >      * @return Cursor positioned to matching note, if found
> > >      * @throws SQLException if note could not be found/retrieved
> > >      */
> > >     public Cursor fetchNote(long rowId) throws SQLException {
>
> > >         Cursor mCursor =
>
> > >             mDb.query(true, DATABASE_TABLE, new String[] {KEY_ID,
> > >                     KEY_WORD}, KEY_ID + "=" + rowId, null,
> > >                     null, null, null, null);
> > >         if (mCursor != null) {
> > >             mCursor.moveToFirst();
> > >         }
> > >         return mCursor;
>
> > >     }
>
> > >     /**
> > >      * Update the note using the details provided. The note to be updated
> > is
> > >      * specified using the rowId, and it is altered to use the title and
> > > body
> > >      * values passed in
> > >      *
> > >      * @param rowId id of note to update
> > >      * @param title value to set note title to
> > >      * @param body value to set note body to
> > >      * @return true if the note was successfully updated, false otherwise
> > >      */
> > >     public boolean updateNote(long rowId,String word) {
> > >         ContentValues args = new ContentValues();
> > >         args.put(KEY_WORD, word);
>
> > >         return mDb.update(DATABASE_TABLE, args, KEY_ID + "=" + rowId,
> > null)
>
> > > > 0;
> > >     }
>
> ...
>
> read more >>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to