2

我正在尝试ToastListView.

listView1.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> a, View v, int pos, long id){
          // Now you have the id, you can set the background colour.
         Toast.makeText(getBaseContext(),((TextView)v).getText(), Toast.LENGTH_LONG).show();
    }
});

我得到的错误:

Error: android.widget.LinearLayout cannot be cast to android.widget.TextView
4

1 回答 1

4

如果您的ListView行布局不只包含 aTextView您将得到该异常(TextView不能用 aLinearLayout或其他东西包装)。相反,您可以这样做:

public void onItemClick(AdapterView<?> a, View v, int pos, long id){
    LinearLayout parent = (LinearLayout) v;
    TextView t = (TextView) parent.findViewById(R.id.the_id_of_the_textview);
    Toast.makeText(getBaseContext(), t.getText(), Toast.LENGTH_LONG).show();
}
于 2012-04-09T11:29:51.153 回答