2012-04-01 58 views
0

我開發了一些短信應用程序,我無法弄清楚如何解決這個問題......在一個佈局中,我顯示兩個文本查看來顯示數字和日期的短信...工作正常...從另一個類的數據填充類的textview

但現在我正在實施onListItemClick,我希望當單擊列表項時,從數據庫的整個消息應加載到新的視圖....如何我完成了...

protected void onListItemClick(ListView l, View v, int position, long id) { 
     // TODO Auto-generated method stub 
     super.onListItemClick(l, v, position, id); 


     dba.open(); 

     Cursor c=dba.executeQuery("select rowid _id,* from messages where rowid="+position+";"); 
     Log.v(tag, "value count"+ c.getCount()); 
     if(c.getCount()>0) 
     { 
      Log.v(tag, "value count"+c.getCount()); 
      c.moveToPosition(position); 
       Intent intent = new Intent(); 

       String mesage = c.getString(c.getColumnIndex("msg")); 

       Log.v("Value passed to class", mesage); 
       intent.putExtra(name, mesage); 

      intent.setClass(this, NewInterface.class); 
      startActivity(intent); 


     } 

    } 

這裏是我傳遞數據的其他類的代碼。

protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.messages); 
     tv4 = (TextView) findViewById(R.id.tvMessages); 
     Bundle bundle=getIntent().getExtras(); 
     if(bundle!=null) 
     { 
      Log.v("Value got from class", bundle.getString("name")); 
      tv4.setText(bundle.getString("name")); 
     } 

列表視圖通過從包含電話號碼數據庫,這種方法填充....

私人無效openAndQueryDatabase(){

newDB=new DBAdapter(this); 

    try { 

     newDB.open(); 
     Cursor c = newDB.executeQuery("select distinct nm,rowid _id from messages"); 

     if (c != null) { 

      if (c.moveToFirst()) 
       Log.v(tag, "Stage 1");{ 
       do { 

        String firstName = c.getString(c.getColumnIndex("nm")); 
        Log.v(tag, firstName); 
       // String date = c.getString(c.getColumnIndex("dt")); 

        results.add(firstName); 


       }while (c.moveToNext()); 
       Log.v(tag, "Stage 1"); 
      } 
     }   
+0

您面臨的問題是什麼 – Ishu 2012-04-01 06:29:02

+0

我希望當用戶點擊某個listitem時,它會獲取顯示在特定listviewitem上的電話號碼,然後對該號碼執行數據庫查詢並獲取消息並傳遞該數據到另一個類顯示它在不同的佈局... – kashifmehmood 2012-04-01 11:00:11

+0

你可以添加你的getview()或什麼listview項目包含我的意思是數據 – Ishu 2012-04-01 11:04:35

回答

1

列表項的點擊您要打開一個數據庫來獲取基於點擊位置的數據。爲什麼不使用Intent將int位置傳遞給下一個Activity,然後在那裏進行數據庫查詢,而不是嘗試讀取第一個Activity的整個datain。這會更容易,更有意義。如果你仍然想通過wholde數據,使用類似如下所示的代碼(更換您的if(c.getCount>0)塊):

ArrayList<String> arrayList ; 

      Cursor c=null; 
      if(c.moveToFirst()){ 
       int x= c.getCount(); 
       int y =c.getColumnIndex("msg"); 
      arrayList = new ArrayList<String>(x); 
       for(int a=0;a<x;a++){ 
c.moveToPosition(a);      
arrayList.add(c.getString(y)); 

       } 
        Intent intent = new Intent(this,NewInterface.class); 
       intent.putStringArrayListExtra("msgs",arrayList); 
      startActivity(intent); 
      } 

從ListView控件你點擊行的textViews的一送人數:

TextView tv1=(TextView)v.findViewbyId(R.id.number)//Replace this with id of tv displaying numbers 
String number = tv1.getText().toString(); 

將這些行添加到onListItemClick中,然後在查詢中使用此數字。

+0

但問題是,當我得到的位置變量它返回一個int ...而我需要顯示在列表中的電話號碼...然後根據該電話號碼執行數據庫查詢....希望你能幫我.. – kashifmehmood 2012-04-01 10:56:47

+0

listview顯示的數字不是textview ....它不是一個textview這被點擊它的listviewitem被點擊,並在該listviewclick的基礎上我想用數據庫中的數據填充textview ... – kashifmehmood 2012-04-02 06:26:48

+0

是否添加了上述兩行? – Akhil 2012-04-02 06:28:04