android listview 自定义适配器,android – 如何使用自定义适配器动态更新ListView?...

该博客探讨如何在Android中使用AsyncTask从后台获取数据并动态填充ListView。示例代码展示了如何创建一个自定义适配器,以及如何在主线程中设置ListView。问题在于如何在AsyncTask中正确地更新ListView,作者尝试了在onProgressUpdate和onPostExecute方法中进行更新,但未能成功实现。
摘要由CSDN通过智能技术生成

我有一个主要活动,创建一个ListView和一个自定义适配器.如果事先已经创建了List,我可以填充ListView,但是如何使用动态获取的数据填充它?

主要活动

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 myList = new ArrayList();

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 {

private int resource;

private LayoutInflater mLayoutInflater;

public MyListAdapter ( Context ctx, int resourceId, List 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;

}

}

项目

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

}

如何修改我的代码,以便后台的函数将项目检索到自定义适配器并填充ListView?我尝试使用AsyncTask但无法让它工作.

编辑:在onCreate()之后在MainActivity中添加以下AsyncTask只是为了测试.它正在工作,我可以看到从1到5的计数,然后完成.

如何让AsyncTask更新我的适配器和ListView呢?

onCreate()应该传递给AsyncTask以及AsyncTask应该返回什么?

private class GetItems extends AsyncTask {

@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!");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值