从适配器onListItemClick获取值

时间:2013-09-12 15:58:29

标签: android listview android-listview adapter android-arrayadapter

从我的主要活动我将字符串插入数据库并使用游标在数组适配器中添加几个值,我的主要类是:

 public class main{
        message = datasource.createMessage(messages,ch,random);
         ArrayAdapter<Message> adapter = (ArrayAdapter<Message>) getListAdapter();
             adapter.add(message);
            public void onListItemClick(ListView l, View v, int position, long id){
                      super.onListItemClick(l, v, position, id);

                      Toast.makeText(getActivity(),
                              "position: "+position+" "+"Id: "+id+getListAdapter().getItem(position), Toast.LENGTH_LONG).show();            

                  String str=getListView().getItemAtPosition(position).toString();

              }
}

当我获取列表中的所有消息时,我在适配器ID,消息和threadid中有三个值,我想获取threadid onListItemClick的值。如何使用onListItemClick方法中的适配器获取值?请帮助和我的数据源类,我查询数据库并使用游标获取对象并添加到适配器..

 public class datasource{
      private String[] allColumns = { MySQLiteHelper.COLUMN_ID,MySQLiteHelper.COLUMN_MESSAGE,MySQLiteHelper.COLUMN_THREADID };

                public Message createMessage(String message,char ch,int random) {
                    ContentValues values = new ContentValues();

                if(ch=='s')
                values.put(MySQLiteHelper.COLUMN_STATUS, "s");
            if(ch=='r')
                values.put(MySQLiteHelper.COLUMN_STATUS, "r");

                values.put(MySQLiteHelper.COLUMN_PARENTID, 0);

                values.put(MySQLiteHelper.COLUMN_THREADID, random);

            values.put(MySQLiteHelper.COLUMN_MESSAGE, message);
            long insertId = database.insert(MySQLiteHelper.TABLE_NAME, null,
                values);


           Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME,
                allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
                null, null, null);
           /* Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME,
                    allColumns, null, null,
                    null, null, null);*/
           cursor.moveToFirst();
            Message newMessage = cursorToMessage(cursor);
           cursor.close();
            return newMessage;
          }
         private Message cursorToMessage(Cursor cursor) {
            Message message = new Message();
            message.setId(cursor.getLong(cursor.getColumnIndex("id")));
           // message.setparentid(cursor.getLong(1));
            message.setmessage(cursor.getString(cursor.getColumnIndex("message")));
            message.setthreadid(cursor.getLong(cursor.getColumnIndex("threadid")));
         //  message.status(cursor.getString(4)); 
            return message;

          }
}

0 个答案:

没有答案