将数据添加到数据库的列表视图中

时间:2014-11-01 07:21:25

标签: android

我想从我的数据库中添加一些信息到列表视图,我已经能够将该信息传递给textView,只是不知道如何将它传递到列表视图这是我的代码。

   public class ViewClass extends Activity{
       ListView myList;
       @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homepage);
        Initialize();
        Database viewAgenda = new Database(this);
        viewAgenda.open();
        String data = viewAgenda.getAgendaData();
        viewAgenda.close();
        test.Add(data);
}
     private void Initialize() {
        // TODO Auto-generated method stub
        myList = (ListView) findViewById(R.id.panel_bottom);

    }


}

3 个答案:

答案 0 :(得分:1)

您可以在Database中添加ArrayList中的数据,然后在List

中添加此广告词
ArrayList<String> list_data = new ArrayList<>();
list_data.add("item"); // likewise add list items here

 ArrayAdapter<String> adapter= new ArrayAdapter<>(context, android.R.layout.activity_list_item, list_data);
myList.setAdapter(adapter);

答案 1 :(得分:0)

答案 2 :(得分:0)

您必须使用适配器类将数据库与ListView链接。请参阅以下代码。

ListView lv = (ListView) findViewById(The Id of your ListView);
//fetch your data to a  Cursor
Cursor c = "The result of a select statement";        
SimpleCursorAdapter adap = new SimpleCursorAdapter(this, 
            R.layout.item2,//Your layout for the list view 
            c, 
            new String[]{"fullname", "balance"}, //field names
            new int[]{R.id.rowData, R.id.rowMoney});//The ids of elements from your list view layout. The field "fullname" will be loaded into the "R.id.rowData" element.     
lv.setAdapter(adap);