I bind ListView with multiple rows select function. I bind the list view with custom adapter. But problem while I want to get the list from Adapter an error is shown - "Could not execute method of the activity". I also set the getter property in the adapter. But while I call the getter method it throw error.
Please suggest some solution so that I can solve it.
Adapter:
public class CityAdapter extends BaseAdapter {
private Context context;
public List<CityModle.City> citys;
LayoutInflater inflater;
private CitySelectInterface listener;
private ArrayList<CityModle.City> arraylist;
public List<CityModle.City> getCitys() {
return citys;
}
public CityAdapter(Context context, List<CityModle.City> citys, CitySelectInterface listener) {
this.context = context;
this.citys = citys;
this.listener = listener;
inflater = LayoutInflater.from(this.context);
this.arraylist = new ArrayList<CityModle.City>();
this.arraylist.addAll(citys);
}
@Override
public int getCount() {
Log.v("getCountJit",this.citys.size()+"") ;
return this.citys.size();
}
@Override
public CityModle.City getItem(int i) {
return this.citys.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final MyViewHolder mViewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.cityrowlayout, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
mViewHolder.tvTitle.setText(this.citys.get(position).getName());
if(this.citys.get(position).isCheckBox() == true)
{
mViewHolder.imageCheck.setVisibility(View.VISIBLE);
} else {
mViewHolder.imageCheck.setVisibility(View.GONE);
}
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mViewHolder.imageCheck.setVisibility(View.GONE);
Toast.makeText(context, "testClick"+position, Toast.LENGTH_SHORT).show();
listener.cityTableRowClick(String.valueOf(position));
int setView = 0;boolean setchk=false;
if(mViewHolder.imageCheck.getVisibility()== View.GONE) {
Toast.makeText(context, "GONE", Toast.LENGTH_SHORT).show();
mViewHolder.imageCheck.setVisibility(View.VISIBLE);
setchk = true;
} else {
Toast.makeText(context, "VISIBLE", Toast.LENGTH_SHORT).show();
mViewHolder.imageCheck.setVisibility(View.GONE);
setchk = false;
}
citys.get(position).setCheckBox(setchk);
}
});
return convertView;
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
citys.clear();
if (charText.length() == 0) {
citys.addAll(arraylist);
} else {
for (CityModle.City wp : arraylist) {
if (wp.getName().toLowerCase(Locale.getDefault())
.contains(charText)) {
citys.add(wp);
}
}
}
notifyDataSetChanged();
}
private class MyViewHolder {
TextView tvTitle;
ImageView imageCheck;
public MyViewHolder(View item) {
tvTitle = (TextView) item.findViewById(R.id.nameOfCountry);
imageCheck = (ImageView) item.findViewById(R.id.imageCheck);
}
}
}
In Activity:
public void closerlviewpopup(View view) {
List<CityModle.City> cts = cityAdapter.getCitys();
Toast.makeText(this, cts.size(), Toast.LENGTH_SHORT).show();
//cts.size() throws an error Could not execute method of the activity
}
Error:
05-08 17:57:12.359 15243-15243/com.burhanrashid52.imageeditor E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.burhanrashid52.imageeditor, PID: 15243
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4028)
at android.view.View.performClick(View.java:4788)
at android.view.View$PerformClick.run(View.java:19923)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5425)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:928)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:723)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4023)
at android.view.View.performClick(View.java:4788)
at android.view.View$PerformClick.run(View.java:19923)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5425)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:928)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:723)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x64
at android.content.res.Resources.getText(Resources.java:318)
at android.content.res.VivoResources.getText(VivoResources.java:123)
at android.widget.Toast.makeText(Toast.java:307)
at com.burhanrashid52.imageeditor.SetUpActivity.closerlviewpopup(SetUpActivity.java:225)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4023)
at android.view.View.performClick(View.java:4788)
at android.view.View$PerformClick.run(View.java:19923)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5425)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:928)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:723)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x64
Indicates problem with some String resource which you did not provide. cts.size()
is returning int
where the Toast.makeText
function requires a String. You need to change the line as follows.
Toast.makeText(this, cts.size().toString(), Toast.LENGTH_SHORT).show();