Reputation: 17383
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 :
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?
Upvotes: 0
Views: 487
Reputation: 55350
As the error message indicates, you must call notifyDataSetChanged()
:
ArrayAdapter
's data, andIn 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