Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可单击的项目形成一个列表视图:setOnItemClickListener赢得';行不通_Java_Android_Listview_Onitemclicklistener - Fatal编程技术网

Java 可单击的项目形成一个列表视图:setOnItemClickListener赢得';行不通

Java 可单击的项目形成一个列表视图:setOnItemClickListener赢得';行不通,java,android,listview,onitemclicklistener,Java,Android,Listview,Onitemclicklistener,我目前正在从事一个android项目,我需要从ListView的一个项目启动一个活动。我搜索了大约8个小时,发现总是同一个解决方案,它不起作用。 这是我的密码: 在编目活动的onCreate方法中: CtlgArrayAdapter adapter = new CtlgArrayAdapter(this, recipeName, recipeDescr, recipeDiff, recipePic, recipeEval); ListView list = (Li

我目前正在从事一个android项目,我需要从ListView的一个项目启动一个活动。我搜索了大约8个小时,发现总是同一个解决方案,它不起作用。 这是我的密码:

在编目活动的onCreate方法中:

CtlgArrayAdapter adapter = new CtlgArrayAdapter(this, recipeName, recipeDescr, recipeDiff,
            recipePic, recipeEval);
    ListView list = (ListView)findViewById(android.R.id.list);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d(TAG, "Item clicked");
        }
    });    
CtlgArrayAdapter adapter=新的CtlgArrayAdapter(此、recipeName、RecipeDscr、RecipeDff、,
RecipeEpic,recipeEval);
ListView列表=(ListView)findViewById(android.R.id.list);
list.setAdapter(适配器);
list.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
Log.d(标记“单击的项目”);
}
});    
CtlgArrayAdapter类:

public class CtlgArrayAdapter extends ArrayAdapter<String> {
private static final String TAG = "CTLGADAPTER";

private final Activity context;
private final String[] recipeName;
private final String[] recipeDescr;
private final Integer[] recipeDiff;
private final Integer[] recipePic;
private final Integer[] recipeEval;

public CtlgArrayAdapter(Activity context, String[] recipeName, String[] recipeDescr,
                        Integer[] recipeDiff, Integer[] recipePic, Integer[] recipeEval) {
    super(context, R.layout.ctlg_list, recipeName);

    this.context = context;
    this.recipeName = recipeName;
    this.recipeDescr = recipeDescr;
    this.recipeDiff = recipeDiff;
    this.recipePic = recipePic;
    this.recipeEval = recipeEval;
}

public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.ctlg_list, null, true);

    TextView nameTile = (TextView) rowView.findViewById(R.id.ctlg_list_name);
    TextView diffTile = (TextView) rowView.findViewById(R.id.ctlg_list_diff);
    TextView descrTile = (TextView) rowView.findViewById(R.id.ctlg_list_descr);
    ImageView picTile = (ImageView) rowView.findViewById(R.id.ctlg_list_img);
    RatingBar rateTile = (RatingBar) rowView.findViewById(R.id.ctlg_list_rating);
    nameTile.setText(recipeName[position]);
    diffTile.setText(recipeDiff[position].toString());
    descrTile.setText(recipeDescr[position]);
    picTile.setImageResource(recipePic[position]);
    rateTile.setRating(recipeEval[position]);

    return rowView;
}
公共类CtlgArrayAdapter扩展了ArrayAdapter{
私有静态最终字符串TAG=“CTLGADAPTER”;
私人最终活动背景;
私有最终字符串[]recipeName;
私有最终字符串[]recipeDescr;
私有最终整数[]recipeDiff;
私有最终整数[];
私有最终整数[]recipeEval;
公共CTLGarrayaAdapter(活动上下文,字符串[]recipeName,字符串[]RecipeDscr,
整数[]recipeDiff,整数[]RecipeEpic,整数[]RecipeVal){
super(上下文,R.layout.ctlg_列表,recipeName);
this.context=上下文;
this.recipeName=recipeName;
this.recipeDescr=recipeDescr;
this.recipeDiff=recipeDiff;
this.recipePic=recipePic;
this.recipeEval=recipeEval;
}
公共视图getView(内部位置、视图视图、视图组父视图){
LayoutInflater充气器=上下文。getLayoutInflater();
视图行视图=充气机。充气(R.layout.ctlg_列表,null,true);
TextView-nameTile=(TextView)rowView.findViewById(R.id.ctlg\u-list\u-name);
TextView diffTile=(TextView)rowView.findViewById(R.id.ctlg\u list\u diff);
TextView descrTile=(TextView)rowView.findViewById(R.id.ctlg\u list\u descr);
ImageView picTile=(ImageView)rowView.findViewById(R.id.ctlg\u list\u img);
RatingBar rateTile=(RatingBar)rowView.findViewById(R.id.ctlg\u列表\u rating);
nameTile.setText(recipeName[位置]);
setText(recipeDiff[position].toString());
descartile.setText(recipeDescr[位置]);
picTile.setImageResource(recipePic[position]);
rattile.setRating(recipeEval[位置]);
返回行视图;
}
问题是,当我点击我的物品时,什么都没有发生。所有的互联网似乎都说这是解决方案,但它不起作用:'(

以下是我的.xml文件: 目录布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
    android:id="@android:id/list"
    android:clickable="true"
    android:layout_alignParentStart="true"
    android:layout_below="@id/linear_ctlg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/> </RelativeLayout>    

以及目录和项目布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
    android:id="@+id/ctlg_list_img"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:focusable="false"
    android:scaleType="centerCrop"
    android:src="@drawable/no_image"
    android:layout_marginEnd="10dp"/>
<TextView
    android:id="@+id/ctlg_list_name"
    android:textColor="@color/colorPrimary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toEndOf="@id/ctlg_list_img"
    android:focusable="false"
    android:textStyle="bold"
    android:text="Nom de la recette sur son lit de char"/>
<TextView
    android:id="@+id/ctlg_list_diff"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@id/ctlg_list_img"
    android:layout_below="@id/ctlg_list_name"
    android:text="Difficile"/>
<TextView
    android:id="@+id/ctlg_list_descr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/ctlg_list_diff"
    android:layout_toEndOf="@id/ctlg_list_img"
    android:focusable="false"
    android:textStyle="italic"
    android:text="Ceci est la description de la recette. J'en mets une longue pour être sûr qu'il y ait assez d'espace. Il fait beau aujourd'hui."/>
<RatingBar
    android:id="@+id/ctlg_list_rating"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:focusable="false"
    android:scaleX="0.4"
    android:scaleY="0.4"
    android:numStars="5"
    android:rating="3"
    android:stepSize="1"/> </RelativeLayout>


如果您发现我的代码有什么问题,非常感谢!:)

请确保catelogue_项目中的任何视图都不可聚焦或单击,请使用下面的代码

android:clickable="false" 
android:focusable="false"
android:focusableInTouchMode="false"
还可以尝试使用以下命令设置根布局:
android:genderantfocusability=“blocksDescendants”


原因是如果列表中的任何元素包含可聚焦项,则不会调用onclick of list。

请确保catelogue_项中的任何视图都不可聚焦或可单击,请使用下面的代码

android:clickable="false" 
android:focusable="false"
android:focusableInTouchMode="false"
还可以尝试使用以下命令设置根布局:
android:genderantfocusability=“blocksDescendants”


原因是如果列表中的任何元素包含可聚焦项,则不会调用列表的onclick。

我不确定其解决方案是否正确

您的传递活动,而不是此处的上下文

super(context,R.layout.ctlg_list,recipeName);
,所以尝试传递context


检查这段代码,它与代码类似,可能很有用。

我不确定它是否是正确的解决方案

您的传递活动,而不是此处的上下文

super(context,R.layout.ctlg_list,recipeName);
,所以尝试传递context


检查此代码,它与代码类似,可能很有用。

您不需要任何可聚焦:false或可点击:false。您可以删除所有这些代码。只需添加此代码即可
android:genderantfocusability=“blocksDescendants”
在您的目录xml顶部相对布局中。我用您的代码进行了尝试并成功了。

您不需要任何可聚焦:false或可点击:false。您可以删除所有这些。只需添加此代码即可在您的目录xml顶部相对布局中。我用您的代码进行了尝试并成功了。

我找到了一个解决方案: 我从目录布局中取消了分级栏,现在一切正常(我不需要将其保留在此布局中,因为它将在以后显示)。我认为有一些解决方案可以通过修改xml文件中的属性使其与分级栏一起工作,但是…无需担心:)

我找到了一个解决方案:
我从目录布局中取消了分级栏,现在一切正常(我不需要将其保留在此布局中,因为稍后将显示它)。我想有一些解决方案可以通过修改xml文件中的属性使其与分级栏一起工作,但是…无需担心:)

他在本例中也传递了一个活动;)我不认为问题来自此,谢谢:)他在本例中也传递了一个活动;)我不认为问题来自此,谢谢:)但它的工作因为我用你的代码创建了一个新项目并尝试了。你能尝试添加
android:genderantfocusability=“blocksDescendants”吗
incatalogue\u item中的属性到相对布局我找到了一个解决方案,但我会尝试:)但是它的工作。因为我用你的代码创建了一个新项目并尝试了。你能尝试添加
android:genderFocusability=“blocksDescendants”
属性到incatalogue\u item中的相对布局我找到了一个解决方案,但我会尝试:)