【发布时间】:2016-11-07 09:41:44
【问题描述】:
我在 Android 中有一个 ListView。每个列表项有 2 个文本视图。如果我点击一个TextView,我想打开一个Activity,如果我点击另一个,我想做另一件事。
我的适配器的代码是这样的:
public class LazyAdapterHelpCentersAlava extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
HashMap<String, String> song;
Typeface tf;
Context context;
public LazyAdapterHelpCentersAlava(Activity a, ArrayList<HashMap<String, String>> d, String font) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
tf = Typeface.createFromAsset(activity.getAssets(), font);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row_help_centers, null);
//TextView id = (TextView)vi.findViewById(R.id.id); // title
TextView title = (TextView)vi.findViewById(R.id.nombre); // title
TextView localizacion = (TextView)vi.findViewById(R.id.localizacion);
TextView email = (TextView)vi.findViewById(R.id.email);
TextView web = (TextView)vi.findViewById(R.id.web);
song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(Help.TAG_NOMBRE));
web.setText(song.get(Help.TAG_TELEFONO));
localizacion.setText(song.get(Help.TAG_DIRECCION));
email.setText(song.get(Help.TAG_EMAIL));
title.setTypeface(tf);
web.setTypeface(tf);
localizacion.setTypeface(tf);
email.setTypeface(tf);
web = (TextView)vi.findViewById(R.id.web);
web.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Need to start an Activity here
}
});
return vi;
}
}
我已经在web 上设置了点击监听器,但是,我不知道如何打开一个活动,因为它说这样的错误方法startActivity(Intent) 未定义新类型View.OnClickListener(){}强>
有人可以帮助我吗?非常感谢。
【问题讨论】:
-
你需要在 Context 上调用 startActivity。在您的情况下,您将其作为活动。调用activity.startActivity(intent);
-
你可以使用我的代码吗?谢谢
-
在您需要启动活动的 onClick 中。输入activity.startActivity(intent)
标签: android listview android-adapter