2010-06-25 172 views
1

這是我用於Listview的代碼,我在某些網站上獲得了參考。它顯示了一個完整的圖像列表。我必須在我的數據庫中放置哪些區域來設置toptext和底部文本的文本?數據庫沒有在ListView中顯示,只有圖片

public class List_View extends ListActivity {

private DBAdapter db;

private TextView toptext; private TextView bottomtext; 私人ListView lv;

公共無效的onCreate(捆綁savedInstanceState) { 嘗試{

super.onCreate(savedInstanceState); 
setContentView(R.layout.list); 
db = new DBAdapter(this); 

toptext = (TextView) findViewById (R.id.toptext); 
bottomtext = (TextView) findViewById (R.id.bottomtext); 

ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); 

HashMap<String, String> item; 
for (int i = 0; i < 31; i++) { 

     item = new HashMap<String, String>(); 
     item.put("Date:", "Saved Date"); 
     item.put("Title:", "Saved Title"); 
     list.add(item); 
} 

SimpleAdapter notes = new SimpleAdapter(this, list, R.layout.view_list, 
         new String[] { "date", "title" }, 
         new int[] {R.id.toptext, R.id.bottomtext }); 
setListAdapter(notes); 

}趕上(Throwable的E){

 Log.e("DBAdapter",e.toString()); 

} }

,這是數據庫的一部分。

public Cursor getAllEntry() 
{ 
    return db.query(DATABASE_TABLE_2, new String[] { 
      KEY_ROWID2, 
      KEY_TITLE, 
      KEY_ENTRY, 
      KEY_MOOD, 
      KEY_DATE, 
      KEY_TIME}, 
      null, 
      null, 
      null, 
      null, 
      null, 
      null); 
} 
+0

你的問題是不清楚的...它會幫助,如果你更好地解釋它... – JaVadid 2010-06-25 06:05:58

+0

噢好吧。我從一些特定的網站跟蹤了這個例子。我想要的列表視圖左側有一個圖像,右側是2個textview。我已經將它們存儲在我的數據庫中了。現在我無法展示出來。只有圖像出來了。 textview沒有出... 所以。我將把我的數據庫部分放入。 – UserA 2010-06-25 06:25:36

回答

1

一些修改,在你的代碼......,你會得到結果

先聲明光標,適配器類在ListActivity類

private NCursorAdapter adapter = null;  
    private Cursor mNotesCursor; 

    mNotesCursor = db.getAllEntry(); 
      startManagingCursor(mNotesCursor); 
if (adapter == null) { 
      adapter = new NCursorAdapter(this, mNotesCursor); 
      setListAdapter(adapter); 
     } else { 
      adapter.changeCursor(mNotesCursor); 
      adapter.notifyDataSetChanged(); 
     } 

您必須在您的應用程序中創建NCursorAdapter類。

public class NCursorAdapter extends CursorAdapter { 

    private Cursor mCursor; 
    private Context mContext; 
    private final LayoutInflater mInflater; 


    public NCursorAdapter(Context context, Cursor c) { 
     super(context, c); 
     // TODO Auto-generated constructor stub 
     mInflater = LayoutInflater.from(context); 
     mContext = context; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     // TODO Auto-generated method stub 
     TextView title = (TextView) view.findViewById(R.id.title); 
     title.setText(cursor.getString(cursor.getColumnIndex("title"))); 

     TextView date = (TextView) view.findViewById(R.id.date); 
     date.setText(cursor.getString(cursor.getColumnIndex("date"))); 

    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     final View view = mInflater.inflate(R.layout.data, parent, false); 

     return view; 
    } 

} 

這裏data.xml文件是你的自定義佈局。其中內容兩個textview .... 「標題」和「日期」你的數據庫表的列名...