0

这是相关代码:

super.onCreate(savedInstanceState);
          this.requestWindowFeature(Window.FEATURE_NO_TITLE);                   

          // storing string resources into Array
          String[] menulist = getResources().getStringArray(R.array.menulist);      

          // Binding resources Array to ListAdapter
          this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, menulist));


          ListView lv = getListView();

          // listening to single list item on click
          lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

                // selected item
                String selected = ((TextView) view).getText().toString();

                Toast toast=Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT);//This is where the error shows  
                toast.show();

                // Launching new Activity on selecting single List Item
                /*Intent i = new Intent(getApplicationContext(), SingleListItem.class);
                // sending data to new activity
                i.putExtra("product", product);
                startActivity(i);*/

            }
          });

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/listgradient" 
        android:clickable="true"      
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold" >
</TextView>

我保留了clickable属性true。但是Toast没有显示。我哪里错了?

4

2 回答 2

2

我认为你的错误在这里:

 String selected = ((TextView) view).getText().toString();

您应该使用变量“位置”来获取列表数据的值。

于 2013-01-17T06:32:03.670 回答
1

试试这个:

String selected = ((TextView) view.findViewById(R.id.your_textView_item_id)).getText().toString();
于 2013-01-17T06:35:36.087 回答