首页 文章

数据库未在ListView中显示,仅显示图像

提问于
浏览
1

这是我用于Listview的代码,我参考了一些网站 . 它显示了整个图像列表 . 我必须在数据库中放置哪个区域来设置toptext和bottom text的文本?

公共类List_View扩展ListActivity {

私有DBAdapter db;

私有TextView toptext;私有TextView底层文本;私人ListView lv;

public void onCreate(Bundle savedInstanceState){try {

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);

} catch(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);
}

1 回答

  • 1

    你的代码中的一些修改......你得到了结果

    首先在ListActivity类中声明Cursor,Adapter类

    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 ....“title”和“date”你的数据库表列名...

相关问题