gpt4 book ai didi

android - setOnItemClickListener NOT WORKING 和 setOnItemLongClickListener 在 ListView 中工作

转载 作者:行者123 更新时间:2023-11-29 17:59:00 26 4
gpt4 key购买 nike

我希望有人能帮我解决这个问题。我有一个 ListView ,我希望列表的每一行都可以点击并且可以长时间点击。 setOnItemLongClickListener 正在工作,但我无法让该行可点击。我看过这个帖子ListView with clickable/editable widget和类似的东西,但我的行中没有可聚焦的项目,所以我不知道还有什么可以。

这是我在 xml 中对 listView 的定义:

<ListView
android:id="@id/android:list"
android:textFilterEnabled="true"
android:layout_width="match_parent"
android:fadeScrollbars="false"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>

这是我的行的定义(有点难看):

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:longClickable="true"
android:descendantFocusability="blocksDescendants">


<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"

tools:ignore="UselessParent" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<ImageView
android:id="@+id/imageViewAgenda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/historicos"
android:src="@android:drawable/ic_menu_agenda" />

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:id="@+id/avisoVerdeFilaHist"
android:textIsSelectable="false"
style="@style/CuadroNotificacion"
android:background="@drawable/borde_blanco"/>

<TextView
android:id="@+id/avisoRojoFilaHist"
android:textIsSelectable="false"
style="@style/CuadroNotificacion"
android:background="@drawable/borde_blanco"/>

</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayoutTarea"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/textViewTarea"
android:textIsSelectable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="5dp"
android:text="@string/tarea2"/>

<TextView
android:id="@+id/textViewTitulo"
android:textIsSelectable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Titulo"/>

</LinearLayout>

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1">

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/textViewFil1Col1"
android:textIsSelectable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="f1c1"/>

<TextView
android:id="@+id/textViewFil1Col2"
android:textIsSelectable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="f1c2"/>

</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/textViewFil2Col1"
android:textIsSelectable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="f2c1"/>

<TextView
android:id="@+id/textViewFil2Col2"
android:textIsSelectable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="f2c2"/>

</TableRow>
</TableLayout>
</LinearLayout>

</TableRow>
</TableLayout>

如果您想知道为什么我使用 tableLayout 是因为 android:stretchColumns="1"属性。 请注意,我使用 android:descendantFocusability="blocksDescendants"。以防万一。

这是我的自定义光标适配器:

public class ListaTareasCursorAdapter extends SimpleCursorAdapter implements Filterable{

private Context context;
private int layout;
private String[] from;
private int[] to;
private FragmentManager fragmentManager;


public ListaTareasCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags, FragmentManager fragmentManager) {

super(context, layout, c, from, to, flags);

this.context = context;
this.layout = layout;
this.from = from;
this.to = to;
this.fragmentManager=fragmentManager;
}


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);

return v;
}

@Override
public void bindView(View v, Context context, Cursor cursor) {

//Columna de la BD que queremos recuperar
String columna = null;

//Indice de la columna de BD
int nombreCol = 0;

//Resultado de obtener el indice de la columna de BD
String nombre = null;

//Nombre del textView en nuestro Layout donde queremos
//que aparezca el resultado.
TextView nombre_text = null;


String nombreCompleto = ""; //almacena nombre completo del empleado
String fechaCompleta = ""; //almacena fecha y hora
String nombreTarea = ""; //almacena nombre de la tarea

//Para cada valor de la BD solicitado, lo mostramos en el text view.
for (int i=0; i<from.length; i++){
columna = from[i];
nombreCol = cursor.getColumnIndex(columna);
nombre = cursor.getString(nombreCol);

if(nombre==null){
nombre = "--";
}

if(columna.equals(Tarea.NOMBRE)){
nombreTarea = nombre;
}

if(columna.equals(Tarea.NOMBRE_EMPLEADO)){
nombreCompleto = nombre;
}

if(columna.equals(Tarea.APELLIDO_EMPLEADO)){
nombreCompleto = nombreCompleto + " " + nombre;
nombre = nombreCompleto;
}

if(columna.equals(Tarea.FECHA)){
fechaCompleta = "Pautado: " +nombre;
}

if(columna.equals(Tarea.HORA)){
fechaCompleta = fechaCompleta + " " + nombre;
nombre = fechaCompleta;
}


if(columna.equals(Tarea.FECHA_FINALIZACION)){
nombre = "Finalizado: " + nombre;
}

nombre_text = (TextView) v.findViewById(to[i]);
if (nombre_text != null) {
nombre_text.setText(nombre);
}
}

}
}

这里是我调用听众的地方:

public class TareasActivity extends ListFragment{
....


ListView lvTareas = (ListView) view.findViewById (android.R.id.list);


final ListaTareasCursorAdapter listCursorAdapterTareas = new ListaTareasCursorAdapter(context, R.layout.activity_fila_tarea, mCursorTareas, from, to, 0,this.getFragmentManager());
lvTareas.setAdapter(listCursorAdapterTareas);
lvTareas.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub

Log.v("CLICK","pos"+" "+pos);


}
});

lvTareas.setOnItemLongClickListener(new OnItemLongClickListener() {

public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub

Log.v("long clicked","pos"+" "+pos);
return true;


}
});

}

一定是我遗漏了什么愚蠢的东西。

任何想法将不胜感激,感谢阅读。

更多信息:我不知道这是否有用,但我正在使用 TabHost。

最佳答案

您需要重写 ListFragment 中的 onListItemClick 方法,而不是使用 onItemClick。您不需要调用 setOnItemClickListener。

onListItemClick

关于android - setOnItemClickListener NOT WORKING 和 setOnItemLongClickListener 在 ListView 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17324076/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com