Cannot get Item position in ListView on first Activity to setText in Action bar title on second Activity

I have Tow Activities, and I want to get Item position in ListView on first Activity to setText in Action bar on second Activity. here the code:

Adapter.java

package com.cambobox.actionbartitle.actionbar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


public class Adapter extends ArrayAdapter<String> {
public Context context;
public String[] value;
public Adapter(Context context, String[] value){
    super(context, R.layout.song_list,value);
    this.context=context;
    this.value=value;
}
@Override
public View getView(int position, View convertView,ViewGroup parent){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.song_list,parent,false);
    TextView textView = (TextView) rowView.findViewById(R.id.song_list);
    textView.setText(value[position]);
    return rowView;
}
}

SongList.java (first activity)

package com.cambobox.actionbartitle.actionbar;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;


public class SongList extends ListActivity{
public  static String SELECTED="SELECTED";
String[] song_resource;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.song_list);
    //getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    //ActionBar actionBar = getActionBar();
    song_resource = getResources().getStringArray(R.array.song_list);
    setListAdapter(new Adapter(this, song_resource));

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    SELECTED = (String) getListAdapter().getItem(position);
    Intent intent = new Intent(this, Song.class);
    intent.putExtra("name",SELECTED);
    startActivity(intent);
}
}

Song.java (second activity)

package com.cambobox.actionbartitle.actionbar;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;


public class Song extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_song);
    Intent intent = getIntent();
    String selected = intent.getStringExtra(SongList.SELECTED);
    ActionBar get_actionbar = getActionBar();
    get_actionbar.setTitle(selected);
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cambobox.actionbartitle.actionbar">

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".SongList"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Song"></activity>
</application>

Answers


Hi i have correct your wrong code to this

package com.cambobox.actionbartitle.actionbar;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class SongList extends ListActivity{
public  static String NAME="Name";
private String name;
String[] song_resource;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 song_resource = getResources().getStringArray(R.array.song_list);
 setListAdapter(new Adapter(this, song_resource));

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
name = (String) getListAdapter().getItem(position);
Intent intent = new Intent(this, Song.class);
intent.putExtra(NAME,name);
startActivity(intent);

}

}

and in the song activity

public class Song extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_song);
Intent intent = getIntent();
String selected = intent.getStringExtra(SongList.NAME);
ActionBar get_actionbar = getActionBar();
get_actionbar.setTitle(selected);

}

}

i think it will work


Need Your Help

How to decode route points from the JSON Output Data?

ios xcode json google-maps mkmapview

I am new in the ios field, Now i am trying to implement MKMapView Based iPhone Application. From the Google Map Web web service i Fetch the Data in JSON Output Format.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.