[英]How do I update a ListView from an ArrayAdapter class?
I have a ArrayList which uses an ArrayAdapter to display its list data in a ListView 我有一个ArrayList,它使用ArrayAdapter在ListView中显示其列表数据
Each ListView item has a single TextView and a Button. 每个ListView项都有一个TextView和一个Button。 Clicking the button updates the data held in the ArrayList and this works fine.
单击该按钮将更新保存在ArrayList中的数据,并且可以正常工作。 How do I get the ListView to update at the same time?
如何同时更新ListView? I know how to do this from the Activity where the ArrayAdapter is created because I can call itemAdapter.notifyDataSetChanged() to update the view.
我知道如何从创建ArrayAdapter的Activity中执行此操作,因为我可以调用itemAdapter.notifyDataSetChanged()来更新视图。 But I don't see how I can do it from within the ArrayAdapter class (ie ListItemAdapter) when Button myButton is clicked.
但是,当单击Button myButton时,我看不到如何从ArrayAdapter类(即ListItemAdapter)中做到这一点。
Any ideas? 有任何想法吗? Thanks.
谢谢。
StartActivity Class StartActivity类
public class StartActivity extends Activity {
ArrayList<DataRecord> datalist;
ListView listView;
ListItemAdapter itemAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_screen);
datalist = new ArrayList<DataRecord>();
listView = (ListView) findViewById(R.id.ListViewId);
itemAdapter = new ListItemAdapter(this, android.R.layout.simple_list_item_1, datalist);
listView.setAdapter(itemAdapter);
DataRecord dataRecord = new DataRecord("Data Item 1 ");
datalist.add(dataRecord);
dataRecord = new DataRecord("Data Item 2 ");
datalist.add(dataRecord);
dataRecord = new DataRecord("Data Item 3");
datalist.add(dataRecord);
itemAdapter.notifyDataSetChanged();
}
}
ListItemAdapter Class ListItemAdapter类
public class ListItemAdapter extends ArrayAdapter<DataRecord> {
private ArrayList<DataRecord> datalist;
public ListItemAdapter(Context context, int textViewResourceId, ArrayList<DataRecord> datalist) {
super(context, textViewResourceId, datalist);
this.datalist = datalist;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_row, null);
}
Button mybutton = (Button)v.findViewById(R.id.pushBtn);
final DataRecord dataitem = datalist.get(position);
mybutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dataitem.information = "Hello";
for (int i = 0; i < datalist.size(); i++) {
Log.d("Item", datalist.get(i).information); // this shows the ArrayList gets updated
}
}
});
if (dataitem != null) {
TextView listText = (TextView) v.findViewById(R.id.list_text);
if (listText != null) {
listText.setText(dataitem.information);
}
}
return v;
}
}
Just use the same method in the adapter, but without the object reference, since you are in the context of the object 只需在适配器中使用相同的方法,但不使用对象引用,因为您位于对象的上下文中
mybutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dataitem.information = "Hello";
notifyDataSetChanged();
}
});
try this 尝试这个
mybutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//
//update data
//
this.notifyDataSetChanged();
}
});
mybutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dataitem.information = "Hello";
ListItemAdapter.this.notifyDataSetChanged();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.