开发者

How to get an ID from database into the ListView

I learn work with Android. I started to create a project with Contacts. I insert contacts into the SQLite database and then I put them from the database and show them in ListView. Thats OK. But I would like to click on an item in the List and then to edit the item. Therefore I need to get somehow the ID of the item and work with it...I do not know if it is clear...As you can see, in the function onItemClick() , there is running a new activity ...and in the new activity, I need the ID of the item in order to work with it.

public class ContactList extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); 
setContentView(R.layout.seznam_kontaktu2);
SQLInsert info = new SQLInsert(this);
info.open();
ArrayList<String> data = info.getDataArrayList();  //It returns Array of "开发者_StackOverflow社区Lastname, Name" which is shown in the List
info.close();


ListView lv1 = (ListView) findViewById(R.id.ListView01);
lv1.setAdapter (new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data));
lv1.setOnItemClickListener(new OnItemClickListener() {
         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            startActivity(new Intent("net.johnyho.cz.EDITOVATKONTAKT"));    
        }
    });
}


One option that has worked for me is to create a custom ArrayAdapter class along with a custom AdapterItem class. We store the id on our AdapterItem, and return it using our custom ArrayAdapter.

MyAdapterItem might look something like ...

public class MyAdapterItem implements Comparable
{
   public String name;
   public long rowId;

   public MyAdapterItem()
   {
      rowId = -1L;
      name = "";
   }

   public MyAdapterItem(long _rowId, String _name)
   {
      rowId = _rowId;
      name = _name;
   }

   public int compareTo( Object o )
   {
      return toString().compareTo(o.toString());
   }

   public String toString()
   {
      return name;
   }
}

Our custom ArrayAdapter, MyAdapter, then overrides getItemId(). It reads and returns the stored row id (making the assumption it is working with elements of MyAdapterItem). The other methods on ArrayAdapter will continue to work with our custom class - the toString method is used by the list for display purposes, and by providing a compareTo method the list knows how to sort the values.

public class MyAdapter<MyAdapterItem> extends ArrayAdapter
{
   public MyAdapter(Context context, int resourceId)
   {
      super(context, resourceId);
   }

   @Override
   public long getItemId( int position )
   {
      MyAdapterItem item = (MyAdapterItem)this.getItem(position);
      return item.rowId;
   }
}

Back in your Activity's onCreate the OnItemClickListener now provides the correct row id in the "id" parameter.

What is not shown is the new implementation of info.getDataArrayList() - it must be modified to create new MyAdapterItem objects instead of simple Strings.

ArrayList<MyAdapterItem> data = info.getDataArrayList(); 

ListView lv1 = (ListView) findViewById(R.id.ListView01);
lv1.setAdapter( new MyAdapter<MyAdapterItem>(this, android.R.layout.simple_list_item_1, data) );

lv1.setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> arg0, View view, int position, long id) 
   {
      // do whatever - the value in "id" is supplied by getRowId on our adapter
   }
});

Also, if you attach a context menu to your ListView, the MenuItem objects passed into your Activity's onContextItemSelected method will also return the correct row id.

@Override
public boolean onContextItemSelected(MenuItem item) 
{
   AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
   long rowId = info.id;
   // do something with the id
}


public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {

        startActivity(new Intent("net.johnyho.cz.EDITOVATKONTAKT"));    
    }

hi i dont know what are the column names of your application.but for example id is your primary key identifier then move cursor to arg2 postion and get the value of _id and then pass it to startActivity and it will work.first thing apply SQLiteOpenHelper class its easy for use then apply this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜