0

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

 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;

          }
}
4

0 回答 0