0

Possible Duplicate:
How to refresh Android listview?

I am looking for using Refresh button refreshing the listview data.. DES:In my app there is refresh button when i onclick on it Listview data which coming from server must get refresh & show update listview data.
And I have no idea how to do that. plz suggest me with example.. Thanks in advance. below is my code.

Mainactivity.java

listView = (ListView) findViewById(R.id.homelistView);
listView.setTextFilterEnabled(true);   
final EfficientAdapter objectAdapter = new EfficientAdapter(this);
listView.setAdapter(objectAdapter);


Button refreshButton= (Button)findViewById(R.id.refreshButton);
 refreshButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    objectAdapter.notifyDataSetChanged();

    }
});

EfficientAdapter.java

public static class EfficientAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context context;

    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        this.context=context;

    }


    public int getCount() {
        return CountriesList.name.length;
    }

    public Object getItem(int position) {

        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.homemplebrowview, null);
            holder = new ViewHolder();
            holder.text1 = (TextView) convertView
                    .findViewById(R.id.name);
            holder.text2 = (TextView) convertView
                    .findViewById(R.id.mrn);
            holder.text3 = (TextView) convertView
                    .findViewById(R.id.date);
            holder.text4 = (TextView) convertView
                    .findViewById(R.id.age);
            holder.text5 = (TextView) convertView
                    .findViewById(R.id.gender);
            holder.text6 = (TextView) convertView
                    .findViewById(R.id.wardno);
            holder.text7 = (TextView) convertView
                    .findViewById(R.id.roomno);
            holder.text8 = (TextView) convertView
                    .findViewById(R.id.bedno);                  
            holder.btnList = (Button)convertView.findViewById(R.id.listbutton);
         //   holder.btnList.setOnClickListener(this);

            holder.btnList.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {                       
                    Intent next=new Intent(context, SeviceDetails.class);
                    context.startActivity(next);
                }
            });


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text1.setText(CountriesList.name[position]);
        holder.text2.setText(CountriesList.mrn[position]);
        holder.text3.setText(CountriesList.actualstart[position]);
        holder.text4.setText(CountriesList.age[position]);
        holder.text5.setText(CountriesList.gender[position]);
        holder.text6.setText(CountriesList.wardNo[position]);
        holder.text7.setText(CountriesList.roomNo[position]);
        holder.text8.setText(CountriesList.bedNo[position]);

        return convertView;
    }


    static class ViewHolder {
        public Button btnList;
        public TextView text8;
        public TextView text7;
        public TextView text6;
        public TextView text5;
        public TextView text4;
        public TextView text1;
        public TextView text2;
        public TextView text3;
    }

    @Override
    public void notifyDataSetChanged() // Create this function in your adapter class
    {
        super.notifyDataSetChanged();
    }


}



}
7
  • Try this: androidhive.info/2012/03/android-listview-with-load-more-button. For automatic additions, try this: mysamplecode.com/2012/07/android-listview-load-more-data.html. And, don't forget the power of SEARCHING!! Commented Sep 17, 2012 at 4:58
  • In your main Activity why you call setAdapter twice? Commented Sep 17, 2012 at 8:51
  • You should remove this line listView.setAdapter(new EfficientAdapter(this)); Commented Sep 17, 2012 at 8:51
  • @MohitVerma thank u..currently i am using static data in countrieslist.java.. How to check my refresh data is working fine or not. currently its not working. Commented Sep 17, 2012 at 9:04
  • Since your data source is static, you cannot check whether its working Commented Sep 17, 2012 at 9:39

4 Answers 4

2

Add new values in your adapter and call notifyDataSetChanged .

Follow the step to update listview.

1) First Add new values to your CountriesList .
2) Before Assign EfficientAdapter to listiew, make one object of it and then assign that object.
3) Create one function in your  EfficientAdapter class called notifyDataSetChanged().
4) Call notifyDataSetChanged() function using EfficientAdapter object.

Example

EfficientAdapter objectAdapter = new EfficientAdapter(this);
listView.setAdapter(objectAdapter);

@Override
public void notifyDataSetChanged() // Create this function in your adapter class
{
    super.notifyDataSetChanged();
}

Now call objectAdapter.notifyDataSetChanged() on onClick event of refresh button.

Sign up to request clarification or add additional context in comments.

12 Comments

This question is duplicate. Asked so many times on StackOverflow.
@ChiragRaval thank u for ur kind answer.. Plz dnt flag it..
@KarthikRao i am not going to flag it.
@ChiragRaval currently i am using static data in my app.. hw to check it. its refreshing or not..
@KarthikRao try to add more static data in list and check it again
|
2

You can do something like this

public class Mainactivity extends Activity{

ListView listView;
EfficientAdapter objectAdapter;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainlayout);

listView = (ListView) findViewById(R.id.homelistView);
listView.setTextFilterEnabled(true);   
objectAdapter = new EfficientAdapter(this);
listView.setAdapter(objectAdapter);


Button refreshButton= (Button)findViewById(R.id.refreshButton);
 refreshButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

       objectAdapter = new EfficientAdapter(Mainactivity.this);// adapter with new data
       listView.setAdapter(objectAdapter);
       objectAdapter.notifyDataSetChanged();

    }
});

}

}

This is a sample of what I want to tell you:

public class second extends Activity{

    ListView listView;
    ArrayAdapter<String> adapter;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        listView=(ListView) findViewById(R.id.listView1);
        button=(Button) findViewById(R.id.button1);

        String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",  
                "Jupiter", "Saturn", "Uranus", "Neptune"};    
        ArrayList<String> planetList = new ArrayList<String>();  
        planetList.addAll( Arrays.asList(planets) );  

        // Create ArrayAdapter using the planet list.  
        adapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);  

        listView.setAdapter(adapter);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String[] planets = new String[] { "Mercury m", "Venus m", "Earth m", "Mars m",  
                        "Jupiter m", "Saturn m", "Uranus m", "Neptune m"};    
                ArrayList<String> planetList = new ArrayList<String>();  
                planetList.addAll( Arrays.asList(planets) );  
                adapter = new ArrayAdapter<String>(second.this, R.layout.simplerow, planetList);

                listView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        });

    }

}

Where R.layout.simplerow:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/rowTextView" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"
 android:padding="10dp"
 android:textSize="16sp" >
</TextView>

and R.layout.second:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >





    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

    </ListView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</RelativeLayout>

3 Comments

i add ur code wr i have to set new data in my static data CountriesList.java Or in mainaactivity. currenly not geting
In your adapter class you have used Countries.java class variables, but you can create new EfficientAdapter2 class where you can use Countries2.java class with different content.First you use objectAdapter = new EfficientAdapter(this) then in button click you can use objectAdapter = new EfficientAdapter2(Mainactivity.this)
thanks a lot lot... Its works fine.. when data is coming from webservice i have to use only this thing rite in onClick(View v) { objectAdapter = new EfficientAdapter(Mainactivity.this);// adapter with new data listView.setAdapter(objectAdapter); objectAdapter.notifyDataSetChanged(); Thanks a lot once again...
0

You have to add new values to CountriesList and call notifyDataSetChanged() API.

Comments

0
Button refreshButton= (Button)findViewById(R.id.refreshButton);
 refreshButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    /*Intent next=new Intent(MainActivity.this, SeviceDetails.class);
    startActivity(next);*/
     Do a notifyDataSetChanged();

    }
});

Click for More Info

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.