如何从自定义列表视图中获取所选项目并在Toast消息中打印?

问题描述 投票:0回答:1

在这里,我试图从自定义列表视图中选择项目,以便在下一个活动中显示它们,但我不知道如何从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>

您提供的解决方案,对我来说将是非常有帮助的

android xml listview
1个回答
0
投票

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

如果你想拥有复选框和所有花哨的东西,那么你将不得不创建一个自定义BaseAdapter,为它提供一个列表和一个XML的单元格,然后在那里进行所有处理并从适配器中检索列表你移动到下一个片段

© www.soinside.com 2019 - 2024. All rights reserved.