0

I am new at Android and have a list to add and remove items at run time.
I am using the following code to add a new item:

public class MainActivity extends Activity {

private ListView list;
private Button btAdd;
private ArrayAdapter<String[]> adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list=(ListView)this.findViewById(R.id.ListView1);
    btAdd=(Button)this.findViewById(R.id.button1);
    String [] name={"new Item"};
    adapter=new ArrayAdapter<String[]>(this,R.id.ListView1);
    adapter.add(name);


    btAdd.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
          {
            list.setAdapter(adapter);
          }

    });   

}

What is the problem with this?

4

1 回答 1

2

1)在onCreate中设置你的适配器:list.setAdapter(adapter);

2)添加//删除您的 OnClickListener 中的项目,然后调用adapter.notifyDatasetChanged()

于 2013-07-20T22:10:06.263 回答