5

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

主要活动

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

我的列表适配器

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?我尝试使用 anAsyncTask但无法使其正常工作。


编辑AsyncTask:在MainActivity之后添加以下内容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!");
  }
}
4

3 回答 3

6

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

适配器.notifyDataSetChanged()

例如:

数据库.插入(数据);

myAdapter.notifyDataSetChanged();

或者:

myAdapter.mySetNewContentMethod(someNewContent);
myAdapter.notifyDataSetChanged();
于 2014-02-18T21:21:04.460 回答
3

更新您的适配器调用后

myAdapter.notifyDataSetChanged();

这将动态更新列表视图。

有关更多信息,请参阅此链接

于 2013-08-23T05:59:30.493 回答
0

当您从中删除项目时,我尝试了最简单的方法来更新列表。希望我的代码对您有所帮助。代码

   //Custom Adatpter
   package com.example.smsptclapp;
   import java.util.ArrayList;
   import java.util.List;
   import android.app.Activity;
   import android.content.Context;
   import android.content.Intent;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.View.OnClickListener;
   import android.view.ViewGroup;
   import android.widget.AdapterView;
   import android.widget.BaseAdapter;
   import android.widget.ImageView;
   import android.widget.TextView;
   import android.widget.Toast;
   import android.widget.AdapterView.OnItemLongClickListener;

   public class CustomAdapter extends BaseAdapter
    {
 static List<String> result;
 //ArrayList<SingleRow> list;
 Activity context;
 static TextView tv;
 static int k,count;
     // int [] imageId; //for Image
     private static LayoutInflater inflater = null;
    public CustomAdapter(Activity activity, List<String> prgmNameList) 
     {
       // TODO Auto-generated constructor stub
        result = prgmNameList;
        context = activity;
       //imageId = prgmImages; //for Image 
       inflater = ( LayoutInflater )context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
     }

     public CustomAdapter(OnClickListener onClickListener, List<String>  listItems) 
     {
    // TODO Auto-generated constructor stub
     }

 @Override
     public int getCount() 
     {
      // TODO Auto-generated method stub
      return result.size();
     }

     @Override
     public Object getItem(int position) 
     {
       // TODO Auto-generated method stub
       return position;
     }

     @Override
     public long getItemId(int position) 
     {
       // TODO Auto-generated method stub
       return position;
     }

     public static int getCounter()
    {
      return count;
    }

    public static int getPosition()
   {
     return k;
   }

   @Override
   public View getView(final int position, View convertView, ViewGroup   parent) 
   {
     // TODO Auto-generated method stub
     //Object cd1=   getItem(position);
     View rowView;       
     rowView = inflater.inflate(R.layout.program_list, null);
     tv=(TextView) rowView.findViewById(R.id.textView1);
     tv.setLongClickable(true);
     tv.setText(result.get(position));
      rowView.setOnClickListener(new OnClickListener() 
      {
         @Override
         public void onClick(View v) {
            k = position;
            Intent i =  new Intent(context, Menu.class);
            context.startActivity(i);
         }
         });
     return rowView;
    }
 }

 // In Main Type these lines
       ss =listItems.get(i);  // where 'ss' is global string, 'i' is the location you want to delete, and 'listItems' is global variable of 'List<String>' type. 
       db.deleteUser(ss);  //'db' is global variable of database and deleteUser is method to delete entry from data base only 
   listItems.remove(i); //to remove the entry from list i.e to refresh it you must call this
   listgroups.setAdapter(new CustomAdapter(this,listItems)); //'listgroups' is 'ListView', which is also global.
于 2016-03-22T07:09:25.337 回答