java - 自定义 ListView onItemClickListener

标签 java android listview

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

列表

public class VjezbeLista extends AppCompatActivity {
    ListView listaVjezbe;
    ArrayAdapter<String> adapter;

    String[] misicneSkupine = {"Prsa",
            "Leđa",
            "Ramena",
            "Biceps",
            "Triceps",
            "Trbuh",
            "Kvadriceps",
            "Loža",
            "Listovi"};

    Integer[] imageId = {R.drawable.ic_vjezbe_prsa,
            R.drawable.ic_vjezbe_ledja,
            R.drawable.ic_vjezbe_ramena,
            R.drawable.ic_vjezbe_biceps,
            R.drawable.ic_vjezbe_triceps,
            R.drawable.ic_vjezbe_trbuh,
            R.drawable.ic_vjezbe_kvadriceps,
            R.drawable.ic_vjezbe_loza,
            R.drawable.ic_vjezbe_listovi};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vjezbe_lista);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);



        AdapterListaVjezbe adapter = new AdapterListaVjezbe(VjezbeLista.this, misicneSkupine, imageId);
        listaVjezbe = (ListView) findViewById(R.id.listaVjezbe);
        listaVjezbe.setAdapter(adapter);
        listaVjezbe.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0){
                    Intent intent = new Intent(VjezbeLista.this, VjezbeListaPrsa.class);
                    startActivity(intent);
                } // Here will go else if etc...

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_pretraga, menu);
        return 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;
    }
}

和行布局

<?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();

关于java - 自定义 ListView onItemClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35688023/

相关文章:

java - java中如何仅在前一个函数完全执行后才调用一个函数

java - 如何在 Realm 浏览器中查看我的 Realm 文件?

android - 结合使用 SurfaceTexture 和 RenderScript

xaml - Windows8 ListView 和项目之间的空间

java - ASM 根据 xml 中定义的规则解析 .class

java - Hadoop-MapReduce 的小型数据集

android - 显示软键盘时向上移动布局

android - 使用 faSTLane 重命名移动项目?

android - ListView Android无法显示项目

java - Android 2.3 使用 Fragment 时保持 ListView 位置