rrayAdapter类中的notifyDataSetChang

rrayAdapter类中的notifyDataSetChang

本文介绍了Android-自定义ArrayAdapter类中的notifyDataSetChange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a custom ArrayAdapter called InteractiveArrayAdapter that adds buttons and button listeners to each item in a list view. Inside the adapter there is a getView method that creates a view inflator. Inside of here is where my button is created and the buttonListener is created. When the button is clicked i delete the element in my ArrayList associated with that button. The problem is that i cant figure out how to call notifyDataSetChange from within this OnClick method, or another way to notify the adapter that the listView needs to be updated.

自定义适配器:

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
    }
}

推荐答案

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

                }

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

Just add notifyDataSetChanged() in ur onClickListener.

这篇关于Android-自定义ArrayAdapter类中的notifyDataSetChange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:37