8

我有一个名为 InteractiveArrayAdapter 的自定义 ArrayAdapter,它将按钮和按钮侦听器添加到列表视图中的每个项目。在适配器内部有一个 getView 方法可以创建一个视图充气器。这里里面是我的按钮被创建和 buttonListener 被创建的地方。单击按钮时,我会删除与该按钮关联的 ArrayList 中的元素。问题是我无法弄清楚如何在此 OnClick 方法中调用 notifyDataSetChange,或者以另一种方式通知适配器需要更新 listView。

自定义适配器:

public class InteractiveArrayAdapter extends ArrayAdapter<String> {

  private final List<String> list;
  private final Activity context;
  private ListView listV;

  public InteractiveArrayAdapter(Activity context, List<String> list) {
    super(context, R.layout.rowbuttonlayout, list);
    this.context = context;
    this.list = list;

  }

  static class ViewHolder {
    protected TextView text;
    protected Button button;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;

    if (convertView == null) {
      LayoutInflater inflator = context.getLayoutInflater();
      view = inflator.inflate(R.layout.rowbuttonlayout, null);
      final ViewHolder viewHolder = new ViewHolder();
      viewHolder.text = (TextView) view.findViewById(R.id.label);
      viewHolder.button = (Button) view.findViewById(R.id.add);
      viewHolder.button
          .setOnClickListener(new Button.OnClickListener() {

//            @Override
//            public void onButtonClicked(Button buttonView,
//                boolean isChecked) {
//              String element = (String) viewHolder.button
//                  .getTag();
//              element.setSelected(buttonView.isChecked());
//
//            }

            public void onClick(View v) {
                String tag = (String) viewHolder.button.getTag();
                list.remove(list.indexOf(tag));
                // TODO Auto-generated method stub

            }
          });
      view.setTag(viewHolder);
      viewHolder.button.setTag(list.get(position));
    } else {
      view = convertView;
      ((ViewHolder) view.getTag()).button.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position));

    return view;
  }
} 

活动:

public class InventoryActivity extends Activity {
    public Inventory appInv;
    private ListView lv1 = null;
    private ListView lv2 = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences prefs = getPreferences(MODE_PRIVATE);
        appInv = new Inventory(prefs);

        setContentView(R.layout.inventory);

        // Get UI references.
        //
        lv1 = (ListView) findViewById (R.id.list1);
        lv2 = (ListView) findViewById (R.id.list2);
        ArrayAdapter<String> adapter1 = new InteractiveArrayAdapter (this, appInv.inventory);
        ArrayAdapter<String> adapter2 = new InteractiveArrayAdapter (this,  appInv.ingredients);
        lv1.setAdapter(adapter1);
        lv2.setAdapter(adapter2);
        // TODO Auto-generated method stub
    }
}
4

1 回答 1

10
    public void onClick(View v) {
                    String tag = (String) viewHolder.button.getTag();
                    list.remove(list.indexOf(tag));
                    notifyDataSetChanged()
                    // TODO Auto-generated method stub

                }

只需在您的 onClickListener 中添加 notifyDataSetChanged()。

于 2012-11-30T03:28:06.277 回答