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

时间:2013-08-23 05:57:57

标签: android listview android-listview adapter

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

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

物品

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,但无法让它发挥作用。


修改:在AsyncTaskMainActivity添加了以下onCreate(),以便对其进行测试。它正在工作,我可以看到计数从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!");
  }
}

3 个答案:

答案 0 :(得分:6)

每当您添加新数据时,您都应该这样做:

  

adapter.notifyDataSetChanged()

例如:

  

database.insert(数据);

     

myAdapter.notifyDataSetChanged();

OR:

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

答案 1 :(得分:2)

更新适配器调用后

myAdapter.notifyDataSetChanged();

这将动态更新列表视图。

有关详情LINK

,请参阅此处

答案 2 :(得分:0)

当您从中删除项目时,我尝试了最简单的方法来更新列表。希望我的代码可以帮到你。 代码

NSNotificationCenter.defaultCenter().addObserver(self, #selector(goBack), name: "your notification name", object: nil)