问题
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)
回答1:
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();
回答2:
android.content.res.Resources$NotFoundException:
will throw whenever you request a id which is not available for you.
The Layout which you inflating doesn't have id named check
. So you need to check the XML
file. If its not there include it. It will resolve your problem.
`holder.checked=(CheckBox)row.findViewById(R.id.check);`
回答3:
Make this changes
static class UserHolder {
TextView textName;
TextView textAddress;
TextView textLocation;
ImageButton btnEdit;
Button btnDelete;
TextView stnumber;
//removed checkbox here
}
and
//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);
来源:https://stackoverflow.com/questions/23603619/app-crashes-when-i-click-on-checkbox-in-contact-listview