动态添加项目到android中的listview

时间:2012-02-15 19:12:06

标签: android android-listview add

我正在制作一个需要将项目添加到动态ListView的Android应用程序。我怎样才能做到这一点?我希望在按下"AddItemToListViewButton"后添加该项目。我试图使用下面的代码,但是,它不起作用。我该如何修复/实现这一目标?我的代码:

public class NotesActivity extends ListActivity implements OnClickListener {

    List<String> myList = new ArrayList<String>();
    EditText AddItemToListViewEditText;
    Button AddItemToListView, AddItemToListViewButton;
    LinearLayout AddItemToListViewLinearLayout;
    static final String[] COUNTRIES = new String[] {
      "Matte på A1 med Ole", "Engelsk på klasserommet", "Film på A1 etter friminuttet"
      };

   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notes);
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
        // When clicked, show a toast with the TextView text
        Toast.makeText(getApplicationContext(), "Note: " + ((TextView) view).getText(),
            Toast.LENGTH_SHORT).show();
  }
    });
  }
    @Override
   public boolean onCreateOptionsMenu(Menu meny) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.listviewmenubuttons, meny);
      return true;
   }
    @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      switch(item.getItemId()) {
      case R.id.AddItemToListView:
        AddItemToListViewEditText = (EditText)findViewById(R.id.AddItemToListViewEditText);
        AddItemToListViewButton = (Button)findViewById(R.id.AddItemToListViewButton);
        AddItemToListViewLinearLayout = (LinearLayout)findViewById(R.id.AddItemToListViewLinearLayout);
        AddItemToListViewButton.setOnClickListener(this);
        AddItemToListViewLinearLayout.setVisibility(View.VISIBLE);
        break;
        }
    return true;
}
   public void onClick(View src) {
      switch(src.getId()) {
      case R.id.AddItemToListViewButton:
        myList.add("Hello");
        myList.add("world");
        System.out.println("The list contains " +
           myList.size() + "elements, and " +
           "the first is " + myList.get(0));
        AddItemToListViewLinearLayout.setVisibility(View.GONE);
        break;
    }
}
}

3 个答案:

答案 0 :(得分:3)

您的ListActivityListView元素基于ArrayAdapter<String>,其中包含一个名为 COUNTRIES 的字符串数组。在onClick()方法中,您将字符串添加到ArrayList<String>(myList),但您的ListView适配器不知道它并且您尝试将其添加到列表中(这就是我想你是想做的)。所以:

- 在myList添加COUNTRIES字符串数组的当前内容:

myList.add("Matte på A1 med Ole");//and the other strings from COUNTRIES

- 将适配器设置为指向myList

setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, myList));

-in onClick()方法写:

public void onClick(View src) {
    switch(src.getId()) {
    case R.id.AddItemToListViewButton:
        myList.add("Hello");
        myList.add("world");
        ((ArrayAdapter)getListView().getAdapter()).notifyDataSetChanged(); 
        AddItemToListViewLinearLayout.setVisibility(View.GONE);
        break;
    }
}

答案 1 :(得分:0)

尝试重新加载列表,或者只是

ListAdapter notifyDataSetChanged();

答案 2 :(得分:0)

你想在点击事件中需要这个......

((ArrayAdapter)getListView().getAdapter()).notifyDataSetChanged();