2012-11-27 120 views
0

我有一個listView從SQLite數據庫加載數據,現在我想實現一個onclicklistener到列表。當用戶點擊列表時,它應該將它們帶到下一個活動並將相應的數據加載到TextView中。我的問題是,我將如何傳遞列表的數據,例如它是一個「主題」列表,並且用戶點擊主題「我的主頁」。我想將主題「我的家」傳遞給下一個活動,以便我分別知道要檢索哪些相應的數據。Android從SQLite數據庫加載數據到下一個活動

我該怎麼辦?我是否將「提取」放到新的意圖中?或者有另一種方式。下面是我的代碼,它們顯示列表視圖部分:

ListView listContent = (ListView) findViewById(R.id.contentlist); 
     mySQLiteAdapter = new SQLiteAdapter(this); 
     mySQLiteAdapter.openToRead(); 

     Cursor cursor = mySQLiteAdapter.queueAll(); 
     startManagingCursor(cursor); 

     String[] from = new String[] { SQLiteAdapter.KEY_CONTENT }; 
     int[] to = new int[] { R.id.text }; 

     SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.listrow, cursor, from, to); 

     listContent.setAdapter(cursorAdapter); 

     mySQLiteAdapter.close(); 

     //Onclick ListView setlistener 
     listContent.setTextFilterEnabled(true); 
     listContent.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       Intent summaryIntent = new Intent(DocumentListActivity.this, ViewDocumentActivity.class); 
//    summaryIntent.putExtra("SummTopic", value); 
      } 
     }); 

編輯: 這部分是下一個活動。

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0 

請幫助:

Intent i = getIntent(); 
     extraTopic = i.getStringExtra("SummTopic"); 

     mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null); 
     Cursor allrows = mydb.rawQuery("SELECT * FROM "+ TABLE + " WHERE topic = \" " + extraTopic + " \" " , null); 

     Integer cindex = allrows.getColumnIndex("topic"); 
     Integer cindex1 = allrows.getColumnIndex("text1"); 
     Integer cindex2 = allrows.getColumnIndex("text2"); 

我在從數據庫中檢索得到一個錯誤。

謝謝。

回答

1
 public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      Intent summaryIntent = new Intent(DocumentListActivity.this, ViewDocumentActivity.class); 
      Cursor c = (Cursor)parent.getItemAtPosition(position); 
      summaryIntent.putExtra("SummTopic", c.getString(c.getColumnIndex(SQLiteAdapter.KEY_CONTENT))); 
      startActivity(summaryIntent); 
     } 

,或者您可以通過idsummaryIntent.putExtra("SummTopicId", id);)以及「ask db」在下一個活動中爲此主題編號

編輯:

protected void onCreate(Bundle savedInstanceState){ 
    Intent i = getIntent(); 
    String extraTopic = i.getStringExtra("SummTopic"); 
    //or long extraTopic = i.getLongExtra("SummTopic"); if you put id there (which is better) 
     mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null); 
    String[] args = new String[] { extraTopic }; 
    //or String[] args = new String[] { Long.toString(extraTopic) }; with id version 
     Cursor singleRow = mydb.rawQuery("SELECT * FROM "+ TABLE + " WHERE topic=?" , args); 
    //args is better then escaping special chars in query 
    //and it should be single row so we've changed var name :) 
    if(singleRow.moveToFirst()){ //we should do moveToFirst before we can use Cursor 
     Integer cindex = allrows.getColumnIndex("topic"); 
     Integer cindex1 = allrows.getColumnIndex("text1"); 
     Integer cindex2 = allrows.getColumnIndex("text2"); 
     //setup views and stuff .... 
    }else{ 
     Toast.makeText(this, "Oops we did not find topic, detail activity was closed", Toast.LENGTH_SHORT).show(); 
     finish(); 
    } 
} 
+0

嗨,這確實有幫助。謝謝。但在下一次活動檢索時出現錯誤。你能否幫助我在編輯過的部分中引用我的代碼。謝謝 – mellissa

+0

好的,我確實改變了我的回答 – Selvin

+0

這完美的作品。謝謝一堆! – mellissa

0

您可以使用意圖。

Intent intent= new Intent(context,classname.class); 
intent.putExtra("name",string); 

您可以使用intent.getextra()將其獲取到目標課程名稱中。

+0

mellissa你解決了這個問題? – Subburaj

0

您使用後setContentView(...)你需要引用您的字符串,並獲取文本,如...

EditText et = (EditText) findViewById(R.id.my_edit_text); 
String theText = et.getText().toString(); 

將它傳遞給你用一個Intent另一個活動。例如...

Intent i = new Intent(this, MyNewActivity.class); 
i.putExtra("text_label", theText); 
startActivity(i); 

在新的活動(in onCreate()),你得到的意向和檢索字符串...

public class MyNewActivity extends Activity { 

    String uriString; 

    @Override 
    protected void onCreate(...) { 

     ... 

     Intent i = getIntent(); 
     uriString = i.getStringExtra("text_label"); 

    } 
} 

編輯:

得到字符串列表視圖從您可以應用以下代碼並獲取ITEM字符串並將其傳遞給下一個活動:

listContent.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
    String ITEM=listContent.getItemAtPosition(position); 
       //Intent summaryIntent = new Intent(DocumentListActivity.this, // ViewDocumentActivity.class); 
//    summaryIntent.putExtra("SummTopic", value); 
      } 
     }); 
+0

如何從ListView中獲取文本?也許我沒有正確強調。我想獲得屬於ListView的「主題」。 – mellissa

+0

已編輯我的答案。經歷它。 –

-1

我想你正在使用一個適配器來填充你的列表中的一些數據。所以,你需要重寫的getItem(INT位置)方法在你的適配器:

@Override 
    public Object getItem(int position) { 
     //Note that return type should be the same you you use to initilise items oof the adapter. E.g. String or some custom Topic class 
     Object someObject = mObjects[position]; 

     return someObject;  
    } 

然後,你需要設置的項目點擊收聽到你列出

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
       Object someObject = adapterView.getItemAtPosition(i); 

       Intent i = new Intent(this, MyNewActivity.class); 
       i.putExtra("text_label", someObject.toString()); 
       startActivity(i); 
      } 
     }); 
+0

他正在使用CursorAdapter ...有沒有必要重寫getItem ... – Selvin

相關問題