2014-02-06 53 views
1

我正在創建一個android應用程序,我有一個片段,作爲textview和一個按鈕。當你點擊按鈕,你將被帶到另一個活動,這是一個表格來填充它有一個添加按鈕。當單擊添加按鈕時,數據將被保存爲解析,現在問題是我想要檢索數據並將其填充到列表視圖並在片段中查看它。請幫忙。從解析獲取數據並填充到一個片段的列表視圖

+0

您可以使用基地適配器列表視圖的片段類中。檢查此線程的代碼:http://stackoverflow.com/questions/20177089/android-baseadapter-with-fragment – Lokesh

回答

2

使用ArrayList和適配器,

您的代碼應該是這樣的:

public class YourActivity extends Activity { 

private ListView lv; 

public void onCreate(Bundle saveInstanceState) { 
    setContentView(R.layout.your_layout); 

    lv = (ListView) findViewById(R.id.your_list_view_id); 

    // Instanciating an array list (you don't need to do this, 
    // you already have yours). 
    ArrayList<String> your_array_list = new ArrayList<String>(); 
    your_array_list.add("foo"); 
    your_array_list.add("bar"); 

    // This is the array adapter, it takes the context of the activity as a 
    // first parameter, the type of list view as a second parameter and your 
    // array as a third parameter. 
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
      this, 
      android.R.layout.simple_list_item_1, 
      your_array_list); 

    lv.setAdapter(arrayAdapter); 
    } 
} 

參見:Populating a ListView using an ArrayList?

相關問題