我想在图像视图点击的数组列表中添加一些元素后更新列表视图。任何人都可以帮助我,我该怎么做?
这是我的代码。
public class DealDetailActivity extends Activity {
private ListView lvAddDocument;
private ImageView ivAddDocument;
private ArrayList<String> list;
// private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.deal_detail_screen);
list = new ArrayList<String>();
lvAddDocument = (ListView) findViewById(R.id.lv_add_document);
ivAddDocument = (ImageView) findViewById(R.id.iv_add_document);
ivAddDocument.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
list.add("rohit");
Log.i("list", "" + list.size());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
DealDetailActivity.this,
android.R.layout.simple_list_item_1, list);
lvAddDocument.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
}
}
答案 0 :(得分:3)
这样做:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.deal_detail_screen);
list = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
DealDetailActivity.this,
android.R.layout.simple_list_item_1, list);
lvAddDocument = (ListView) findViewById(R.id.lv_add_document);
lvAddDocument.setAdapter(adapter);
ivAddDocument = (ImageView) findViewById(R.id.iv_add_document);
ivAddDocument.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
adapter.add("rohit");
Log.i("list", "" + list.size());
adapter.notifyDataSetChanged();
}
});
答案 1 :(得分:1)
尝试将Adapter从匿名类中删除:
public class DealDetailActivity extends Activity {
private ListView lvAddDocument;
private ImageView ivAddDocument;
private ArrayList<String> list;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.deal_detail_screen);
list = new ArrayList<String>();
lvAddDocument = (ListView) findViewById(R.id.lv_add_document);
ivAddDocument = (ImageView) findViewById(R.id.iv_add_document);
adapter = new ArrayAdapter<String>(
DealDetailActivity.this,
android.R.layout.simple_list_item_1, list);
lvAddDocument.setAdapter(adapter);
ivAddDocument.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
list.add("rohit");
Log.i("list", "" + list.size());
adapter.notifyDataSetChanged();
}
});
}
}
答案 2 :(得分:0)
adapter.notifyDataSetChanged();
lvAddDocument.setAdapter(adapter);
首先尝试通知adapterand然后将该适配器设置为listview.Hope这将起作用。
答案 3 :(得分:0)
1)您必须将arrayList声明为全局arraylist意味着:
ArrayList<DemoClass> arrlistDemo;
然后在onCreate方法中你必须创建你的arrayList Like:
arrlistDemo = new ArrayList<DemoClass>();
2)每当你想要添加项目时,你只需要向arrayList添加项目,而不是调用notifyDatasetChanged()方法。你的listView将更新其他数据。