Android - How to create clickable listview?
Asked Answered
E

5

14

I want to make all my list items in the listview open up into a new page, so each listview item opens up onto a new black page that I can use. I don't know how to implement this at all. I have searched for hours on end and can't find an answer to my solution. It would be much appreciated if someone could show and/or explain how to do this instead of providing a link, but either is helpful.

Here is my code so far:

  <string-array name="sections">
    <item >Pro Constructive</item>
    <item >Con Constructive</item>
    <item >1st Speaker Cross</item>
    <item >Pro Rebbutal</item>
    <item >Con Rebuttal</item>
    <item >2nd Speaker Cross</item>
    <item >Pro Summary</item>
    <item >Con Summary</item>
    <item >Grand Cross</item>
    <item >Pro Final Focus</item>
    <item >Con Final Focus</item>
</string-array>

This is in my string.xml

    <ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:entries="@array/sections" >
</ListView>

This is in my activity_main.xml.

Where do I go from here to make each item in my list clickable and able to open up onto a new page?

Thanks in advance!

EDIT:

Logcat no longer relevant.

Ehtelehud answered 8/11, 2012 at 1:37 Comment(0)
C
34

In fact it is quite easy:

This is your Activity with the ListView, it implements an OnItemClickListener:

public class MainActivity extends Activity implements OnItemClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        //* *EDIT* * 
        ListView listview = (ListView) findViewById(R.id.listView1);
        listview.setOnItemClickListener(this);
    }

    public void onItemClick(AdapterView<?> l, View v, int position, long id) {
        Log.i("HelloListView", "You clicked Item: " + id + " at position:" + position);
            // Then you start a new Activity via Intent
            Intent intent = new Intent();
            intent.setClass(this, ListItemDetail.class);
            intent.putExtra("position", position);
            // Or / And
            intent.putExtra("id", id);
            startActivity(intent);
    }

Edit

The above code would be placed in your MainActivity.java. I changed the name of the class to MainActivity and the contentView to setContentView(R.layout.activity_main) - The names are those of a freshly created Android Project in Eclipse.
Please see also the 2 new lines under //* Edit * - those will set the Listener for clicks on items in the list.

Your activity_main.xml should look like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:entries="@array/sections" >
    </ListView>
</RelativeLayout>

The array.xml (not string.xml) in your `res/values/` folder looks like this

<resources>
    <string-array name="sections">
        <item >Pro Constructive</item>
        <item >Con Constructive</item>
        <item >1st Speaker Cross</item>
        <item >Pro Rebbutal</item>
        <item >Con Rebuttal</item>
        <item >2nd Speaker Cross</item>
        <item >Pro Summary</item>
        <item >Con Summary</item>
        <item >Grand Cross</item>
        <item >Pro Final Focus</item>
        <item >Con Final Focus</item>
    </string-array>
</resources>

N.B.: If you copy & paste this code it should work. But you will get an error by clicking on an Item because you haven't created the ListItemDetail.class yet.

Here is an example of how this could look:

Your ListItemDetail.java:

public class ListItemDetail extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listitem);

    Intent intent = getIntent();
    int position = intent.getIntExtra("position", 0);

    // Here we turn your string.xml in an array
    String[] myKeys = getResources().getStringArray(R.array.sections);

    TextView myTextView = (TextView) findViewById(R.id.my_textview);
    myTextView.setText(myKeys[position]);


    }

}

And its activity_listitem.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/my_textview"/>

</LinearLayout>

If you copy past this code it will work.

Chrysostom answered 8/11, 2012 at 1:45 Comment(8)
I'm sorry, I'm new to this. Would I place this in the string.xml or activity_main.xml, and would I be replacing anything in this with my own information?Ehtelehud
Okay so like you said, it gives me an error, but I did add the activity_listitem.xml and ListItemDetail.java and I don't know why the app FC's, especially because I have no errors.Ehtelehud
You did create the array.xml file in /res/values/ - Without error it's just guessing : / Tried my code again in eclipse and it worksChrysostom
To make it easier i zipped the files: ego.lu/stuff/listview.zip - Scan with www.virustotal.com to be sure it's save ;) - The .java files and the .xml files are in thereChrysostom
Alright, I'll compare yours to mine and report back later. Thanks for even doing this though, you've been a real help.Ehtelehud
Oh and verify you have included the class in your manifest: <activity android:name=".ListItemDetail"></activity> (I might have left it named TextActivity)Chrysostom
I added my logcat error for you to see. Maybe I need to declare something in the Manifest?Ehtelehud
Ok so I verified the class in my manifest like you said, and that fixed the problem except for that now my array doesn't show up.Ehtelehud
A
1

you can populate listview from array in string.xml as like this

String[] myKeys = getResources().getStringArray(R.array.sections);
ListView mListView = (ListView)findViewById(R.id.listView1);
mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myKeys));  

and clicking on the listview items is quite simple and easy method.just use setOnItemClickListener

mListView.setOnItemClickListener(new OnItemClickListener() 
{
    public void onItemClick(AdapterView<?> arg0,View arg1, int position, long arg3) 
    {

        Intent n = new Intent(getApplicationContext(), yourclass.class);
        n.putExtra("position", position);
        startActivity(n);
    }
});
Aquamarine answered 8/11, 2012 at 2:52 Comment(0)
S
1

listView= (ListView) findViewById(R.id.listview);

  • List item

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
            if(position==0){
                Intent i=new Intent(MainActivity.this,Main3Activity.class);
                startActivity(i);
            }
        }
    });
    
Sutter answered 2/12, 2016 at 6:14 Comment(0)
P
0

You use the onListItemClick function to set up your Intent that loads the next activity and passes any data across.

public void onListItemClick(ListView parent, View view, int position, long id)
{
    Intent intent = new Intent(this, AnotherActivity.class);
    intent.putExtra("position", position);
    startActivity(intent);
}
Penstemon answered 8/11, 2012 at 1:44 Comment(0)
S
0
  1. Inside fragment.java with Array Adapter

String[] menuItems = {"Default C02", "Default 02 "};

    ListView listView = (ListView) root.findViewById(R.id.main_menu);

    ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(
        getActivity()
        ,android.R.layout.simple_list_item_1
        ,menuItems
    );

    listView.setAdapter(listViewAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        public void onItemClick(AdapterView<?> l, View v, int position, long id){
            Log.i("menuItems", "You clicked Item: " + id + " at position:" + position);



        }
    });
Selfassured answered 9/9, 2019 at 10:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.