2012-08-13 63 views
0

我創建了這個簡單的android活動來演示我的問題。在屏幕上添加ListView並用按鈕單擊添加元素

我只想讓屏幕有一個textInput和一個按鈕。下面這兩個,我想創建一個ListView,如果按鈕被按下(該按鈕基本上調用一些方法並獲得一個String數組,我想讓ListView顯示該數組。 ,文本輸入時,按下按鈕它調用的方法和接收字符串數組,並希望打印列表它們下面。

public class TagListViewer extends ListActivity { 

    private Button clickBtn; 
    EditText textInput; 
    String[] resultStr = {"a", "b", "c"}; //Ideally would want this inside the button OnClickListener... but couldn't bcz I needed one for the Array adapter. 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tagselection); 

     clickBtn = (Button) findViewById(R.id.CreatePL); 
     clickBtn.setText("Search"); 
     textInput = (EditText) findViewById(R.id.textInput); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, resultStr); 
     setListAdapter(adapter); 

     clickBtn.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

       ArrayAdapter<String> adapter = (ArrayAdapter<String>) getListAdapter(); 

       adapter.add("ABC"); //This I could use the array I get to add its elements 
       adapter.notifyDataSetChanged(); 
      } 

     }); 
    } 
} 

回答

2

我不能肯定你的問題是什麼,但我注意到,你是試圖添加項目到原始字符串數組並創建兩個不同的適配器...我對這個問題有一個預感。請看下面的簡單更改:

public class TagListViewer extends ListActivity { 
    // Make adapter a class variable 
    private ArrayAdapter<String> adapter; 

    private Button clickBtn; 
    EditText textInput; 

    // You cannot add items to a primitive String array, we'll convert this to an ArrayList 
    String[] resultStr = {"a", "b", "c"}; 
    List<String> list = new ArrayList<String>(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tagselection); 

     // Add contents of resultStr to the dynamic List 
     Collections.addAll(list, resultStr); 

     clickBtn = (Button) findViewById(R.id.CreatePL); 
     clickBtn.setText("Search"); 
     textInput = (EditText) findViewById(R.id.textInput); 

     // Reflect class variable change and use list instead of resultStr 
     adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list); 
     setListAdapter(adapter); 

     clickBtn.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       // This will add the one phrase "ABC" 
       //adapter.add("ABC"); 

       // This will add the contents of textInput 
       adapter.add(textInput.getText().toString()); 
      } 
     }); 
    } 
} 

新增的評論

一個ListActivity需要有id爲android.R.id.list一個ListView在其佈局:

<ListView android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 
+0

是的,這基本上我想,只是有什麼需要的resultStr這不是問題。但是當我運行它時,它給了我RunTimeException:「你的內容必須有一個ListView,其id屬性是'android.R.id.list」btw我凸輪從另一個屏幕調用整個屏幕,這可能是原因嗎? – Achilles 2012-08-13 21:24:21

+0

這個錯誤僅僅意味着你的佈局tagselection.xml需要一個帶有屬性「android:id =」@ android.R.id.list「'的ListView。 – Sam 2012-08-13 21:38:32

+0

讓我試試這個,看看結果,對不起,我把它添加到你的代碼中,這樣你就可以看到,但我們可以修復,只要我找到最終的解決方案 – Achilles 2012-08-13 22:06:16