2017-08-03 33 views
0

需要從列表視圖適配器中提取數據到android studio中的字符串變量。從列表視圖提取數據到android項目中的變量(適配器變量)

目前它發送一切到tableview列我需要它發送到android studio logcat或將它們放在變量中,這樣我就可以在應用程序中使用它。

lvMsg = (ListView) findViewById(R.id.lvMsg); 

    // Create Inbox box URI 
    Uri inboxURI = Uri.parse("content://sms/inbox"); 

    // List required columns 
    String[] reqCols = new String[]{"_id", "address", "body"}; 

    // Get Content Resolver object, which will deal with Content 
    // Provider 
    ContentResolver cr = getContentResolver(); 

    // Fetch Inbox SMS Message from Built-in Content Provider 
    Cursor c = cr.query(inboxURI, reqCols, null, null, null); 

    System.out.println(c); 

    // Attached Cursor with adapter and display in listview 
    adapter = new SimpleCursorAdapter(this, R.layout.row, c, new String[]{"body", "address"}, new int[]{ 
      R.id.lblMsg, R.id.lblNumber 
    }); 

    lvMsg.setAdapter(adapter); 

回答

0

你需要實例化一個光標,並從你的項目SimpleCursorAdapter使用上SimpleCursorAdapter方法的getItem(INT位置)。

光標光標=(光標)adapter.getItem(位置)

一旦你的光標這樣做如下。

boolean cursorMove = cursor.moveToFirst(); 
if (cursorMove) 
{ 
    while(cursorMove) 
    { 
    String data = cursor.getString(cursor.getColumnIndex("data")); 
    // your logic here on data 
    cursorMove = cursor.moveToNext() 
    } 
} 
cursor.close(); 

CursorAdapter

Cursor Docs

相關問題