2

我知道可以通过调用 'clear' 到 arrayadapter 等来清除正常的列表视图。但是如果它是自定义列表视图,我们如何清除项目?

这是我的代码:

活动:

public class GroupList extends Activity{
    TextView v_no_groups;
    Cursor grouplist_cur;
    Context context = GroupList.this;
    SQLiteDatabase mydb=null;
    ListView v_grouplist;
    ArrayList<String> grpname_arr = new ArrayList<String>();
    ArrayList<String> grpid_arr = new ArrayList<String>();
    ArrayList<Integer> memcount_arr = new ArrayList<Integer>();
    ArrayList<Integer> totaltrans_arr = new ArrayList<Integer>();
    M_grplist_adapter mga;
    Button v_creategrp;
    List<M_grplist_class> glist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grouplist);
    }

    @Override
    protected void onResume() {

        super.onResume();

        //mga.clearlistview();

        v_creategrp = (Button)findViewById(R.id.x_creategrp);
        v_grouplist = (ListView)findViewById(android.R.id.list);
        registerForContextMenu(v_grouplist);
        v_no_groups = (TextView)findViewById(R.id.x_no_groups);

        mydb = this.openOrCreateDatabase("moneydb", 0, null);
        mydb.execSQL("CREATE TABLE IF NOT EXISTS grouptable(grpname varchar(30),grpid varchar(10),memcount number(3),totaltrans number(3))");   
        grouplist_cur = mydb.rawQuery("SELECT * from grouptable", null);

        if(grouplist_cur.getCount()>0)
        {

            grouplist_cur.moveToFirst();
            do{
                grpname_arr.add(grouplist_cur.getString(0));
                memcount_arr.add(grouplist_cur.getInt(2));
                totaltrans_arr.add(grouplist_cur.getInt(3));
            }while(grouplist_cur.moveToNext());

            glist = new ArrayList<M_grplist_class>();
            for(int i=grpname_arr.size()-1;i>=0;i--){
                glist.add(new M_grplist_class(grpname_arr.get(i),memcount_arr.get(i),totaltrans_arr.get(i)));
            }

            mga = new M_grplist_adapter(context,glist);
            v_grouplist.setAdapter(mga);

            grouplist_cur.close();
            mydb.close();
        }
        else
        {
            grouplist_cur.close();
            mydb.close();
        }

        v_creategrp.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                Intent int_newgrp = new Intent(context,NewGroup.class);
                startActivity(int_newgrp);
            }
        });
    }

grplist_adapter 类:

public class M_grplist_adapter extends BaseAdapter{
    private List<M_grplist_class> items = null;
    private Context context;
    private int index;

    public M_grplist_adapter(Context context,List<M_grplist_class> items){
        items.clear();
        notifyDataSetChanged();
        this.items = items;
        this.context = context;
        //notifyDataSetChanged();
        Log.i("Dillu", "you are here M_grplist_adapter");
    }

    @Override
    public int getCount() {
        Log.i("Dillu", "you are here getcount");
        return items.size();

    }

    @Override
    public Object getItem(int position) {
        index = position;
        Log.i("Dillu", "you are here getitem");
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        Log.i("Dillu", "you are here getitemid");
        return position;
    }

    public String clear()
    {
        String name = items.get(index).getgrpname();
        items.remove(index);
        notifyDataSetChanged();
        return name;
    }

    @Override
    public View getView(int index, View convertView, ViewGroup parent) {
        Log.i("Dillu", "you are here getview");
        LinearLayout rowLayout=null;
        if(convertView==null) 
            rowLayout = (LinearLayout)LayoutInflater.from(context).inflate(R.layout.m_grplist_item,parent,false);
        else
            rowLayout = (LinearLayout)convertView;  

        TextView gname = (TextView)rowLayout.findViewById(R.id.grpname);
        gname.setText(items.get(index).getgrpname());

        TextView gmems = (TextView)rowLayout.findViewById(R.id.noofmems);
        int int_temp = items.get(index).getmemcount();
        String str_temp = Integer.toString(int_temp);
        gmems.setText(str_temp);

        TextView gtrans = (TextView)rowLayout.findViewById(R.id.nooftrans);
        int int_temp_1 = items.get(index).gettotaltrans();
        String str_temp_1 = Integer.toString(int_temp_1);
        gtrans.setText(str_temp_1);

        return rowLayout;
    }

}

M_grplist_class :

public class M_grplist_class {
    String loc_grpname;
    int loc_memcount;
    int loc_totaltrans;

     public M_grplist_class(String grpname,int memcount,int totaltrans){
         loc_grpname = grpname;
         loc_memcount = memcount;
         loc_totaltrans = totaltrans; 
     }

     public String getgrpname(){
         return loc_grpname;
     }

     public int getmemcount(){
         return loc_memcount;
     }

     public int gettotaltrans(){
         return loc_totaltrans;
     }

}

请为此提供解决方案。

4

4 回答 4

7

清除基础列表后,您需要在适配器上调用notifyDataSetChanged() 。ListView将自己注册为观察者并对该调用作出反应。

public void clearlistview()
{
    items.clear();
    this.notifyDataSetChanged();
}
于 2012-04-17T16:36:22.057 回答
3

只需使用 listView.setAdapter(null);

这对我有用。

于 2016-07-16T18:49:39.850 回答
0

据我所知,无需清除适配器并将 notifyDataSetChanged() 设置为适配器。你已经使用 List,ArrayList 为项目或数组 1 st 清除它并重新初始化它,并将数据传递给服装适配器后清除使用的数组/列表。它会正常工作!

于 2014-12-04T02:47:08.693 回答
0

在您的代码中,您必须清除“glist”,即 glist.clear 但您不应调用 mga.clearlistview();

glist 是项目的原始数组列表,其中“项目”只是 glist 的副本。

有关自定义适配器的更多详细信息,请参阅此链接。

链接1

于 2012-04-17T17:37:21.167 回答