在我问这里之前,我已经搜索并找到了答案:
long viewId = view.getId();
但是当我在我的代码上使用它时,它仍然是单击整个listview行而不是单击我想要单击的textview。这是我的代码在实现之后回答:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView <? > parent, View view, int position, long id) {
long viewId = view.getId();
if (viewId == R.id.masterclass_name) {
Toast.makeText(getApplicationContext(), "masterclass_name clicked", Toast.LENGTH_SHORT).show();
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(R.layout.dialog_popup_pesan_kelas);
pesan_kelas = (TextView) dialog.findViewById(R.id.pesan_kelas);
sukses = (TextView) dialog.findViewById(R.id.sukses);
studio_name = (TextView) dialog.findViewById(R.id.studio_name);
mohon_hadir = (TextView) dialog.findViewById(R.id.mohon_hadir);
booking_number = (TextView) dialog.findViewById(R.id.booking_number);
indentitas_resmi = (TextView) dialog.findViewById(R.id.indentitas_resmi);
bookbutton = (Button) dialog.findViewById(R.id.bookbutton);
selesaibutton = (Button) dialog.findViewById(R.id.selesaibutton);
bookbutton.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
pesan_kelas.setVisibility(View.GONE);
sukses.setVisibility(View.VISIBLE);
bookbutton.setVisibility(View.GONE);
selesaibutton.setVisibility(View.VISIBLE);
mohon_hadir.setVisibility(View.VISIBLE);
booking_number.setVisibility(View.VISIBLE);
indentitas_resmi.setVisibility(View.VISIBLE);
}
});
selesaibutton.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
dialog.dismiss();
}
});
closedialog = (ImageView) dialog.findViewById(R.id.closeDialog);
closedialog.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
dialog.dismiss();
}
});
studio_name.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
Intent intent = new Intent(context, StudioDetail.class);
startActivity(intent);
}
});
dialog.show();
} else if (viewId == R.id.studio_nama) {
Toast.makeText(getApplicationContext(), "studio_nama clicked", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, StudioDetail.class);
startActivity(intent);
} else if (viewId == R.id.trainer_name) {
Toast.makeText(getApplicationContext(), "trainer_name clicked", Toast.LENGTH_SHORT).show();
} else if (viewId == R.id.area) {
Toast.makeText(getApplicationContext(), "area clicked", Toast.LENGTH_SHORT).show();
} else {
CariKelasAdapter carikelas = (CariKelasAdapter) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), carikelas.getMasterclass_name(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, checkin.class);
startActivity(intent);
}
}
});
我要点击的textview有id:masterclass_name,studio_nama,trainer_name,area。还有一个textview id,但我不想要点击id。因此,只能点击5个textview的4个textview。
每个(来自4个)textview将重定向到不同的结果,例如:一个将显示对话框,另一个将重定向到另一个活动类。
我的代码有什么问题(为什么答案对我的代码不起作用)?如何/ 4正确的代码使4 textview变得可点击?
有关信息,这是我的适配器getView():
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.image_carikelas = (ImageView) v.findViewById(R.id.image_carikelas);
holder.masterclass_name = (TextView) v.findViewById(R.id.masterclass_name);
holder.studio_nama = (TextView) v.findViewById(R.id.studio_nama);
holder.trainer_name = (TextView) v.findViewById(R.id.trainer_name);
holder.area = (TextView) v.findViewById(R.id.area);
holder.facility = (TextView) v.findViewById(R.id.facility);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
//holder.imageview.setImageResource(R.drawable.promo_1);
new DownloadImageTask(holder.image_carikelas).execute(kelasList.get(position).getImage());
holder.masterclass_name.setText(kelasList.get(position).getName());
holder.studio_nama.setText(kelasList.get(position).getStudio_nama());
holder.trainer_name.setText("Trainer: " + kelasList.get(position).getTrainer_name());
holder.area.setText("Area: " + kelasList.get(position).getArea());
holder.facility.setText("Facility: " + kelasList.get(position).getFacility());
return v;
}
答案 0 :(得分:0)
我认为你根本不应该使用setOnItemClickListener。这是用于单击ListView中的整个项目。
您应该为4个textviews创建4个OnClickListener,并在适配器的getView()中,使用setOnClickListener将4个textviews链接到那些OnClickListeners。
通常你应该将项目位置设置为每个textview的标记,因此在OnClickListener中你可以知道它被view.getTag()
点击的项目。示例:
public class MyAdapter extends BaseAdapter {
private MyActivity mActivity
public MyAdapter(MyActivity activity) {
mActivity = activity;
}
View.OnClickListener listener1 = new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = (int)v.getTag();
// do stuff based on position or kelasList.get(position)
// you can call mActivity.startActivity() if you need
}
};
// similar for listener2,3,4
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.image_carikelas = (ImageView) v.findViewById(R.id.image_carikelas);
holder.masterclass_name = (TextView) v.findViewById(R.id.masterclass_name);
holder.studio_nama = (TextView) v.findViewById(R.id.studio_nama);
holder.trainer_name = (TextView) v.findViewById(R.id.trainer_name);
holder.area = (TextView) v.findViewById(R.id.area);
holder.facility = (TextView) v.findViewById(R.id.facility);
// set OnClickListeners
holder.studio_nama.setOnClickListener(listener1);
holder.trainer_name.setOnClickListener(listener2);
holder.area.setOnClickListener(listener3);
holder.facility.setOnClickListener(listener4);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
//holder.imageview.setImageResource(R.drawable.promo_1);
new DownloadImageTask(holder.image_carikelas).execute(kelasList.get(position).getImage());
holder.masterclass_name.setText(kelasList.get(position).getName());
holder.studio_nama.setText(kelasList.get(position).getStudio_nama());
holder.trainer_name.setText("Trainer: " + kelasList.get(position).getTrainer_name());
holder.area.setText("Area: " + kelasList.get(position).getArea());
holder.facility.setText("Facility: " + kelasList.get(position).getFacility());
// set tags
holder.studio_nama.setTag(position);
holder.trainer_name.setTag(position);
holder.area.setTag(position);
holder.facility.setTag(position);
return v;
}
}