android - 如何使用自定义适配器动态更新 ListView?

标签 android listview android-listview adapter

我有一个创建 ListViewCustom Adapter 的主要 Activity 。如果我事先已经创建了 List,我可以填充 ListView,但是如何使用动态获取的数据填充它?

主 Activity

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


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

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

最佳答案

无论何时添加新数据,您都应该这样做:

adapter.notifyDataSetChanged()

例如:

database.insert(data);

myAdapter.notifyDataSetChanged();

或者:

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

关于android - 如何使用自定义适配器动态更新 ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18395753/

相关文章:

android - Listview 的非常奇怪的行为

android - 如何使用保存实例状态保存 Activity 状态?

listview - 如何使ListView中的文本居中?

android - Android Listview项目中的翻转动画

android - 当项目数量发生变化时,如何更新绑定(bind)到 ListView 的 CursorAdapter?

c# - 将 AutomationID 与 ListView 一起使用

android - 使用 buildozer 使用适用于 Android 的 numpy 库打包 Kivy 时出错

Android StaggeredGridLayoutManager 偏移错误

java - 如何在Android中的EditText中移动光标

android - ListView 项目选定状态不起作用