Android - How to create an interactive list?

I want all the list items in the list to appear on a new page, so each list item opens on a new black page that I can use. I do not know how to implement this. I searched the clock and cannot find the answer to my solution. It would be very appreciated if someone could show and / or explain how to do this, rather than provide a link, but also useful.

Here is my code:

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

  <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 can I go from here to make every item in my list accessible and accessible to open on a new page?

Thanks in advance!

EDIT:

Logcat is no longer suitable.

+11
java android eclipse listview clickable
source share
5 answers

This is actually pretty simple:

This is your activity using ListView, it implements 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 will be placed in your MainActivity.java. I changed the class name to MainActivity , and the contentView to setContentView(R.layout.activity_main) - the names are the names of the newly created Android project in Eclipse.
See also two new lines in the section // * * Edit * - they will install the Listener for clicks on items in the list.

Your activity_main.xml file 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> 

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> 

NB: If you copy and paste this code, it should work. But you will get an error by clicking on the item because you have not created ListItemDetail.class .

Here is an example of how it might 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 this code, it will work.

+31
source share

you can fill the list from an array in string.xml as shown

 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 listview items is pretty simple and convenient. 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); } }); 
+1
source share

You use the onListItemClick function to set your intent, which loads the next action and transfers any data.

 public void onListItemClick(ListView parent, View view, int position, long id) { Intent intent = new Intent(this, AnotherActivity.class); intent.putExtra("position", position); startActivity(intent); } 
0
source share

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); } } }); 
0
source share
  1. Inside frag.java with an array adapter

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

  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); } }); 
0
source share

All Articles