I have custom ListView
,my ListView
contains one button
, if we click on button i want to go another activity with some data.I used following code,
holder.mMore.setOnTouchListener(new OnTouchListener() {
开发者_开发知识库 @Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == event.ACTION_DOWN){
Intent moreIntent=new Intent(getContext(),SelectItemDesp.class);
v.getContext().startActivity(moreIntent);
}
return false;
}
});
it is showing error.pls help me
I'll assume that you have written a class for your ListView adapter. Let's just name this class quickly: MyListViewAdapter. And in this class you most probably have a constructor. It could look like this:
public MyListViewAdapter (Context context, ArrayList<String> myList) {
super (context, R.layout.my_layout, R.id.my_text_view, myList);
Now the context is what you need to start a new Activity because a ListView adapter which extends an ArrayAdapter cannot start an Activity because its not derived from the Activity-class. So this is how you start an Activity then:
context.startActivity(context, GoToClass.class);
Just make sure to add a global but private variable to your code (private Context context
) and add this to your constructor this.context = context
and if you create the object you have to put MyListViewAdapter m = new MyListViewAdapter(CurrentClass.this, myListFullOfStrings);
Replace
Intent moreIntent=new Intent(getContext(),SelectItemDesp.class);
v.getContext().startActivity(moreIntent);
With
Intent lObjIntent = new Intent(getApplicationContext(), SelectItemDesp.class);
startActivity(lObjIntent);
finish();
You're invoking the new activity from an anonymous inner class, anything like this
would refer to this anonymous class only.
Use MyClass.this
as Vinayak suggested and if error persists, post the logcat here
I am also using customlistview and also have one delete button in that listview and i have done something like below and it work for me.
and one thing my class extends ArrayAdapter
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.row_layout_mymedicine, null);
holder.btnDelete = (Button)convertView.findViewById(R.id.btnDelete);
holder.btnDelete.setOnClickListener(this);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
return convertView;
}
then onClick() method i have do
public void onClick(View v)
{
switch(v.getId())
{
case R.id.btnDelete:
getContext().startActivity(new Intent(getContext(),DeleteActivity.class));
break;
default:
break;
}
}
Try This. It should Work
Intent intent = new Intent(context, SelectItemDesp.class);
context.startActivity(intent);
精彩评论