Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to refresh a List in Android?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 278
    Comment on it

    1)Write activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">
    
       <android.support.v4.widget.SwipeRefreshLayout
           android:id="@+id/swipe"
           android:layout_height="match_parent"
           android:layout_width="match_parent"
           android:layout_below="@+id/btnClick">
    
           <ListView
               android:id="@+id/listView"
               android:layout_width="match_parent"
               android:layout_height="match_parent"/>
    
    
       </android.support.v4.widget.SwipeRefreshLayout>
    
        <Button
            android:id="@+id/btnClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="HIT" />
    
    </RelativeLayout>
    

    Here,to swipe a list we have put a Listview in

    <android.support.v4.widget.SwipeRefreshLayout>
    

    because Android provides a Support widget to swipe something that is declared inside it.


    2)In respective Java file,onCreate method will be:

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
            listView = (ListView) findViewById(R.id.listView);
            btnClick = (Button) findViewById(R.id.btnClick);
            value = new ArrayList<>();
    
    
            month = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, value);
            listView.setAdapter(month);
            btnClick.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    value.add(value.size() + "");
    
                }
            });
    
            swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
    
                    month.notifyDataSetChanged();
                    swipe.setRefreshing(false);
    
                }
            });
           /* swipe.setColorSchemeColors(android.R.color.holo_red_light, android.R.color.holo_green_light,
                    android.R.color.holo_purple, android.R.color.holo_blue_light);*/
            swipe.setRefreshing(false);
    
    
        }
    

    Here on a Button Click,I have count that how many times Button is pressed and when we Swipe down and refresh a list,it will show the values in a list.

    So that's how we refresh a list by Swiping down:)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: