如何动态更新与自定义适配器的ListView? [英] How to dynamically update a ListView with custom adapter?
问题描述
我有一个创建一个的ListView
和自定义适配器
主要活动。我能够填充的ListView
如果我已经预先创建,但列表如何填充它与动态获取的数据?
MainActivity
公共类MainActivity延伸活动{
私人的ListView myListView;
私人上下文CTX;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.MainActivity);
CTX =这一点;
名单<项目> myList上=新的ArrayList<项目>();
myList.add(新项目(名1,说明1));
myList.add(新项目(名2,说明2));
myList.add(新项目(3名,说明3));
myList.add(新项目(4名,说明4));
myList.add(新项目(5名,说明5));
myListView =(ListView控件)findViewById(R.id.list);
MyListAdapter myAdapter =新MyListAdapter(CTX,R.layout.listitem,myList中);
myListView.setAdapter(myAdapter);
}
}
MyListAdapter
公共类MyListAdapter扩展ArrayAdapter<项目> {
私人诠释的资源;
私人LayoutInflater mLayoutInflater;
公共MyListAdapter(上下文CTX,INT RESOURCEID,列表和LT;项目>对象){
超(CTX,RESOURCEID,对象);
资源= RESOURCEID;
mLayoutInflater = LayoutInflater.from(CTX);
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
convertView =(RelativeLayout的)mLayoutInflater.inflate(资源,NULL);
项目项目=(产品)的getItem(位置);
TextView的txtName的=(的TextView)convertView.findViewById(R.id.listName);
txtName.setText(item.getName());
TextView的txtDesc =(TextView中)convertView.findViewById(R.id.listDescription);
txtDesc.setText(item.getDesc());
返回convertView;
}
}
项目
公共类项目{
私人字符串名称;
私人字符串说明;
公共项目(字符串名称,字符串DESC){
超();
this.name =名称;
this.desc =说明;
}
// getter和setter
}
如何修改我的code,使得在后台函数检索项自定义适配器和填充的ListView?我试图使用的AsyncTask
,但不能设法得到它的工作。
修改:添加了以下的AsyncTask
在 MainActivity
在的onCreate()
只是为了测试的事情了。它的工作中,我可以看到的计数从1到5,然后完成。
我如何获得的AsyncTask
来更新我的适配器和的ListView
虽然?
我应该的onCreate()
传递给的AsyncTask
又该的AsyncTask
返回?
私有类GetItems扩展的AsyncTask<太虚,整型,太虚> {
@覆盖
在preExecute保护无效(){
super.on preExecute();
TextView的myMsg =(TextView中)findViewById(R.id.topMsg);
myMsg.setText(载入中...);
}
@覆盖
保护无效doInBackground(虚空...... PARAMS){
TextView的myMsg =(TextView中)findViewById(R.id.topMsg);
的for(int i = 1; I< = 5;我++){
尝试 {
视频下载(1000);
publishProgress(ⅰ);
}赶上(InterruptedException异常E){
}
}
返回null;
}
保护无效onProgressUpdate(整数...值){
TextView的myMsg =(TextView中)findViewById(R.id.topMsg);
myMsg.setText(Integer.toString(值[0] .intValue()));
}
@覆盖
保护无效onPostExecute(无效的结果){
TextView的myMsg =(TextView中)findViewById(R.id.topMsg);
myMsg.setText(完成!);
}
}
当你添加新的数据,你应该这样做:
adapter.notifyDataSetChanged()
例如:
database.insert(数据);
myAdapter.notifyDataSetChanged();
I have a main activity that creates a ListView
and a Custom Adapter
. I'm able to populate the ListView
if I have the List already created beforehand, but how do I populate it with dynamically fetched data?
MainActivity
public class MainActivity extends Activity {
private ListView myListView;
private Context ctx;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.MainActivity);
ctx = this;
List<Items> myList = new ArrayList<Items>();
myList.add(new Item("Name 1", "Desc 1"));
myList.add(new Item("Name 2", "Desc 2"));
myList.add(new Item("Name 3", "Desc 3"));
myList.add(new Item("Name 4", "Desc 4"));
myList.add(new Item("Name 5", "Desc 5"));
myListView = (ListView)findViewById(R.id.list);
MyListAdapter myAdapter = new MyListAdapter(ctx,R.layout.listitem, myList);
myListView.setAdapter(myAdapter);
}
}
MyListAdapter
public class MyListAdapter extends ArrayAdapter<Items> {
private int resource;
private LayoutInflater mLayoutInflater;
public MyListAdapter ( Context ctx, int resourceId, List<Items> objects) {
super( ctx, resourceId, objects );
resource = resourceId;
mLayoutInflater = LayoutInflater.from( ctx );
}
@Override
public View getView ( int position, View convertView, ViewGroup parent ) {
convertView = ( RelativeLayout ) mLayoutInflater.inflate( resource, null );
Items item = (Items) getItem( position );
TextView txtName = (TextView) convertView.findViewById(R.id.listName);
txtName.setText(item.getName());
TextView txtDesc = (TextView) convertView.findViewById(R.id.listDescription);
txtDesc.setText(item.getDesc());
return convertView;
}
}
Item
public class Item {
private String name;
private String desc;
public Item(String name, String desc) {
super();
this.name = name;
this.desc = desc;
}
//getters and setters
}
How can I modify my code such that a function in the background retrieves the items to the custom adapter and populates the ListView? I tried using an AsyncTask
but couldn't manage to get it to work.
Edit: Added the following AsyncTask
in MainActivity
after onCreate()
just to test things out. It's working in that I can see the counting from 1 to 5 and then done.
How do I get the AsyncTask
to update my adapter and ListView
though?
What should onCreate()
pass to the AsyncTask
and what should AsyncTask
return?
private class GetItems extends AsyncTask<Void, Integer, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
TextView myMsg = (TextView)findViewById(R.id.topMsg);
myMsg.setText("Loading...");
}
@Override
protected Void doInBackground(Void... params) {
TextView myMsg = (TextView)findViewById(R.id.topMsg);
for (int i=1;i<=5;i++) {
try {
Thread.sleep(1000);
publishProgress(i);
} catch (InterruptedException e) {
}
}
return null;
}
protected void onProgressUpdate(Integer... values) {
TextView myMsg = (TextView)findViewById(R.id.topMsg);
myMsg.setText(Integer.toString(values[0].intValue()));
}
@Override
protected void onPostExecute(Void result) {
TextView myMsg = (TextView)findViewById(R.id.topMsg);
myMsg.setText("Done!");
}
}
Whenever you add new data you should do this:
adapter.notifyDataSetChanged()
For example:
database.insert(data);
myAdapter.notifyDataSetChanged();
这篇关于如何动态更新与自定义适配器的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!