首页 > 解决方案 > 从自定义 ListView 点击获取项目

问题描述

我有一个自定义 ListView,里面有 4 个文本视图。ListView 中的值是从数据库中填充的。

这是它的样子:

在此处输入图像描述

这是xml文件:

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

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

        <TextView
            android:id="@+id/budgetCategoryLV"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1.3"
            android:gravity="center"
            android:padding="5dp"
            android:text="Category"
            android:textSize="13sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/expenseBudgetLV"
            android:layout_width="14dp"
            android:layout_height="50dp"
            android:layout_weight="0.9"
            android:gravity="center"
            android:padding="5dp"
            android:text="Expense Budget"
            android:textSize="13sp" />

        <TextView
            android:id="@+id/budgetSpentLV"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="0.9"
            android:gravity="center"
            android:padding="5dp"
            android:text="Spent"
            android:textSize="13sp" />

        <TextView
            android:id="@+id/totalBalanceLV"
            android:layout_width="2dp"
            android:layout_height="50dp"
            android:layout_weight="0.9"
            android:gravity="center"
            android:padding="5dp"
            android:text="Total Balance"
            android:textSize="13sp" />
    </LinearLayout>    </LinearLayout>

我想要做的是单击 ListView 中的一个项目,并将其显示在 Toast 消息中。

这是我到目前为止所做的一个片段,但它对我不起作用:

 currentBudgetListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String value = currentBudgetListView.getItemAtPosition(position).toString(); //this does not work
                        TextView tv1 = (TextView) findViewById(R.id.budgetCategoryLV); // this gives the default value of the textview
                        TextView tv2 = (TextView) findViewById(R.id.expenseBudgetLV);
                        TextView tv3 = (TextView) findViewById(R.id.budgetSpentLV);
                        TextView tv4 = (TextView) findViewById(R.id.totalBalanceLV);
                        String text1 = tv1.getText().toString();
                        String text2 = tv2.getText().toString();
                        String text3 = tv3.getText().toString();
                        String text4 = tv4.getText().toString();
                        Toast.makeText(ExpensesBreakdownActivity.this, text1 + " " + text2 + " "  + text3 + " " + text4 + " " + value , Toast.LENGTH_LONG).show();
                    }
                });

标签: androidlistview

解决方案


尝试这个,

currentBudgetListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                           final CurrentBudgetList user = (CurrentBudgetList)parent.getAdapter().getItem(position).getName();

                    Toast.makeText(ExpensesBreakdownActivity.this, ""+name , Toast.LENGTH_LONG).show();


                    }
                });

推荐阅读