S.M_Emamian
S.M_Emamian

Reputation: 17383

ArrayAdapter and NotifyDataSetChanged()

I'm trying to use in my Android Application the notifyDataSetChanged() method for an ArrayAdapter but it doesn't work for me. I sometimes get this error :

enter image description here

Adapterarticlelist class :

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

}

It seems I must use notifyDataSetChanged() function after btnsend.setOnClickListener function, but how? Could anybody tell me how do I fix it?

my site

Upvotes: 0

Views: 487

Answers (2)

sakir
sakir

Reputation: 3502

just use adapter.notifyDatasetChanged(); after insert the new data

Upvotes: 1

matiash
matiash

Reputation: 55350

As the error message indicates, you must call notifyDataSetChanged():

  1. Just after modifying the ArrayAdapter's data, and
  2. Always from the UI thread, never from background.

In other words, do not call add(), remove() or in any other way change the backing data from a background thread (for example, if you're using an AsyncTask to update the ListView, do it in onPostExecute()).

Upvotes: 2

Related Questions