当我点击联系人列表视图中的复选框时,应用程序崩溃了

时间:2014-05-12 07:48:08

标签: android listview checkbox custom-adapter android-checkbox

我是新的Android开发人员,我想在列表视图中使用复选框选择多个联系人但是当我点击任何复选框应用程序崩溃时我正在使用自定义适配器PLZ任何人都可以帮助我

我的contactList ACtivity正在关注

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


}

我的自定义适配器正在关注

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


}

我的phoneList类是..

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布局

<?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布局

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

错误如下......

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)

3 个答案:

答案 0 :(得分:1)

为什么您在代码中更改了checkbox的ID?

 holder.checked.setId(position);   

如果您希望在checkbox使用Tag而不是id

中保持位置

你可以使用:

holder.checked.setTag(position);

并获得了:

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

当你在代码中更改了复选框的ID时,你会得到资源$ NotFoundException ,因为你将复选框的id更改为1,2,3,4,...而这并没有保存在R文件中,

//更新

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

将以下属性添加到PhoneList

boolean isSelected;

为此创建getter和setter方法。

public boolean getIsSelected()
{
  return isSelected;
}


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

然后在getView方法的代码中使用:

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

点击复选框,将isSelected更改为!isSelected

所以你的代码必须像:

 holder.checked.setOnClickListener(this);
onClick方法中的

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

答案 1 :(得分:0)

每当您请求无法使用的ID时,

android.content.res.Resources$NotFoundException:都会抛出。

您充气的布局没有名为check的ID。所以你需要检查XML文件。如果不包括它。它将解决您的问题。

 `holder.checked=(CheckBox)row.findViewById(R.id.check);`

答案 2 :(得分:0)

进行此更改

 static class UserHolder {
        TextView textName;
        TextView textAddress;
        TextView textLocation;
        ImageButton btnEdit;
        Button btnDelete;
        TextView stnumber;
       //removed checkbox here
        }

 //Added checkbox here
 CheckBox checked;
 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);
//removed    holder.checked=(CheckBox)row.findViewById(R.id.check); this line
//           holder.btnEdit = (ImageButton) row.findViewById(R.id.atomPay_removePay);
             row.setTag(holder);
        }
        else
        {
             holder = (UserHolder) row.getTag();            
        } 


  //And added this line
    checked=(CheckBox)row.findViewById(R.id.check);