我有一个应用程序,它按Variant,Unit和Quantity列出产品的值。
我有一个显示ListView的活动:
public class OrderForm extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_form);
ArrayList image_details = getListData();
final ListView lv1 = (ListView) findViewById(R.id.custom_list);
lv1.setAdapter(new CustomListAdapter(this, image_details));
}
private ArrayList getListData() {
ArrayList results = new ArrayList();
OrderDetailsClass orderData = new OrderDetailsClass();
return results;
}
public void onAddItem(View view){
ArrayList results = new ArrayList();
OrderDetailsClass orderData = new OrderDetailsClass();
orderData = new OrderDetailsClass();
orderData.setVariants("Flavored");
orderData.setUnit("cans");
orderData.setQuantity(1);
results.add(orderData);
//selectVariant();
//selectUnit();
//selectQuantity();
}
}
这是我的CustomAdapter
public class CustomListAdapter extends BaseAdapter {
private ArrayList<OrderDetailsClass> listData;
private LayoutInflater layoutInflater;
public CustomListAdapter(Context context, ArrayList<OrderDetailsClass> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.variantView = (TextView) convertView.findViewById(R.id.variant);
holder.unitView = (TextView) convertView.findViewById(R.id.unit);
holder.quantityView = (TextView) convertView.findViewById(R.id.quantity);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.variantView.setText(listData.get(position).getVariants());
holder.unitView.setText("By, " + listData.get(position).getUnit());
holder.quantityView.setText(String.valueOf(listData.get(position).getQuantity()));
return convertView;
}
static class ViewHolder {
TextView variantView;
TextView unitView;
TextView quantityView;
}
public void updateResults(ArrayList<OrderDetailsClass> results) {
listData = results;
//Triggers the list update
notifyDataSetChanged();
}
}
这是list_row_layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!--
android:minHeight="50dp"
android:orientation="horizontal"
android:padding="5dip"
-->
<TextView
android:id="@+id/variant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="variant" />
<TextView
android:id="@+id/unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="unit" />
<TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="quantity" />
</RelativeLayout>
这是activity_order_form
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/custom_list"
android:layout_width="fill_parent"
android:layout_height="386dp"
android:dividerHeight="1dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="onAddItem"
android:text="Add" />
</RelativeLayout>
我想要做的是当我按下onAddItem
功能时,在添加数据后,ListView将刷新并显示更改。到目前为止,我所看到的是notifyDataSetChanged()
类中使用了BaseAdapter
。我已经尝试了,但似乎notifyDataSetChanged()
应该在BaseAdapter
内调用,我希望能够从我的OrderForm
活动中刷新。
有什么想法吗?
感谢。
答案 0 :(得分:2)
在订单表格活动中保留对自定义适配器的引用:
public class OrderForm extends Activity {
CustomListAdapter customListAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_form);
ArrayList image_details = getListData();
final ListView lv1 = (ListView) findViewById(R.id.custom_list);
customListAdapter = new CustomListAdapter(this, image_details);
lv1.setAdapter(customListAdapter);
}
然后你可以致电customAdapter.notifyDataSetChanged()
。这是适配器上的公共方法,因此您可以从活动中调用它。