【问题标题】:How to get selected items from custom listview and print that in toast message?如何从自定义列表视图中获取选定项目并在 toast 消息中打印?
【发布时间】:2019-07-19 20:39:24
【问题描述】:

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

这里是 产品.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】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 2015-12-20
      相关资源
      最近更新 更多