如何使用自定义适配器动态更新 ListView? [英] How to dynamically update a ListView with custom adapter?

查看:43
本文介绍了如何使用自定义适配器动态更新 ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要活动,它创建了一个 ListView 和一个 Custom Adapter.如果我已经预先创建了列表,我可以填充 ListView,但是如何使用动态获取的数据填充它?

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?

主活动

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;
  }
}

项目

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 但无法让它工作.

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:在 onCreate() 之后的 MainActivity 中添加以下 AsyncTask 只是为了测试一下.它的工作原理是我可以看到从 1 到 5 的计数然后完成.
我如何让 AsyncTask 更新我的适配器和 ListView?
onCreate() 应该传递什么给 AsyncTask 以及 AsyncTask 应该返回什么?

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()

adapter.notifyDataSetChanged()

例如:

database.insert(data);

database.insert(data);

myAdapter.notifyDataSetChanged();

myAdapter.notifyDataSetChanged();

或:

myAdapter.mySetNewContentMethod(someNewContent);
myAdapter.notifyDataSetChanged();

这篇关于如何使用自定义适配器动态更新 ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆