我正在使用适配器类来填充我的列表视图,但我的列表视图中的每个项目都有两个文本视图,我想要将其设置为监听器。所以我在适配器类中设置它,当我尝试显示吐司时它工作正常。
但问题是我在OnClickListener中无法启动活动。该应用程序崩溃。请帮助或建议另一种方法来实现相同的目标。 Manifest中已经提到了该活动。
这是我的代码: -
public class Adapter_NearMe_TyreWorx extends ArrayAdapter<List_NearMe> implements View.OnClickListener {
ArrayList<List_NearMe> arraylist;
private Context context;
private List<List`enter code here`_NearMe> list;
public Adapter_NearMe_TyreWorx(Context context, int resource, List<List_NearMe> objects) {
super(context, resource, objects);
this.context = context;
this.List = objects;
arraylist = new ArrayList<List_NearMe>();
arraylist.addAll(List);
}
TextView Btn_Call;
String Fac_landmark;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.canvas_two, parent, false);
List_NearMe list= list.get(position);
String Fac_name=list.getName();
Fac_landmark=list.getLandmark();
String Fac_gMap=list.getgMap();
String Fac_contact=list.getContact();
TextView distance=(TextView)view.findViewById(R.id.fac_distance);
TextView Fac_Name=(TextView)view.findViewById(R.id.fac_name);
TextView Fac_Address=(TextView)view.findViewById(R.id.fac_address);
Btn_Call=(TextView)view.findViewById(R.id.btn_call);
TextView Btn_Go=(TextView)view.findViewById(R.id.btn_go);
Btn_Go.setOnClickListener(this);
return view;
}
public void onClick(View v) {
Toast.makeText(getContext(),"Toast text",LENGTH.SHORT).show(); //working toast code
Intent intent = new Intent(getContext(), SampleActivity.class);
getContext().startActivity(intent);
}
}
答案 0 :(得分:1)
你必须添加一个Intent标志来从非活动类private ViewFlipper viewFlipper;
private float lastX;
viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = touchevent.getX();
break;
case MotionEvent.ACTION_UP:
float currentX = touchevent.getX();
// Handling left to right screen swap.
if (lastX < currentX) {
// If there aren't any other children, just break.
if (viewFlipper.getDisplayedChild() == 1)
break;
// c.add(Calendar.DAY_OF_MONTH,-1);
// formattedDate = df.format(c.getTime());
// txt1.setText(formattedDate);
// Next screen comes in from left.
viewFlipper.setInAnimation(this, R.anim.slide_in_from_left);
// Current screen goes out from right.
viewFlipper.setOutAnimation(this, R.anim.slide_out_to_right);
// Display next screen.
viewFlipper.showNext();
}
// Handling right to left screen swap.
if (lastX > currentX) {
// If there is a child (to the left), kust break.
if (viewFlipper.getDisplayedChild() == 1)
break;
// c.add(Calendar.DAY_OF_MONTH,1);
// formattedDate = df.format(c.getTime());
// txt1.setText(formattedDate);
// Next screen comes in from right.
viewFlipper.setInAnimation(this, R.anim.slide_in_from_right);
// Current screen goes out from left.
viewFlipper.setOutAnimation(this, R.anim.slide_out_to_left);
// Display previous screen.
viewFlipper.showPrevious();
}
break;
}
return false;
}
传递一个Intent,在intent中添加这个标志会很好地尝试传递你的Intent:
Intent.FLAG_ACTIVITY_NEW_TASK
或
Intent intent = new Intent(getContext(), SampleActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(intent);
答案 1 :(得分:0)
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context.startActivity(new Intent(context.getApplicationContext(), SampleActivity.class));
}
});
在您在适配器的构造函数中设置的上下文中调用该方法。
因为startActivity()
只能从应用程序上下文中的方法启动,因为你的适配器不存在,你必须引用你在{{1}提供的context
您的适配器类
答案 2 :(得分:0)
用上下文替换getContext()
。
Intent intent = new Intent(context, SampleActivity.class);
context.startActivity(intent);