开发者

How to update list in android

i have a list in android. and it has 30 records currently. but on my activity i am only showing 5 records... after 5 records it shows me a button

"Display All Data"

when i click on that button, then it should display all 30 records and update the activity List. Please tell me how can i update. like we do in AJAX in web technology. i hope u guys understand what i am trying to say?

Refresh the Activity without refreshing the whole activity. Please Reply Friends.

waiting for positive 开发者_如何学JAVAresponse.


You should just simply add the newly arrived items to your list of data (the already listed 5 items), and call notifyDatasetChanged() on your ListAdapter implementation.

Update
Here I share a sample activity which contains a list and a TextView at the bottom (inflated from stepping_list.xml), where the list initially contains 5 items, and at the bottom a button. When pressing the button, other 25 values get loaded into the list, and the button disappears.

For this we need the main layout, res/layout/stepping_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content">
    <TextView android:id="@+id/footer"
        android:layout_width="fill_parent" android:layout_height="60dp"
        android:background="@drawable/box"
        android:text="Lazy loading list in steps" android:textStyle="bold"
        android:gravity="center_vertical|center_horizontal"
        android:layout_alignParentBottom="true" />
    <ListView android:id="@+id/list"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:layout_alignParentTop="true" android:layout_above="@id/footer" />
</RelativeLayout>

For the Load more data button to always appear after the last item of the initial list (even if need to scroll to it), I put it into the list's item renderer layout. This way the list will have two item renderers.

The common renderer res/layout/row.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView" android:layout_alignParentRight="true"
    android:layout_width="fill_parent" android:layout_height="60dp"
    android:gravity="center_vertical|right" android:paddingRight="10dp"
    android:textSize="35dp"
    android:textColor="#2B78E4" />

is a simple TextView, and the renderer for the last item (of the initial list)
res/layout/row_with_button.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView android:id="@+id/textView"
        android:layout_alignParentRight="true"
        android:layout_width="fill_parent" android:layout_height="60dp"
        android:gravity="center_vertical|right" android:paddingRight="10dp"
        android:layout_weight="1" android:textColor="#2B78E4"
        android:textSize="35dp" />
    <Button android:id="@+id/loadbtn"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="1" android:text="Load more data"
        android:onClick="loadMoreData" />
</LinearLayout>

Finally the Activity class that connects these layouts:
SteppingListActivity.java:

public class SteppingListActivity extends Activity
{
    private MyAdapter adapter;
    private ArrayList<Integer> values;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stepping_list);

        //initialize the list of data which will populate the list
        //TODO: You need to retrieve this data from the server, but I use
        //      here simple int values.
        values = new ArrayList<Integer>();
        for (int i = 0; i < 5; i++)
        {
            values.add((i % 2 == 0) ? i * 3 : i + 3);
        }

        //initialize the adapter, and attach it to the ListView:
        adapter = new MyAdapter();
        final ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(adapter);
    }

    /**
     * The onClick function of the last itemrenderer's button
     * @param button the button clicked.
     */
    public void loadMoreData(View button)
    {
        //Just put some more data into the values ArrayList:
        //TODO: You need to retrieve these data from the server, as well!
        for (int i = 5; i < 30; i++)
        {
            values.add((i % 2 == 0) ? i * 3 : i + 3);
        }
        //notify the ListAdapter about the changes:
        adapter.notifyDataSetChanged();
    }

    /**
     * The custom ListAdapter class used to populate the ListView
     */
    private class MyAdapter extends BaseAdapter
    {
        private LayoutInflater inflater;

        public MyAdapter()
        {
            inflater = LayoutInflater.from(SteppingListActivity.this);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            //check if the current item to show is the last item of the 
            //  initial list, and if so, inflate the proper renderer for it:
            if ((position == 4) && (values.size() == 5))
                convertView = inflater.inflate(
                        R.layout.row_with_button, parent, false);
            else if ((convertView == null) || 
                    (convertView.findViewById(R.id.loadbtn) != null))
                convertView = inflater.inflate(R.layout.row, parent, false);
            //set the value of the TextView 
            ((TextView) convertView.findViewById(
                    R.id.textView)).setText(values.get(position)+ ".50 €");

            return convertView;
        }
        @Override
        public int getCount()
        {
            return values.size();
        }
        @Override
        public Object getItem(int position)
        {
            return values.get(position);
        }
        @Override
        public long getItemId(int position)
        {
            return position;
        }
    }
}

I hope you got the idea :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜