如何从 ArrayAdapter 类更新 ListView?
How do I update a ListView from an ArrayAdapter class?
我有一个 ArrayList,它使用 ArrayAdapter 在 ListView 中显示其列表数据
每个 ListView 项都有一个 TextView 和一个 Button。单击按钮更新 ArrayList 中保存的数据,这工作正常。如何让 ListView 同时更新?我知道如何从创建 ArrayAdapter 的 Activity 执行此操作,因为我可以调用 itemAdapter.notifyDataSetChanged() 来更新视图。但是当单击 Button myButton 时,我看不到如何从 ArrayAdapter class(即 ListItemAdapter)中执行此操作。
有什么想法吗?谢谢
开始Activity课
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 类
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;
}
}
只需在适配器中使用相同的方法,但不使用对象引用,因为您处于对象的上下文中
mybutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dataitem.information = "Hello";
notifyDataSetChanged();
}
});
试试这个
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();
}
});
我有一个 ArrayList,它使用 ArrayAdapter 在 ListView 中显示其列表数据
每个 ListView 项都有一个 TextView 和一个 Button。单击按钮更新 ArrayList 中保存的数据,这工作正常。如何让 ListView 同时更新?我知道如何从创建 ArrayAdapter 的 Activity 执行此操作,因为我可以调用 itemAdapter.notifyDataSetChanged() 来更新视图。但是当单击 Button myButton 时,我看不到如何从 ArrayAdapter class(即 ListItemAdapter)中执行此操作。
有什么想法吗?谢谢
开始Activity课
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 类
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;
}
}
只需在适配器中使用相同的方法,但不使用对象引用,因为您处于对象的上下文中
mybutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dataitem.information = "Hello";
notifyDataSetChanged();
}
});
试试这个
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();
}
});