Java 自定义ListView侦听器

Java 自定义ListView侦听器,java,android,listview,Java,Android,Listview,已使用自定义适配器和行布局创建listView,但无法将onClickListener正确设置为它。onClickListener在android内置列表(而非自定义列表)中工作时。。我不知道为什么现在不行。 不管怎样,这是代码 名单 公共类VjezbeLista扩展了AppCompative活动{ ListView listaVjezbe; 阵列适配器; 字符串[]misicneSkupine={“Prsa”, “Leđa”, “拉美娜”, “二头肌”, “三头肌”, “Trbuh”, “Kv

已使用自定义适配器和行布局创建listView,但无法将onClickListener正确设置为它。onClickListener在android内置列表(而非自定义列表)中工作时。。我不知道为什么现在不行。 不管怎样,这是代码

名单

公共类VjezbeLista扩展了AppCompative活动{
ListView listaVjezbe;
阵列适配器;
字符串[]misicneSkupine={“Prsa”,
“Leđa”,
“拉美娜”,
“二头肌”,
“三头肌”,
“Trbuh”,
“Kvadriceps”,
“洛ž阿”,
“Listovi”};
整数[]imageId={R.drawable.ic_vjezbe_prsa,
R.drawable.ic_vjezbe_ledja,
R.drawable.ic_vjezbe_ramena,
R.drawable.ic_vjezbe_肱二头肌,
R.drawable.ic_vjezbe_三头肌,
R.drawable.ic_vjezbe_trbuh,
R.drawable.ic_vjezbe_kvadriceps,
R.drawable.ic_vjezbe_loza,
R.drawable.ic_vjezbe_listovi};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vjezbe_lista);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
AdapterListaVjezbe适配器=新适配器ListaVjezbe(VjezbeLista.this,misicneSkupine,imageId);
listaVjezbe=(ListView)findViewById(R.id.listaVjezbe);
listaVjezbe.setAdapter(适配器);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
如果(位置==0){
意向意向=新意向(VjezbeLista.this,VjezbeListaPrsa.class);
星触觉(意向);
}//如果etc。。。
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.menu\u pretraga,menu);
返回true;
}
}
适配器

public class AdapterListaVjezbe extends ArrayAdapter<String> {
    private final Activity context;
    private final String[] web;
    private final Integer[] imageId;


     public AdapterListaVjezbe(Activity context,
                      String[] web, Integer[] imageId) {
        super(context, R.layout.layout_lista_vjezbe_2, web);

        this.context = context;
        this.web = web;
        this.imageId = imageId;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.layout_lista_vjezbe_2, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.textLista);

        ImageView imageView = (ImageView) rowView.findViewById(R.id.imageViewListaVjezbe2);
        txtTitle.setText(web[position]);

        imageView.setImageResource(imageId[position]);
        return rowView;
    }
}
公共类适配器Listavjezbe扩展ArrayAdapter{
私人最终活动背景;
私有最终字符串[]web;
私有最终整数[]imageId;
公共适配器Listavjezbe(活动背景,
字符串[]web,整数[]imageId){
super(上下文,R.layout.layout_lista_vjezbe_2,web);
this.context=上下文;
this.web=web;
this.imageId=imageId;
}
@凌驾
公共视图getView(内部位置、视图视图、视图组父视图){
LayoutInflater充气器=上下文。getLayoutInflater();
视图行视图=充气机。充气(R.layout.layout_lista_vjezbe_2,null,true);
TextView txtTitle=(TextView)rowView.findViewById(R.id.textLista);
ImageView ImageView=(ImageView)rowView.findViewById(R.id.imageViewListaVjezbe2);
setxt(web[位置]);
setImageResource(imageId[位置]);
返回行视图;
}
}
和行布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/btnVjezbePrsaPotisakSKlupe"
        android:minHeight="72dp"
        android:clickable="true"
        android:background="?attr/selectableItemBackground">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical">

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

                <ImageView
                    android:layout_width="56dp"
                    android:layout_height="56dp"
                    android:id="@+id/imageViewListaVjezbe2"
                    android:layout_marginLeft="10dp"
                    android:layout_gravity="center_vertical"
                    android:src="@drawable/ic_potisak_s_klupe" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="0.9"
                android:gravity="center_vertical"
                android:layout_marginBottom="0dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="10dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/potisakSKlupe"
                    android:id="@+id/textLista"
                    android:textSize="16sp"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="0.9"
                    android:layout_marginBottom="10dp" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageVisdfsdew19"
                    android:background="@drawable/ic_strelica"
                    android:layout_marginBottom="10dp"
                    android:minHeight="36dp"
                    android:minWidth="36dp"
                    android:layout_marginLeft="10dp" />

            </LinearLayout>

        </TableRow>

    </LinearLayout>
    </LinearLayout>
</RelativeLayout>


因此,如果有人能帮我克服错误,我将不胜感激。。谢谢

您可以在listview中的填充日期之后添加此行

listaVjezbe.setAdapter(适配器);
adapter.notifyDataSetChanged()

删除线性布局中的android:clickable=“true”,该线性布局在您指定的行布局中id为“btnvjezbepsapotisaksklupe”。因为,单击事件是针对线性布局而不是整个列表项布局触发的。

从btnvjezbepsapotisaklupe lineareayout中删除android:clickable=“true”。然后它会工作良好(经过测试)



可能您的项目未附加到您的根父项。您是否尝试过View rowView=inflater.inflate(R.layout.layout_lista_vjezbe_2,parent,true)?它给出了java.lang.UnsupportedOperationException:addView(View,LayoutParams)在AdapterView错误中不受支持android:clickable=“true”中的
android:id=“@+id/btnvjezbepsapotisaksklupe”
RelativeLayout看起来可疑。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/btnVjezbePrsaPotisakSKlupe"
        android:minHeight="72dp"
        android:clickable="true"
        android:background="?attr/selectableItemBackground">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical">

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

                <ImageView
                    android:layout_width="56dp"
                    android:layout_height="56dp"
                    android:id="@+id/imageViewListaVjezbe2"
                    android:layout_marginLeft="10dp"
                    android:layout_gravity="center_vertical"
                    android:src="@drawable/ic_potisak_s_klupe" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="0.9"
                android:gravity="center_vertical"
                android:layout_marginBottom="0dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="10dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/potisakSKlupe"
                    android:id="@+id/textLista"
                    android:textSize="16sp"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="0.9"
                    android:layout_marginBottom="10dp" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageVisdfsdew19"
                    android:background="@drawable/ic_strelica"
                    android:layout_marginBottom="10dp"
                    android:minHeight="36dp"
                    android:minWidth="36dp"
                    android:layout_marginLeft="10dp" />

            </LinearLayout>

        </TableRow>

    </LinearLayout>
    </LinearLayout>
</RelativeLayout>
    <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/linearLayout">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/btnVjezbePrsaPotisakSKlupe"
        android:minHeight="72dp"

        android:clickable="true"

        android:background="?attr/selectableItemBackground">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical">