pedja
pedja

Reputation: 3413

Android Refresh ListActivity from BaseAdapter

I have a ListActivity and a custom adapter that extends BaseAdapter.

On each row of the ListView, there is a button and a few more views. onClickListener is declared in the adapter.

How can I refresh list view after button is clicked ?

Here is the code

List Activity:

 .........
 super.onCreate(savedInstanceState);


here im reading content from a file and then creating adapter with that content

     adapter = new UvArrayAdapter(this, strarray3, strarray2, intarray);
    setListAdapter(adapter);
 ..........

Adapter:

.......

 public class UvArrayAdapter extends BaseAdapter {
 private Context mContext;
 private LayoutInflater linflater;
 private TextView txt_1, txt_2, txt_3;
 private ProgressBar pr1;
private String[] str1;
private String[] str2;
private String[] str3;
private Integer[] int1;

private class OnItemClickListener implements OnClickListener{       
    private int mPosition;
    OnItemClickListener(int position){
        mPosition = position;
    }
    @Override
    public void onClick(View arg0) {
        Log.v("TAG", "onItemClick at position" + mPosition);          
    }       
}



public UvArrayAdapter(Context context, String[] s1, String[] s2, Integer[] i1) {
mContext = context;
str1 = s1;
 str2 = s2;
int1 = i1;
linflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
return str1.length;
}

@Override
public Object getItem(int arg0) {
return str1[arg0];
}

@Override
public long getItemId(int arg0) {
return arg0;
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {

if (convertView == null) {

    convertView = linflater.inflate(R.layout.uv_rowlayout, null);

}
convertView.setOnClickListener(new OnItemClickListener(position));
txt_1 = (TextView) convertView.findViewById(R.id.textView1);
pr1 = (ProgressBar) convertView.findViewById(R.id.progressBar1);
pr1.setProgress(int1[position]);
txt_1.setText(str1[position]);

txt_2 = (TextView) convertView.findViewById(R.id.textView2);
txt_2.setText(str2[position]+"mV");
Button btn = (Button) convertView.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
         Log.v("onClickListener", "button - clicked at position "             position);

        do some changes in mentioned file and after that i need to update the list view with new data
             }});




return convertView;

}



}

Upvotes: 3

Views: 4571

Answers (2)

Swayam
Swayam

Reputation: 16354

adapter.notifyDataSetChanged(); // to notify the adapter that your data has been updated

Note: If you get any errors with the above line, you could try the following code (where mListView is the name of my ListView object)

((BaseAdapter) mListView.getAdapter()).notifyDataSetChanged(); 

Another way would be to invalidate the List so that it is redrawn form the updated data set once again.

mListView=(ListView) findViewById(R.id.MyListView);
mListView.invalidate();

Upvotes: 4

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

Use notifyDataSetChanged()

adapter = new UvArrayAdapter(this, strarray3, strarray2, intarray);
setListAdapter(adapter);
UvArrayAdapter.notifyDataSetChanged(); 

// To make the adapter aware of the changes

Upvotes: 2

Related Questions