Skip to content Skip to sidebar Skip to footer

How To Update The Listview When I Click The Button?

public class MainActivity extends Activity { ListView list; String[] abc={'1','2','3'}; MyCustomAdapter adapter; Button refresh; @Override prote

Solution 1:

Change getView method code as below.

@Overridepublic View getView(finalint position, View convertView,ViewGroup parent) {
            Viewvi= convertView;
            final TextView textView;
            if (convertView == null)
                inflater = (LayoutInflater)    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            vi = inflater.inflate(R.layout.vi, null);

            textView = (TextView) vi.findViewById(R.id.text);
            Buttonbtn= (Button) vi.findViewById(R.id.refresh);
            textView.setText(hashmap[position]);
            btn.setOnClickListener(newOnClickListener() {
                @OverridepublicvoidonClick(View v) {
                    Log.i("position  ", position + "");
                    textView.setText("10");
                }
            });

            return vi;
        }

Solution 2:

try this :

holder.btn.setTag(convertView);

holder.btn.setOnClickListener(newOnClickListener(){  

                       @OverridepublicvoidonClick(View v) {
                          ViewtempView= v.getTag();
            TextViewtv= (TextView)tempView.findViewById(R.id.text);
            tv.setText("your string");  
                       }
                     });

Solution 3:

call this

setListAdapter(newArrayAdapter<String>(this, R.layout.list_item, your_new_value));

For more info try this link

Solution 4:

For updating values listview you can use adapter.notifyDataSetChanged() this will refresh your list adapter. But call this after changing value in your data source.

Solution 5:

You should use notifyDataSetChanged () method to refresh your listview...

Call notifyDataSetChanged() on your Adapter

  refresh.setOnClickListener(newOnClickListener() {
            @OverridepublicvoidonClick(View p_v) {
                // TODO Auto-generated method stub
                adapter.notifyDataSetChanged();
            }
        });

Post a Comment for "How To Update The Listview When I Click The Button?"