在android编程中选中复选框时,使用警报框显示从给定的自定义列表视图中删除行的代码
Posted
技术标签:
【中文标题】在android编程中选中复选框时,使用警报框显示从给定的自定义列表视图中删除行的代码【英文标题】:Code to Delete row from given customized listview using alertbox displayed when checkbox is checked in android programming 【发布时间】:2015-09-11 16:10:59 【问题描述】:这是我下面的代码 1)这是具有getter和setter方法的类+基本适配器的扩展
package virtual.mall;
import java.util.List;
import virtual.mall.Salt_page_ada.saltlist_holder;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
class Order1_cls
String o1_it_name,o1_it_count;
int o1_it_cost,o1_it_qty;
Boolean o1isselect=false;
public Order1_cls(String o1_it_name,String o1_it_count,int o1_it_qty,int o1_it_cost)
super();
this.o1_it_name = o1_it_name;
this.o1_it_count=o1_it_count;
this.o1_it_qty=o1_it_qty;
this.o1_it_cost = o1_it_cost;
this.o1isselect=o1isselect;
public String getO1_it_name()
return o1_it_name;
public void setO1_it_name(String o1_it_name)
this.o1_it_name = o1_it_name;
public String getO1_it_count()
return o1_it_count;
public void setO1_it_count(String o1_it_count)
this.o1_it_count = o1_it_count;
public int getO1_it_cost()
return o1_it_cost;
public void setO1_it_cost(int o1_it_cost)
this.o1_it_cost = o1_it_cost;
public int getO1_it_qty()
return o1_it_qty;
public void setO1_it_qty(int o1_it_qty)
this.o1_it_qty = o1_it_qty;
public Boolean getO1isselect()
return o1isselect;
public void setO1isselect(Boolean o1isselect)
this.o1isselect = o1isselect;
public class Order1_page extends ArrayAdapter<Order1_cls>
private List <Order1_cls> order1list;
private Context context;
public Order1_page(List<Order1_cls> order1list, Context context)
super(context, R.layout.order1listref,order1list);
this.order1list=order1list;
this.context=context;
public static class Order1list_holder
public TextView o1_name;
public TextView o1_count;
public TextView o1_qty;
public TextView o1_cost;
public CheckBox o1chkbx;
public View getView(int pos,View con_view,ViewGroup parent)
View v=con_view;
Order1list_holder hold=new Order1list_holder();
if(con_view==null)
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=inflater.inflate(R.layout.order1listref,null);
hold.o1_name=(TextView)v.findViewById(R.id.o1_tv_name);
hold.o1_count=(TextView)v.findViewById(R.id.o1_tv_count);
hold.o1_qty=(TextView)v.findViewById(R.id.o1_tv_qty);
hold.o1_cost=(TextView)v.findViewById(R.id.o1_tv_cost);
hold.o1chkbx=(CheckBox)v.findViewById(R.id.o1_cBox);
hold.o1chkbx.setOnCheckedChangeListener((Order1class) context);
else
hold=(Order1list_holder)v.getTag();
Order1_cls o1= order1list.get(pos);
//hold.o1_name.setText(o1.getO1_it_name());
hold.o1_count.setText(o1.getO1_it_count());
hold.o1_qty.setText(" "+o1.getO1_it_qty());
hold.o1_cost.setText(" " + o1.getO1_it_cost());
hold.o1chkbx.setChecked(o1.o1isselect);
hold.o1chkbx.setTag(o1);
return v;
2) 这是使用处理程序扩展活动的主类的代码 我已经在下面编写了删除代码,但它显示了一个异常
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
public class Order1class extends Activity implements android.widget.CompoundButton.OnCheckedChangeListener
ListView lv;
ArrayList<Order1_cls> order1list; //class name in ref java file
Order1_page o1adapter; // ref xml name
ArrayList<Integer> positionslist;
ImageButton addc,delbtn;
protected Object posi;
final Context context = this;
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.order1pageclass);
positionslist=new ArrayList<Integer>();
lv=(ListView)findViewById(R.id.order1class_listv); //listview id in main layout
disp_ord1list();
private void disp_ord1list()
// TODO Auto-generated method stub
order1list =new ArrayList<Order1_cls>();
order1list.add(new Order1_cls("Rice(basmati)","1kg packet",3,450));
order1list.add(new Order1_cls("Lentils(finegrade)","2kg packet",2,500));
order1list.add(new Order1_cls("Colgate","5kg packet",3,60));
order1list.add(new Order1_cls("cereal","1kg packet",3,60));
order1list.add(new Order1_cls("surfexcel","2kg packet",2,40));
order1list.add(new Order1_cls("vim","200gms Bar",1,25));
order1list.add(new Order1_cls("Flour","5kg packet",1,300));
o1adapter=new Order1_page(order1list,this);
lv.setAdapter(o1adapter);
@Override
public void onCheckedChanged(CompoundButton but_view, boolean o1isselected)
// TODO Auto-generated method stub
int posi=lv.getPositionForView(but_view);
if(posi !=ListView.INVALID_POSITION)
Order1_cls o1=order1list.get(posi);
o1.setO1isselect(o1isselected);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("remove item ??");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to delete")
.setCancelable(false)
.setPositiveButton("Delete",new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog,int id)
for(int position : positionslist)
order1list.remove(position);
o1adapter.notifyDataSetChanged();
)
.setNegativeButton("add",new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog,int id)
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
);
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
// Toast.makeText(this,"CLICKED ON " + o1.getO1_it_name()+"of count "+ o1.getO1_it_count()+"cost is "+o1.getO1_it_cost(),Toast.LENGTH_SHORT).show();
3) 实际页面布局的 order1pageclass.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_ >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#58ACFA"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_
android:layout_
android:text="Order-1 27/3/15"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25dp" />
</LinearLayout>
<ListView
android:id="@+id/order1class_listv"
android:layout_
android:layout_
android:layout_above="@+id/lay1"
android:layout_alignParentLeft="true"
android:layout_below="@+id/linearLayout1"
android:background="#FAFAFA" >
</ListView>
<LinearLayout
android:id="@+id/lay1"
android:layout_
android:layout_
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#58ACFA"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/imageButton3"
android:layout_
android:layout_
android:src="@drawable/menu_btn"
android:background="#58ACFA" />
<ImageButton
android:id="@+id/imageButton4"
android:layout_
android:layout_
android:layout_weight="0.65"
android:src="@drawable/home_btn"
android:background="#58ACFA"
/>
<ImageButton
android:id="@+id/imageButton5"
android:layout_
android:layout_
android:src="@drawable/cart_btn"
android:background="#58ACFA" />
</LinearLayout>
</RelativeLayout>
4) 参考页面布局(每行的外观或必须具有的内容)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_ >
<TextView
android:id="@+id/o1_tv_name"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:text="Rice(Basmati)"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="@+id/o1_cBox"
android:layout_
android:layout_
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_marginRight="36dp"
android:text="+ Cart" />
<TextView
android:id="@+id/o1_tv_count"
android:layout_
android:layout_
android:layout_alignLeft="@+id/o1_tv_name"
android:layout_below="@+id/o1_tv_name"
android:layout_margin="5dp"
android:text="5kg Pack"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp" />
<TextView
android:id="@+id/o1_tv_qty"
android:layout_
android:layout_
android:layout_alignBaseline="@+id/o1_tv_count"
android:layout_alignBottom="@+id/o1_tv_count"
android:layout_margin="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="28dp"
android:layout_toLeftOf="@+id/o1_cBox"
android:text="3"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp" />
<TextView
android:id="@+id/tvqty"
android:layout_
android:layout_
android:layout_alignBaseline="@+id/o1_tv_qty"
android:layout_alignBottom="@+id/o1_tv_qty"
android:layout_margin="5dp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/o1_tv_name"
android:text="Qty :"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp" />
<TextView
android:id="@+id/o1_tv_cost"
android:layout_
android:layout_
android:layout_alignBaseline="@+id/o1_tv_qty"
android:layout_alignBottom="@+id/o1_tv_qty"
android:layout_alignRight="@+id/o1_cBox"
android:layout_margin="5dp"
android:text="450 "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp" />
</RelativeLayout>
【问题讨论】:
无法删除按钮单击时的行。你也需要xml吗?我只需要代码来删除按钮单击时选定的行 如果你想从列表视图中删除项目,然后为列表视图本身编写 onItemClickListener。 ` final int position = lv.getPositionForView((View) v.getParent());` 在此代码中,传递给它的视图必须是适配器项或适配器项的后代。 不,实际上我每行都有复选框..我想删除带有复选框的行..当我点击另一个按钮时 @boss ..这就是我真正困惑的地方,你能不能把那些对你太好的代码行写出来 【参考方案1】:在您的onCheckedChanged()
方法中,将位置保存在列表中。并在单击按钮时遍历列表并将它们从适配器列表中删除。
【讨论】:
我应该再创建一个班级职位列表吗?如果是这样,在哪个文件中? @唠叨 No.. 只是一个列表(例如:ArrayList)来存储检查视图的位置。以上是关于在android编程中选中复选框时,使用警报框显示从给定的自定义列表视图中删除行的代码的主要内容,如果未能解决你的问题,请参考以下文章