android - 从自定义 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();
}
});
解决方案
尝试这个,
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();
}
});
推荐阅读
- regex - 在Postgresql中使用正则表达式从字符串中提取前两个单词排除逗号
- django - 如何编写使用正则表达式的 django 视图测试用例
- java - 如何将图像缩小到窗口?
- angular - 如何通过Angular在json中存储secondtable值的创建值
- c# - 通过 c# 读取 Azure Blob 并插入到本地 SQL Server
- php - get value attribute of hidden input field in symfony controller
- c# - C# LINQ 组按属性集合,然后按列表定义的显式顺序对每个组进行排序
- react-native - ReferenceError:找不到变量:电子邮件
- java - spring security 正在验证所有请求,即使它们被允许
- css - Footer in Next.js