Pregunta

In my android application (almost finished) i have a list view to display the name of the cheat code's, when the user click the name i want the text to change
Ex:
Before - Click Me
After- You Clicked Me
I am not sure if i am doing this right , if a OnClick popup would be easier any links or comments/ answers will be highly appriciated


google to look for the following keywords but the results didnt give me what i was looking for

"Android Listview", "Android Listview popup", "Android Listview Onclick", "Listview Onclick", "Make android app text change when clicked", "make android listview results change when clicked"


cheatcodes.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/app_bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="20sp" >
</TextView>

</RelativeLayout>

CheatCodes.java

package grand.theft.auto.v;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class CheatCodes extends ListActivity {

static final String[] Cheatcodes = new String[] { "Wanted Level Up", "Wanted Level Down", "Recharge Ability",
        "Fast Run", "Fast Swim", "Parachute", "Explosive Punch", "Flaming Bullets",
        "Slow Motion Aim", "Sky Fall", "Buzzard Attack Helicopter",   "Comet", "Sanchez", "Trash Master",
        "Limo", "Stunt Plane", "PCJ-600", "Caddy", "Rapid GT", "Duster"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setListAdapter(new ArrayAdapter<String>(this,  R.layout.cheatcodes,Cheatcodes));

    ListView listView = getListView();
    listView.setTextFilterEnabled(true);

    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // When clicked, show a toast with the TextView text
            Toast.makeText(getApplicationContext(),
            ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
        }
    });

}

}
¿Fue útil?

Solución

Try this inside the onItemClick:

CharSequence text= (CharSequence) parent.getItemAtPosition(pos);
//This is where you change the text to show
text = "You " + text;
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
//If you want to update the listview
Cheatcodes[pos] = text;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top