我在android中有一个列表。目前有30条记录。
但在我的 Activity 中,我只显示 5 条记录...5 条记录后,它向我显示一个按钮
“显示所有数据”
当我单击该按钮时,它应该显示所有 30 条记录并更新 Activity 列表。请告诉我如何更新。就像我们在 Web 技术中的 AJAX 中所做的那样。我希望你们明白我想说的是什么?
刷新 Activity 而不刷新整个 Activity。请 friend 们回复。
等待积极回应。
最佳答案
您只需将新到达的项目添加到数据列表中(已列出的 5 个项目),然后在 ListAdapter
实现上调用 notifyDatasetChanged()
即可。
更新
在这里,我分享了一个示例 Activity ,其中包含一个列表和底部的 TextView
(从 stepping_list.xml
扩充),其中列表最初包含 5 个项目,并且在底部一个按钮。按下按钮时,其他 25 个值将加载到列表中,然后按钮消失。
为此,我们需要主布局,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>
为了让加载更多数据按钮始终出现在初始列表的最后一个项目之后(即使需要滚动到它),我将其放入列表的项目渲染器布局中。这样列表将有两个项目渲染器。
通用渲染器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" />
是一个简单的TextView
,以及(初始列表中)最后一项的渲染器
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>
最后是连接这些布局的 Activity
类:
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;
}
}
}
我希望你明白了:)
关于android - 如何在android中更新列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5977551/