我有一个带适配器的listview但是当我点击listview时我没有回复。我已尝试过其他帖子的一些回复,例如setOnItemClickListener Not Working In Custom Listview或Android - setOnItemClickListener event of ListView not working in Fragment,但我无法解决问题。我告诉你我的代码。
首先,mainActivity
public class MyListActivity extends ListActivity {
private MainView view;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray("listaEventos");
/**A continuación, generamos los distintos valores que ocuparán el LinearLayout, concretamente
* las categorías de los eventos.
*/
String[] values = new String[] { "Actividades vacacionales", "Aire libre y excursiones", "Cine",
"Conferencias y otros", "Congresos y jornadas", "Cursos y talleres", "Deporte", "Escénicas",
"Exposiciones", "Ferias", "Fiestas y festivales", "Música", "Viajes", "Otras" };
/**
* Llamamos al adapter personalizado para mostrar la vista.
*/
MyListCategoryArrayAdapter adapter = new MyListCategoryArrayAdapter(this, values, array);
setListAdapter(adapter);
//Log.d("YA SE HA ADAPTADO", "YA SE HA ADAPTADO");
view = (MainView)View.inflate(this, R.layout.all_products, null);
//view.setViewListener(viewListener);
setContentView(view);
}
然后,adapterActivity:
public class MyListCategoryArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private final String[] listanumber2;
public MyListCategoryArrayAdapter(Context context, String[] values, String[] listanumber) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
this.listanumber2 = listanumber;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.rowlayout, parent, false);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
TextView textView = (TextView) convertView.findViewById(R.id.label);
TextView textView2 = (TextView) convertView.findViewById(R.id.number);
textView.setText(values[position]);
String categoria = values[position];
if (categoria.startsWith("Actividades vacacionales")) {
imageView.setImageResource(R.drawable.pic_vacaciones);
textView2.setText(listanumber2[0].trim()+" eventos");
}
if (categoria.startsWith("Aire libre y excursiones")) {
imageView.setImageResource(R.drawable.pic_aire_libre_excursiones);
textView2.setText(listanumber2[1].trim()+" eventos");
}
if (categoria.startsWith("Cine")) {
imageView.setImageResource(R.drawable.pic_cine);
textView2.setText(listanumber2[2].trim()+" eventos");
}
if (categoria.startsWith("Conferencias y otros")) {
imageView.setImageResource(R.drawable.pic_conferencias);
textView2.setText(listanumber2[3].trim()+" eventos");
}
if (categoria.startsWith("Congresos y jornadas")) {
imageView.setImageResource(R.drawable.pic_congresos_jornadas);
textView2.setText(listanumber2[4].trim()+" eventos");
}
if (categoria.startsWith("Cursos y talleres")) {
imageView.setImageResource(R.drawable.pic_curso_taller);
textView2.setText(listanumber2[5].trim()+" eventos");
}
if (categoria.startsWith("Deporte")) {
imageView.setImageResource(R.drawable.pic_deporte);
textView2.setText(listanumber2[6].trim()+" eventos");
}
if (categoria.startsWith("Escénicas")) {
imageView.setImageResource(R.drawable.pic_escenicas);
textView2.setText(listanumber2[7].trim()+" eventos");
}
if (categoria.startsWith("Exposiciones")) {
imageView.setImageResource(R.drawable.pic_exposiciones);
textView2.setText(listanumber2[8].trim()+" eventos");
}
if (categoria.startsWith("Ferias")) {
imageView.setImageResource(R.drawable.pic_ferias);
textView2.setText(listanumber2[9].trim()+" eventos");
}
if (categoria.startsWith("Fiestas y festivales")) {
imageView.setImageResource(R.drawable.pic_fiesta_festival);
textView2.setText(listanumber2[10].trim()+" eventos");
}
if (categoria.startsWith("Música")) {
imageView.setImageResource(R.drawable.pic_musica);
textView2.setText(listanumber2[11].trim()+" eventos");
}
if (categoria.startsWith("Viajes")) {
imageView.setImageResource(R.drawable.pic_viajes);
textView2.setText(listanumber2[12].trim()+" eventos");
}
if (categoria.startsWith("Otras")) {
imageView.setImageResource(R.drawable.pic_otros);
textView2.setText(listanumber2[13].trim()+" eventos");
}
return convertView;
}
然后,我用来捕获用户交互的视图:
/**
* Find our references to the objects in the xml layout
*/
@Override
protected void onFinishInflate() {
super.onFinishInflate();
final ListView list = (ListView)findViewById(android.R.id.list);
list.setItemsCanFocus(false);
list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Log.d("HERE IT DOES NOT ENTER", "HERE IT DOES NOT ENTER");
}
});
final Button button0 = (Button)findViewById(R.id.button1);
button0.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("Boton0000", "Boton0000");
}
});
final Button button1 = (Button)findViewById(R.id.button2);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("Boton1111", "Boton1111");
}
});
final Button button2 = (Button)findViewById(R.id.button3);
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("Boton2222", "Boton2222");
}
});
final Button button3 = (Button)findViewById(R.id.button4);
button3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("Boton3333", "Boton3333");
}
});
}
最后,我为listView和列表的每一行显示.xml:
all.products.xml
<?xml version="1.0" encoding="utf-8"?>
<com.agendajovenzgz.view.MainView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:clickable="false"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="389dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:focusable="true" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Mapa" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/button1"
android:text="Lista" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/button2"
android:text="Buscador" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/button3"
android:text="Más" />
</com.agendajovenzgz.view.MainView>
rowlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip"
>
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:src="@drawable/ic_launcher"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="6dip"
android:layout_toRightOf="@id/icon"
android:gravity="center"
android:textStyle="bold"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:id="@+id/number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/icon"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_below="@id/label"
android:layout_alignWithParentIfMissing="false"
android:gravity="center"
android:textSize="12dp"
android:focusable="false"
android:focusableInTouchMode="false" />
</RelativeLayout>
有人可以帮帮我吗?
提前致谢。
问候。
答案 0 :(得分:0)
您应该在扩展的ListActivity中覆盖onListItemClick。
答案 1 :(得分:0)
尝试不要使用ListView。在android中按名称命名列表视图:
android:id="@+id/my_list"
然后在MainActivity中引用它:
ListView myList = (ListView) findViewById(R.id.my_list);
然后,您可以使用以下内容将myList设置为onItemClickListener
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
// Your code here
}
});
您无需担心后期充气,因为在ListView充气之前,用户无需点击任何项目。如果不使用ListActivity,您可以摆脱onFinishInflate()
以及对其父级的调用,只需将所有点击侦听器放入onCreate()
方法中。
另外,另一个提示 - 在onCreate()
方法之外声明您的变量,并在onCreate()
方法的开头定义它们。这将使它们在整个活动中全球化,因此您不需要final
关键字。
答案 2 :(得分:0)
在ListActivity
中,onContentChanged
执行各种操作,包括设置自己的OnItemClickListener
。
此OnItemClickListener
调用onListItemClick
中的ListActivity
方法。
因此,为了使其有效,您需要
onListItemClick
代替设置OnItemClickListener
OnItemClickListener
之后您调用setContentView
(可能在onAttachedToWindow
,这应该在之后发生,但我没有测试过这个)。答案 3 :(得分:0)
尝试将所有textview的clickable属性设置为false。
机器人:可点击= “假”
答案 4 :(得分:0)
在OnItemClickListener
ListView
设置为Activity#onCreate()
// inside activity
@Override
protected void onCreate(Bundle savedInstanceState) {
// calls to super and setcontentview
listView = (ListView) findViewById(android.R.id.list);
listView.setOnItemClickListener(new OnItemClickListener() { ......