-2

I want to send the name of the movie from MainActivity(ListView) to SongActivity. This is what I've tried.

MainActivity.java

public class MainActivity extends Activity{
  Activity context;
   HttpPost httppost;
   StringBuffer buffer;
   HttpResponse response;
   HttpClient httpclient;
   ProgressDialog pd;
   CustomAdapter adapter;
   ListView listProduct;
   ArrayList<String> records;
protected void onCreate(Bundle savedInstanceState) {
   //TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   context=this;
   records=new ArrayList<String>();
   listProduct=(ListView)findViewById(R.id.product_list);
   adapter=new CustomAdapter(context, R.layout.list_item,R.id.pro_name, records);
   listProduct.setAdapter(adapter);
   listProduct.setOnItemClickListener(new OnItemClickListener(){
       @Override
       public void onItemClick(AdapterView<?> arg0, View v, int position, long id){

           String sText = ((TextView) v).getText().toString();
           Intent songIntent = new Intent(getApplicationContext(), SongActivity.class);
           songIntent.putExtra("movie_name", sText );
           startActivity(songIntent);

       }

   });
   }

This is my SongActivity.java

package com.example.telugump3;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SongActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.song_activity);
        TextView tt = (TextView)findViewById(R.id.testing);
        Intent iin= getIntent();
        Bundle b = iin.getExtras();

        if(b!=null)
        {
            String j =(String) b.get("movie_name");
            tt.setText(j);
        }

    }

logcat

03-22 17:17:20.949: E/IMGSRV(22747): :0: PVRDRMOpen: TP3, ret = 46
03-22 17:17:20.959: E/IMGSRV(22747): :0: PVRDRMOpen: TP3, ret = 50
03-22 17:17:20.959: E/IMGSRV(22747): :0: PVRDRMOpen: TP3, ret = 51
03-22 17:17:20.959: E/IMGSRV(22747): :0: PVRDRMOpen: TP3, ret = 51
03-22 17:17:20.959: E/IMGSRV(22747): :0: PVRDRMOpen: TP3, ret = 51
03-22 17:17:20.969: E/IMGSRV(22747): :0: PVRDRMOpen: TP3, ret = 53
03-22 17:17:20.999: D/OpenGLRenderer(22747): Enabling debug mode 0
03-22 17:17:23.149: I/dalvikvm(22747): Total arena pages for JIT: 11
03-22 17:17:23.149: I/dalvikvm(22747): Total arena pages for JIT: 12
03-22 17:17:23.149: I/dalvikvm(22747): Total arena pages for JIT: 13
03-22 17:17:23.149: I/dalvikvm(22747): Total arena pages for JIT: 14
03-22 17:17:23.149: I/dalvikvm(22747): Total arena pages for JIT: 15
03-22 17:17:23.149: I/dalvikvm(22747): Total arena pages for JIT: 16
03-22 17:17:23.159: I/dalvikvm(22747): Total arena pages for JIT: 17
03-22 17:17:23.159: I/dalvikvm(22747): Total arena pages for JIT: 18
03-22 17:17:23.159: I/dalvikvm(22747): Total arena pages for JIT: 19
03-22 17:17:23.159: I/dalvikvm(22747): Total arena pages for JIT: 20
03-22 17:17:25.289: D/AndroidRuntime(22747): Shutting down VM
03-22 17:17:25.289: W/dalvikvm(22747): threadid=1: thread exiting with uncaught exception (group=0x430ef140)
03-22 17:17:25.299: E/AndroidRuntime(22747): FATAL EXCEPTION: main
03-22 17:17:25.299: E/AndroidRuntime(22747): Process: com.example.telugump3, PID: 22747
03-22 17:17:25.299: E/AndroidRuntime(22747): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
03-22 17:17:25.299: E/AndroidRuntime(22747):    at com.example.telugump3.MainActivity$1.onItemClick(MainActivity.java:51)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at android.widget.AdapterView.performItemClick(AdapterView.java:299)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at android.widget.AbsListView.performItemClick(AbsListView.java:1158)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2949)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at android.widget.AbsListView$3.run(AbsListView.java:3683)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at android.os.Handler.handleCallback(Handler.java:733)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at android.os.Handler.dispatchMessage(Handler.java:95)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at android.os.Looper.loop(Looper.java:149)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at android.app.ActivityThread.main(ActivityThread.java:5257)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at java.lang.reflect.Method.invokeNative(Native Method)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at java.lang.reflect.Method.invoke(Method.java:515)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
03-22 17:17:25.299: E/AndroidRuntime(22747):    at dalvik.system.NativeStart.main(Native Method)
03-22 17:17:27.259: I/Process(22747): Sending signal. PID: 22747 SIG: 9

I've tried removing putExtra() and the SongActivity launched perfectly. But if I use putExtra(), the app crashes when I click on any item in the ListView. I just want the text of clicked item so that I can use it in other Activity.

Shahid Roshan
  • 105
  • 3
  • 14

6 Answers6

0

Try: String j =(String) b.getString("movie_name");

Source

Community
  • 1
  • 1
fida1989
  • 3,234
  • 1
  • 27
  • 30
0

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView

You are casting a RelativeLayout to a TextView. Check your findViewById() calls if there is such a casting or you are using the wrong id

sockeqwe
  • 15,574
  • 24
  • 88
  • 144
  • changed the listitem layout from RelativeLayout to TextView...it worked!! – Shahid Roshan Mar 22 '15 at 12:31
  • 1
    While it's working it's a bad practice. You have a perfect data set of strings which you can use (`records.get(position)`) that are fed into the adapter which the list view uses (`adapter.getItem(position).toString()`) and yet you choose to pick your data from a text view - the presentation layer. – Eugen Pechanec Mar 22 '15 at 12:44
0

While sending do this

 intent.putExtra("message", message);

and at the time of receiving

  Intent intent = getIntent();
  String message = intent.getStringExtra("message");
Setu Kumar Basak
  • 11,460
  • 9
  • 53
  • 85
0

The argument v of onItemClick is root layout of your listview item. It's not your TextView. So you have to modify like below.

//String sText = ((TextView) v).getText().toString();    
String sText = ((TextView) v.findViewById(R.id.[your TextView id])).getText().toString();
Lee Jeongmin
  • 793
  • 11
  • 22
0

Ops,You do not have a problem with the put or get Extra ,Pay attention the error your problem is that target clicked is a Layout,and a Layout cannot cast to the TextView

0

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView

at com.example.telugump3.MainActivity$1.onItemClick(MainActivity.java:51)

As you can see your problem has nothing to do with extras, but you know where to look, so let's take a look at how you declare your item adapter in the MainActivity

adapter = new CustomAdapter(context, R.layout.list_item,R.id.pro_name, records);

I'm going to assume the parameters are following: context, layout id, text view id, data set. So the id of a TextView in an item layout is R.id.pro_name.

Now let's see what happens when in your OnItemClickListener:

@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id){
    // arg0 is the ListView
    // v is the whole item layout

    String sText = ((TextView) v).getText().toString();
    // you assumed that the whole layout was a TextView

    // ...
}

Now you get a pretty good idea of where the problem is. You have two options:

Option 1) The quick and dirty.

String sText = ((TextView) v.findViewById(R.id.pro_name).getText().toString();

Why is this bad? You have to write R.id.pro_name on two places. You're mixing presentation and model layers of your app together - you're not asking the data set, you're asking someone who already depends on the data set.

Option 2) The way to go.

String sText = adapter.getItem(position).toString();

You ask for data the same adapter which the list view uses.

Community
  • 1
  • 1
Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124