我有一个自定义ListView,其中包含4个textview。从数据库中填充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();
}
});
答案 0 :(得分:0)
尝试一下,
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();
}
});