Reputation: 301
I have created a custom adapter for my list view so that I can have listener events on my checkbox and list as well. Following is my code:
public class CustomAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] names;
private final String TAG = "MyPerformanceArrayAdapter";
private static final int TASK_CREATE = 0;
private static final int ACTIVITY_EDIT = 1;
static class ViewHolder {
public TextView text;
public CheckBox reminderCheckBox;
}
public CustomAdapter(Activity context, String[] names) {
super(context, R.layout.reminder_row, names);
this.context = context;
this.names = names;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.reminder_row, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.reminderRowTextId);
viewHolder.reminderCheckBox = (CheckBox) rowView.findViewById(R.id.CheckBoxId);
rowView.setTag(viewHolder);
}
final int pos = position;
final ViewHolder holder = (ViewHolder) rowView.getTag();
String s = names[position];
holder.text.setText(s);
holder.reminderCheckBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (holder.reminderCheckBox.isChecked()) {
Toast.makeText(getContext(), "pos-->chkd" + pos, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "pos-->un--chkd" + pos, Toast.LENGTH_SHORT).show();
}
}
});
holder.text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "TEXT CLICKED" + pos , Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context,ReminderModificationActivity.class);
intent.putExtra(TasksDBAdapter.KEY_ROWID, pos);
Log.i(TAG, "row clickd --> " + pos);
startActivity(intent,ACTIVITY_EDIT); <<<<-------ERROR ON THIS LINE
}
});
return rowView;
}
}
The error I am getting on pointed line (startActivity(intent,ACTIVITY_EDIT);) is:
The method startActivity(Intent, int) is undefined for the type new View.OnClickListener(){}
Can anyone please provide some pointers for same?
Upvotes: 0
Views: 1376
Reputation: 243
Intent mintent = new Intent(context,ReminderModificationActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mintent);
Upvotes: 0
Reputation: 29436
Use CustomAdapter.this.getContext().startActivity()
instead.
Why:
To refer outer instance from inner anonymous class use OuterClassName.this
. In your case CustomAdapter.this
.
And, to start activity, you need Context
, which you can get by calling getContext()
method of ArrayAdapter
.
Upvotes: 0
Reputation: 5624
Since your code is in the View.OnClickListener, the wrong context is being used. You need to call startActivity() on the Activity class and not on the View.OnClickListener class.
To do this you should be able to do:
context.startActivity(intent, ACTIVITY_EDIT);
or
((Activity) context).startActivity(intent, ACTIVITY_EDIT);
Upvotes: 2