应用程序崩溃,当我接触列表视图上点击复选框当我、视图、应用程序、复选框

2023-09-07 09:54:03 作者:清故宸凉

我是新的Andr​​oid开发者,我想使用复选框,但尚未当我点击任何复选框应用程序是坠毁我使用的是自定义适配器PLZ任何一个可以帮助我

选择列表视图中的多个联系人

我contactList活动是继

 公共类CustomAdapter扩展ArrayAdapter<&PHONELIST GT; {    INT inflatr;    上下文ctxt;    ArrayList的<&PHONELIST GT;数据=新的ArrayList<&PHONELIST GT;();    公共CustomAdapter(上下文的背景下,INT资源的ArrayList<&PHONELIST GT; ARR){        超(背景下,资源,ARR);        this.inflatr =资源;        this.ctxt =背景;        this.data = ARR;    }    @覆盖    众长getItemId(INT位置){        返回的位置;    }    @覆盖    公共查看getView(INT位置,查看convertView,父母的ViewGroup){        UserHolder支架=无效;         查看排= convertView;        如果(convertView == NULL)        {             LayoutInflater吹气=((活动)ctxt).getLayoutInflater();             行= inflater.inflate(inflatr,父母,假);             持有人=新UserHolder();             holder.textName =(TextView中)row.findViewById(R.id.lblName);             holder.stnumber =(TextView中)row.findViewById(R.id.mobilenum);             holder.checked =(复选框)row.findViewById(R.id.check);// holder.btnEdit =(的ImageButton)row.findViewById(R.id.atomPay_removePay);             row.setTag(保持器);        }        其他        {             支架=(UserHolder)row.getTag();        }         PHONELIST DTA = data.get(位置);         holder.checked.setId(位置);        Toast.makeText(ctxtHumayoon zoooommmm+ dta.getNumber(),Toast.LENGTH_SHORT).show();        的for(int i = 1; I< = 256;我++)        {            holder.textName.setText(dta.getName());        }        对于(INT J = 1; J< = 256; J ++)        {            holder.stnumber.setText(dta.getNumber());        }// holder.btnEdit.setOnClickListener(新OnClickListener(){//// @覆盖//公共无效的onClick(视图v){// Toast.makeText(ctxtHumayoon Siddiqueeeeeeeeeeeeeeeeeee+ dta.getName(),Toast.LENGTH_SHORT).show();//意图moreIntent =新意图(的getContext(),ContactList.class);//串TNAME = dta.getName();// moreIntent.putExtra(模板,TNAME);// v.getContext()startActivity(moreIntent)。// // ctxt.startActivity(ctxt,ContactList.class);//}//});        返回行;    }    @覆盖    公众诠释的getCount(){// // TODO自动生成方法存根        返回data.size();    }    静态类UserHolder {        TextView的textName;        TextView的textAddress;        TextView的textLocation;        的ImageButton btnEdit;        按钮btnDelete;        TextView的stnumber;        选中复选框;        }} 

我的自定义适配器以下

 公共类CustomAdapter扩展ArrayAdapter<&PHONELIST GT; {    INT inflatr;    上下文ctxt;    ArrayList的<&PHONELIST GT;数据=新的ArrayList<&PHONELIST GT;();    公共CustomAdapter(上下文的背景下,INT资源的ArrayList<&PHONELIST GT; ARR){        超(背景下,资源,ARR);        this.inflatr =资源;        this.ctxt =背景;        this.data = ARR;    }    @覆盖    众长getItemId(INT位置){        返回的位置;    }    @覆盖    公共查看getView(INT位置,查看convertView,父母的ViewGroup){        UserHolder支架=无效;         查看排= convertView;        如果(convertView == NULL)        {             LayoutInflater吹气=((活动)ctxt).getLayoutInflater();             行= inflater.inflate(inflatr,父母,假);             持有人=新UserHolder();             holder.textName =(TextView中)row.findViewById(R.id.lblName);             holder.stnumber =(TextView中)row.findViewById(R.id.mobilenum);             holder.checked =(复选框)row.findViewById(R.id.check);// holder.btnEdit =(的ImageButton)row.findViewById(R.id.atomPay_removePay);             row.setTag(保持器);        }        其他        {             支架=(UserHolder)row.getTag();        }         PHONELIST DTA = data.get(位置);         holder.checked.setId(位置);        Toast.makeText(ctxtHumayoon zoooommmm+ dta.getNumber(),Toast.LENGTH_SHORT).show();        的for(int i = 1; I< = 256;我++)        {            holder.textName.setText(dta.getName());        }        对于(INT J = 1; J< = 256; J ++)        {            holder.stnumber.setText(dta.getNumber());        }// holder.btnEdit.setOnClickListener(新OnClickListener(){//// @覆盖//公共无效的onClick(视图v){// Toast.makeText(ctxtHumayoon Siddiqueeeeeeeeeeeeeeeeeee+ dta.getName(),Toast.LENGTH_SHORT).show();//意图moreIntent =新意图(的getContext(),ContactList.class);//串TNAME = dta.getName();// moreIntent.putExtra(模板,TNAME);// v.getContext()startActivity(moreIntent)。// // ctxt.startActivity(ctxt,ContactList.class);//}//});        返回行;    }    @覆盖    公众诠释的getCount(){// // TODO自动生成方法存根        返回data.size();    }    静态类UserHolder {        TextView的textName;        TextView的textAddress;        TextView的textLocation;        的ImageButton btnEdit;        按钮btnDelete;        TextView的stnumber;        选中复选框;        }} 

我PHONELIST类是..

 包com.example.sqliteapplication;公共类PHONELIST {    字符串名称;    串号;    公共PHONELIST(串串,串号){        超();        this.name =串;        this.number =号;    }    公共字符串的getName(){        返回名称;        }    公共无效setNumber(串号)    {        this.number =号;    }    公共字符串getNumber()    {        返回数;    }    公共无效setname可以(字符串名称){        this.name =名称;        }} 
监控应用

的列表视图XML布局

 <?XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android    机器人:方向=垂直的android:layout_width =FILL_PARENT    机器人:layout_height =FILL_PARENT    机器人:背景=@布局/ bg_player_header    >    < ListView控件的android:layout_width =match_parent             机器人:分=#FFEEEE        机器人:layout_height =match_parent机器人:ID =@ + ID / lvContacts>< /&的ListView GT;< / LinearLayout中> 

在列表视图中的每一行的XML布局

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android    的xmlns:工具=htt​​p://schemas.android.com/tool​​s    机器人:layout_width =match_parent    机器人:layout_height =match_parent    机器人:方向=垂直>    <的LinearLayout        机器人:ID =@ + ID / player_header_bg        机器人:layout_width =FILL_PARENT        机器人:layout_height =60dip        机器人:背景=@布局/ bg_player_header>        < ImageView的            机器人:ID =@ + ID / imageView1            机器人:layout_width =32dip            机器人:layout_height =40dip            机器人:layout_marginTop =10dip            机器人:SRC =@绘制/面>        < / ImageView的>        <的TextView            机器人:ID =@ + ID / lblName            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:layout_marginLeft =15dip            机器人:layout_weight =1            机器人:paddingLeft =5DP            机器人:文字=说明            机器人:文字颜色=#04b3d2            机器人:TEXTSIZE =16DP            机器人:文字样式=大胆/>        <的TextView            机器人:ID =@ + ID / mobilenum            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:layout_marginLeft =15dip            机器人:layout_weight =1            机器人:paddingLeft =5DP            机器人:文字=03455039469            机器人:文字颜色=#04b3d2            机器人:TEXTSIZE =16DP            机器人:文字样式=大胆/>        <复选框            机器人:ID =@ + ID /勾选            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =FILL_PARENT            机器人:layout_alignParentRight =真            机器人:可聚焦=假            机器人:SRC =@绘制/形状            />    < / LinearLayout中>< / LinearLayout中> 

错误是继....

  13 05-12:30:32.432:E / AndroidRuntime(25447):致命异常:主要05-12 13:30:​​32.432:E / AndroidRuntime(25447):$ android.content.res.Resources NotFoundException:无法找到资源编号为0x405-12 13:30:​​32.432:E / AndroidRuntime(25447):在android.content.res.Resources.getResourceEntryName(Resources.java:1756)05-12 13:30:​​32.432:E / AndroidRuntime(25447):在android.view.View $ 1.onClick(View.java:3590)05-12 13:30:​​32.432:E / AndroidRuntime(25447):在android.view.View.performClick(View.java:4211)05-12 13:30:​​32.432:E / AndroidRuntime(25447):在android.widget.CompoundButton.performClick(CompoundButton.java:100)05-12 13:30:​​32.432:E / AndroidRuntime(25447):在android.view.View $ PerformClick.run(View.java:17446)05-12 13:30:​​32.432:E / AndroidRuntime(25447):在android.os.Handler.handleCallback(Handler.java:725)05-12 13:30:​​32.432:E / AndroidRuntime(25447):在android.os.Handler.dispatchMessage(Handler.java:92)05-12 13:30:​​32.432:E / AndroidRuntime(25447):在android.os.Looper.loop(Looper.java:153)05-12 13:30:​​32.432:E / AndroidRuntime(25447):在android.app.ActivityThread.main(ActivityThread.java:5299)05-12 13:30:​​32.432:E / AndroidRuntime(25447):在java.lang.reflect.Method.invokeNative(本机方法)05-12 13:30:​​32.432:E / AndroidRuntime(25447):在java.lang.reflect.Method.invoke(Method.java:511)05-12 13:30:​​32.432:E / AndroidRuntime(25447):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:833)05-12 13:30:​​32.432:E / AndroidRuntime(25447):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)05-12 13:30:​​32.432:E / AndroidRuntime(25447):在dalvik.system.NativeStart.main(本机方法) 

解决方案

为什么你在code改为复选框的ID?

  holder.checked.setId(位置); 

如果你想在你的复选框位置使用标签而不是 ID

您可以使用:

  holder.checked.setTag(位置); 

和获取是:

  INT位置=(整数)holder.checked.getTag(); 

如您更改$ C $复选框编号C你的资源$ NotFoundException 因为你改变复选框的编号1,2,3,4,...这不保存在R档,

//更新

多个选择项发生,因为listviews-recycling,为解决这一问题,你需要保存在一个地方选择的位置或者可以这样做:

添加以下属性到 PHONELIST

 布尔isSelected; 

创建getter和setter方法​​的。

 公共布尔getIsSelected(){  返回isSelected;}公共无效setIsSelected(布尔isSelected){   this.isSelected = isSelected;} 

然后在code在 getView 使用方法:

  holder.checked.setChecked(dta.getIsSelected()); 

isSelected 的复选框变化值的点击 isSelected

所以你的code必须是这样的:

  holder.checked.setOnClickListener(本); 

的onClick 方法:

  data.get((整数)v.getTag())setIsSelected(data.get((整数)v.getTag())getIsSelected!)。 notifyDataSetChanged(); 

I am new android Developer and i want to select multiple contacts in list view using check box but yet when i click any check box app is crashed I am Using a custom Adapter plz any one can help me

my contactList ACtivity is following

public class CustomAdapter extends ArrayAdapter<PhoneList> {
    int inflatr;
    Context ctxt;
    ArrayList<PhoneList> data=new ArrayList<PhoneList>();
    public CustomAdapter(Context context, int resource, ArrayList<PhoneList> arr) {

        super(context, resource, arr);
        this.inflatr = resource;
        this.ctxt = context;
        this.data= arr;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        UserHolder holder = null;
         View row = convertView;
        if(convertView==null)
        {
             LayoutInflater inflater = ((Activity) ctxt).getLayoutInflater();
             row = inflater.inflate(inflatr, parent, false);
             holder = new UserHolder();
             holder.textName=(TextView)row.findViewById(R.id.lblName);
             holder.stnumber=(TextView)row.findViewById(R.id.mobilenum);
             holder.checked=(CheckBox)row.findViewById(R.id.check);
//           holder.btnEdit = (ImageButton) row.findViewById(R.id.atomPay_removePay);
             row.setTag(holder);
        }
        else
        {
             holder = (UserHolder) row.getTag();            
        }


         PhoneList dta=data.get(position);
         holder.checked.setId(position);
        Toast.makeText(ctxt, "Humayoon    zoooommmm"+dta.getNumber(), Toast.LENGTH_SHORT).show();

        for(int i=1; i<=256; i++)
        {
            holder.textName.setText(dta.getName());
        }
        for(int j=1; j<=256; j++)
        {
            holder.stnumber.setText(dta.getNumber());
        }
//      holder.btnEdit.setOnClickListener(new OnClickListener() {
//          
//          @Override
//          public void onClick(View v) {
//              Toast.makeText(ctxt, "Humayoon    Siddiqueeeeeeeeeeeeeeeeeee"+dta.getName(), Toast.LENGTH_SHORT).show();
//              Intent moreIntent=new Intent(getContext(),ContactList.class);
//              String tName=dta.getName();
//              moreIntent.putExtra("Template",tName);
//              v.getContext().startActivity(moreIntent);
//               // ctxt.startActivity(ctxt,ContactList.class);
//          }
//      });
        return row;
    }
    @Override
    public int getCount() {
//      // TODO Auto-generated method stub
        return data.size();
    }

    static class UserHolder {
        TextView textName;
        TextView textAddress;
        TextView textLocation;
        ImageButton btnEdit;
        Button btnDelete;
        TextView stnumber;
        CheckBox checked;
        }


}

my custom adapter is following

public class CustomAdapter extends ArrayAdapter<PhoneList> {
    int inflatr;
    Context ctxt;
    ArrayList<PhoneList> data=new ArrayList<PhoneList>();
    public CustomAdapter(Context context, int resource, ArrayList<PhoneList> arr) {

        super(context, resource, arr);
        this.inflatr = resource;
        this.ctxt = context;
        this.data= arr;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        UserHolder holder = null;
         View row = convertView;
        if(convertView==null)
        {
             LayoutInflater inflater = ((Activity) ctxt).getLayoutInflater();
             row = inflater.inflate(inflatr, parent, false);
             holder = new UserHolder();
             holder.textName=(TextView)row.findViewById(R.id.lblName);
             holder.stnumber=(TextView)row.findViewById(R.id.mobilenum);
             holder.checked=(CheckBox)row.findViewById(R.id.check);
//           holder.btnEdit = (ImageButton) row.findViewById(R.id.atomPay_removePay);
             row.setTag(holder);
        }
        else
        {
             holder = (UserHolder) row.getTag();            
        }


         PhoneList dta=data.get(position);
         holder.checked.setId(position);
        Toast.makeText(ctxt, "Humayoon    zoooommmm"+dta.getNumber(), Toast.LENGTH_SHORT).show();

        for(int i=1; i<=256; i++)
        {
            holder.textName.setText(dta.getName());
        }
        for(int j=1; j<=256; j++)
        {
            holder.stnumber.setText(dta.getNumber());
        }
//      holder.btnEdit.setOnClickListener(new OnClickListener() {
//          
//          @Override
//          public void onClick(View v) {
//              Toast.makeText(ctxt, "Humayoon    Siddiqueeeeeeeeeeeeeeeeeee"+dta.getName(), Toast.LENGTH_SHORT).show();
//              Intent moreIntent=new Intent(getContext(),ContactList.class);
//              String tName=dta.getName();
//              moreIntent.putExtra("Template",tName);
//              v.getContext().startActivity(moreIntent);
//               // ctxt.startActivity(ctxt,ContactList.class);
//          }
//      });
        return row;
    }
    @Override
    public int getCount() {
//      // TODO Auto-generated method stub
        return data.size();
    }

    static class UserHolder {
        TextView textName;
        TextView textAddress;
        TextView textLocation;
        ImageButton btnEdit;
        Button btnDelete;
        TextView stnumber;
        CheckBox checked;
        }


}

My phoneList Class is ..

package com.example.sqliteapplication;

public class PhoneList {
    String name;
    String number;
    public PhoneList(String string,String number) {
        super();
        this.name = string;
        this.number=number;
    }
    public String getName() {
        return name;
        }
    public void setNumber(String number)
    {
        this.number=number;
    }
    public String getNumber()
    {
        return number;
    }
    public void setName(String name) {
        this.name = name;
        }



}

xml layout of List View

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@layout/bg_player_header"
    >
    <ListView android:layout_width="match_parent"
             android:divider="#FFEEEE" 
        android:layout_height="match_parent" android:id="@+id/lvContacts"></ListView>
</LinearLayout>

xml layout of each row in a List View

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/player_header_bg"
        android:layout_width="fill_parent"
        android:layout_height="60dip"
        android:background="@layout/bg_player_header" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="32dip"
            android:layout_height="40dip"
            android:layout_marginTop="10dip"
            android:src="@drawable/face" >
        </ImageView>
        <TextView
            android:id="@+id/lblName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dip"
            android:layout_weight="1"
            android:paddingLeft="5dp"
            android:text="Description"
            android:textColor="#04b3d2"
            android:textSize="16dp"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/mobilenum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dip"
            android:layout_weight="1"
            android:paddingLeft="5dp"
            android:text="03455039469"
            android:textColor="#04b3d2"
            android:textSize="16dp"
            android:textStyle="bold" />

        <CheckBox
            android:id="@+id/check"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:focusable="false"
            android:src="@drawable/shape"
            />
    </LinearLayout>

</LinearLayout>

Error is following....

05-12 13:30:32.432: E/AndroidRuntime(25447): FATAL EXCEPTION: main
05-12 13:30:32.432: E/AndroidRuntime(25447): android.content.res.Resources$NotFoundException: Unable to find resource ID #0x4
05-12 13:30:32.432: E/AndroidRuntime(25447):    at android.content.res.Resources.getResourceEntryName(Resources.java:1756)
05-12 13:30:32.432: E/AndroidRuntime(25447):    at android.view.View$1.onClick(View.java:3590)
05-12 13:30:32.432: E/AndroidRuntime(25447):    at android.view.View.performClick(View.java:4211)
05-12 13:30:32.432: E/AndroidRuntime(25447):    at android.widget.CompoundButton.performClick(CompoundButton.java:100)
05-12 13:30:32.432: E/AndroidRuntime(25447):    at android.view.View$PerformClick.run(View.java:17446)
05-12 13:30:32.432: E/AndroidRuntime(25447):    at android.os.Handler.handleCallback(Handler.java:725)
05-12 13:30:32.432: E/AndroidRuntime(25447):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-12 13:30:32.432: E/AndroidRuntime(25447):    at android.os.Looper.loop(Looper.java:153)
05-12 13:30:32.432: E/AndroidRuntime(25447):    at android.app.ActivityThread.main(ActivityThread.java:5299)
05-12 13:30:32.432: E/AndroidRuntime(25447):    at java.lang.reflect.Method.invokeNative(Native Method)
05-12 13:30:32.432: E/AndroidRuntime(25447):    at java.lang.reflect.Method.invoke(Method.java:511)
05-12 13:30:32.432: E/AndroidRuntime(25447):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-12 13:30:32.432: E/AndroidRuntime(25447):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-12 13:30:32.432: E/AndroidRuntime(25447):    at dalvik.system.NativeStart.main(Native Method)

解决方案

why you changed id of checkbox in your code?

 holder.checked.setId(position);   

if you want keep position in your checkbox use Tag instead of id

you can use:

holder.checked.setTag(position);

and for getting that:

int position = (Integer)holder.checked.getTag();

as you change id of checkbox in your code you get Resources$NotFoundException because you changed id of checkbox to 1,2,3,4,... and this not saved in R file,

// UPDATE

multiple select item happened because listviews-recycling, for solving this issue you need save selected position in one place or you can do:

add following property to PhoneList

boolean isSelected;

create getter and setter method for that.

public boolean getIsSelected()
{
  return isSelected;
}


public void setIsSelected(boolean isSelected)
{
   this.isSelected = isSelected;
}

then in your code in getView method use:

 holder.checked.setChecked(dta.getIsSelected());

and in click of checkbox change value of isSelected to !isSelected:

so your code must be like:

 holder.checked.setOnClickListener(this);

in onClick method:

 data.get((Integer)v.getTag()).setIsSelected(!data.get((Integer)v.getTag()).getIsSelected);
 notifyDataSetChanged();