我想从我的数据库中添加一些信息到列表视图,我已经能够将该信息传递给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);
}
}
答案 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)
这里是示例
http://wiki.remobjects.com/wiki/Displaying_Data_with_ListView_and_BaseAdapter_(Android) http://androidsolution4u.blogspot.in/2013/09/android-populate-listview-from-sqlite.html
可能会帮助你。
答案 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);