ArrayAdapter和NotifyDataSetChanged()

时间:2014-06-08 07:05:15

标签: android android-arrayadapter

我试图在我的Android应用程序中使用ArrayAdapter的notifyDataSetChanged()方法,但它对我不起作用。 我有时会收到这个错误:

enter image description here

Adapterarticlelist类:

public class Adapterarticlelist extends ArrayAdapter<StructNote> {

    public Adapterarticlelist(ArrayList<StructNote> array) {
        super(G.context, R.layout.adapter_articlelist, array);
    }


    private static class ViewHolder {

        //  public ViewGroup layoutarticleroot;
        public TextView txtarticlebrand;
        public TextView txtarticlemodel;
        public Button   btnbuy;
        public Button   btnsend;
        public EditText txtvalue;
        public TextView txtarticletype;
        public TextView txtarticleid;


        public ViewHolder(View view) {
            //     layoutarticleroot = (ViewGroup) view.findViewById(R.id.layoutroot);
            txtarticletype = (TextView) view.findViewById(R.id.txtarticletype);
            txtvalue = (EditText) view.findViewById(R.id.txtvalue);
            txtarticlebrand = (TextView) view.findViewById(R.id.txtarticlebrand);
            btnsend = (Button) view.findViewById(R.id.btnsend);
            btnbuy = (Button) view.findViewById(R.id.btnbuy);
            txtarticlemodel = (TextView) view.findViewById(R.id.txtarticlemodel);
            txtarticleid = (TextView) view.findViewById(R.id.txtarticleid);
        }


        public void fill(final ArrayAdapter<StructNote> adapter, final StructNote item, final int position) {

            btnsend.setVisibility(View.GONE);
            txtvalue.setVisibility(View.INVISIBLE);
            txtvalue.setText(item.articlevalue);
            txtarticletype.setText(item.articletype);
            txtarticlebrand.setText(item.articlebrand);
            txtarticlemodel.setText(item.articlemodel);
            txtarticleid.setText(item.articleid);

            btnbuy.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    txtvalue.setVisibility(View.VISIBLE);
                    btnbuy.setVisibility(View.GONE);
                    btnsend.setVisibility(View.VISIBLE);

                }
            });

            btnsend.setOnClickListener(new OnClickListener() {



                @Override
                public void onClick(View arg0) {
                    if (txtvalue.length() <= 0) {
                        Toast.makeText(G.currentActivity, "insert your value:", Toast.LENGTH_LONG).show();
                    }
                    else {
                        G.database.execSQL("INSERT INTO article_select (article_id,article_model,article_brand,article_type,article_value,person_id) VALUES ('" + item.articleid + "', '" + item.articlemodel + "','" + item.articlebrand + "','" + item.articletype + "','" + txtvalue.getText() + "','" + item.id + "');");
                        txtvalue.setVisibility(View.GONE);
                        btnsend.setVisibility(View.GONE);


                    }

                }
            });



        }

    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        StructNote item = getItem(position);
        if (convertView == null) {
            convertView = G.inflater.inflate(R.layout.adapter_articlelist, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.fill(this, item, position);
        return convertView;
    }

}

似乎我必须在notifyDataSetChanged()函数之后使用btnsend.setOnClickListener函数,但是如何? 有谁能告诉我如何解决它?

my site

2 个答案:

答案 0 :(得分:1)

如错误消息所示,您必须致电notifyDataSetChanged()

  1. 修改ArrayAdapter的数据后,
  2. 始终来自UI线程,绝不是来自后台。
  3. 换句话说,不要调用add()remove()或以任何其他方式更改后台线程中的后备数据(例如,如果您使用AsyncTask来更新ListView,在onPostExecute()中执行此操作。

答案 1 :(得分:1)

只需使用adapter.notifyDatasetChanged();插入新数据后