gpt4 book ai didi

Android:如何清除自定义 ListView 中的所有项目

转载 作者:太空狗 更新时间:2023-10-29 15:48:25 26 4
gpt4 key购买 nike

我知道可以通过对 arrayadapter 等调用“清除”来清除普通 ListView 。但是如果它是自定义 ListView ,我们如何清除项目??

这是我的代码:

Activity :

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

}

请为此提供解决方案。

最佳答案

您需要调用notifyDataSetChanged()清除基础列表后在适配器上。您的 ListView 会将自己注册为观察者并对调用使用react。

public void clearlistview()
{
items.clear();
this.notifyDataSetChanged();
}

关于Android:如何清除自定义 ListView 中的所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10195237/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com