开发者

ClassCastException in my application

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 开发者_如何学Pythonlayout 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();
    }
}

Here is the layout for the adaptor:

<?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>

I will appreciate the help.


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


Hi in your activity code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sms);
    listViewSMS=(ListView)findViewById(R.id.listViewSMS);
    listViewSMS.setOnItemClickListener(this);
}

You have initialized a listview but there is no such listview in your XML file. Are you sure you are using correct layout XML file in your setContentView method?

Another observation can be: have you posted correct layout XML file here in the question?


Try this

HashMap<String, String> select = (HashMap<String, String>) parent.getItemAtPosition(position);
         String person = select.get("person");
         String body = select.get("body");
         String date = select.get("date");

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜