2014-11-01 174 views
-1

我想從我的數據庫添加一些信息到列表視圖,我已經能夠將該信息傳遞給textView只是不知道如何獲得它到列表視圖this是我的代碼。將數據添加到數據庫的列表視圖

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

    } 


} 
+1

您需要了解適配器如何工作。谷歌搜索:「listview適配器教程」或類似的東西 – 2014-11-01 07:22:59

回答

1

您可以添加從DatabaseArrayList獲取數據,然後在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); 
+0

myList.setAdapter(new ArrayAdapter (this,android.R.layout.simple_list_item_1,test)); – 2014-11-01 07:40:42

+0

是的,你可以直接分配適配器而不用創建變量 – 2014-11-01 08:01:40

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