0

我有一个应用程序,它按变体、单位和数量列出产品的值。

我有一个显示 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();

    }
}

这是我的自定义适配器

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活动中刷新。

有任何想法吗?

谢谢。

4

1 回答 1

2

在 OrderForm 活动中保留对您的 CustomAdapter 的引用:

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()。这是适配器上的公共方法,因此您可以从活动中调用它就好了。

于 2013-07-23T22:37:50.373 回答