如何从自定义ListView中获取所选项目并在Toast消息中打印出来



在这里,我试图从自定义列表视图中获取选定的项目,以在下一个活动中显示它们,但我对如何从Custum列表查看中获取所选项目有任何了解提供我项目的代码。product.java(片段)

`package com.example.chavda.agency_managment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class Product extends Fragment {
    ListView product_list;

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // getActivity().setTitle(R.string.attandance);
        View v = inflater.inflate(R.layout.product, container, false);
        product_list = v.findViewById(R.id.product_list);
        final downloader d = new downloader(getContext(), product_list);
        d.execute();
        ArrayList<SingleRow> list;
        final CustomHelper p = new CustomHelper();

        product_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                try {
                    //SingleRow temp = p.list.get(i);
                    Toast.makeText(getContext(), "" + product_list.getItemIdAtPosition(i), Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    Toast.makeText(getContext(), "" + e, Toast.LENGTH_SHORT).show();
                }

            }
        });
        return v;
    }

single_row_product.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:layout_marginTop="20dp"
    android:focusable="false">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:background="@drawable/rounded_edittext"
        android:focusable="false"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/productimage"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_marginLeft="10dp"
            android:focusable="false" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:orientation="vertical">
            <TextView
                android:id="@+id/namep"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignStart="@+id/sizep"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:focusable="false"
                android:text="TextView"
                android:textSize="15dp" />
            <TextView
                android:id="@+id/sizep"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:focusable="false"
                android:text="TextView"
                android:textSize="15dp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:focusable="false"
            android:orientation="vertical">
            <TextView
                android:id="@+id/stock"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:focusable="false"
                android:text="TextView"
                android:textSize="15dp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:orientation="vertical">
            <TextView
                android:id="@+id/prize"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignStart="@+id/sizep"
                android:layout_marginLeft="20dp"
                android:focusable="false"
                android:text="TextView"
                android:textColor="#000000"
                android:textSize="25dp" />
            <TextView
                android:id="@+id/category"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:focusable="false"
                android:text="TextView"
                android:textSize="15dp" />
        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

这是product.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/product_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></ListView>
</LinearLayout>

您提供的解决方案,这对我很有帮助

如果要点击每个项目,然后单击一个按钮移动到下一个片段并显示所有选定的片段,那么您只需要保留选定项目的列表,然后添加/删除项目在您的on LISLISTITEMPLICK上。

如果您想拥有复选框和所有精美的东西,那么您将必须创建一个自定义的baseadapter,为单元格提供列表和XML转移到下一个片段之前的适配器

最新更新