ClassCastException异常在我的应用 [英] ClassCastException in my application

查看:167
本文介绍了ClassCastException异常在我的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我试图让使用onItemClick的OnItemClickListener接口方法的ListView项的文本。问题是simpleCursorAdaptor的布局有3 TextViews,我如何才能只TextViews之一的文字?我得到一个ClassCastException异常。我怎样才能提取所有布局中的3 textViews的内容。这里是我的code

I am having a problem, I am trying to get the text of a listView Item using onItemClick method of the OnItemClickListener interface. The problem is the layout of the simpleCursorAdaptor has 3 TextViews, how do I get the text of only one of those TextViews? I am getting an ClassCastException. How Can I extract the contents of all the 3 textViews in the layout. Here is my code

package com.messageHider;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class sms extends Activity implements OnItemClickListener
{
ListView listViewSMS;
Uri smsUri=Uri.parse("content://sms/inbox");
Cursor cursor;
SimpleCursorAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sms);
    listViewSMS=(ListView)findViewById(R.id.listViewSMS);
    listViewSMS.setOnItemClickListener(this);
}
@Override
protected void onStart() {
    super.onStart();
    getsms();
}
private void getsms()
{
    cursor=getContentResolver().query(smsUri,null, null, null, null);
    String[]from={"person","body","date"};
    int[]to={R.id.textViewPerson,R.id.textViewBody,R.id.textViewDate};
    adapter=new SimpleCursorAdapter(this,R.layout.smslist, cursor, from, to);
    listViewSMS.setAdapter(adapter);
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    Toast.makeText(getApplicationContext(),((TextView) v).getText(), Toast.LENGTH_LONG).show();
    }
}

下面是布局适配器

<?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_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="match_parent">
        <TextView android:text="TextView" android:layout_height="wrap_content" android:padding="5dp" android:layout_width="wrap_content" android:id="@+id/textViewPerson"></TextView>
        <TextView android:text="TextView" android:layout_height="wrap_content" android:layout_width="fill_parent"  android:id="@+id/textViewDate" android:gravity="right" android:paddingRight="5dp"></TextView>
    </LinearLayout>
    <TextView android:text="TextView" android:id="@+id/textViewBody" android:layout_height="wrap_content" android:height="60dp" android:padding="5dp" android:layout_width="fill_parent"></TextView>
</LinearLayout>

我将AP preciate的帮助。

I will appreciate the help.

推荐答案

您收到整个的LinearLayout作为参数v。
你应该尝试v.findViewById(),然后使用这个TextView的。

You receive the whole LinearLayout as the parameter v. You should try v.findViewById() and then use this textview.

这篇关于ClassCastException异常在我的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆