我在Android中有一個ListView
。每個listitem有2個textview。如果我點擊一個TextView
我想打開一個Activity
,如果我點擊另一個我想做另一件事。在列表行中多次點擊
我adapter's代碼是律這樣的:
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); – X3Btel
你可以使用我的代碼嗎?謝謝 –
在onClick您需要開始活動。類型activity.startActivity(意圖) – X3Btel