0

Okay, so I have an app with a few tutorials.(around 20) (Each tut is just some text filled in an activity screen)

I want to create a button on the main menu, that points to a listview activity containing the names of all the tutorial's activities - alphabetically.

Not perfect navigation, I know. But I think it's the best quick and easy solution till I learn more.

Here's the problem: I just want to say find out which listview item was clicked, and put a normal onclick() which opens up the corresponding tutorial activity.

How exactly do I set up the listview ? I know how the normal onclick works... but how to do it with a listview item ?

I came here from https://groups.google.com/forum/#!forum/android-developers

It said we can ask beginner questions here, but people seem to rather spend their precious time being mean. Is it really worth it ? If you know pls help me out. (A code example would be great, but any help would be appreciated)

Marek Sebera
  • 38,635
  • 35
  • 154
  • 241

3 Answers3

2

This example shows how to initialize a listView named list1 and defined in a layout-file res\layout\main.xml

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    ListView list = (ListView) findViewById(R.id.list1);
    String[] days = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, days);
    list.setAdapter(adapter);

    /**
      * go to next activity for detail image
      */
    list.setOnItemClickListener(new OnItemClickListener() {

       @Override
       public void onItemClick(AdapterView<?> parent, View v, final int position, long id) {
           System.out.println(v+position);
           Intent intent = new Intent(A.this, B.class);
           startActivity(intent);
        }
    });
}

Here are two more examples:

Alexander Pacha
  • 8,494
  • 3
  • 64
  • 100
Manish Srivastava
  • 1,810
  • 2
  • 15
  • 23
  • Hey there !...When you defined listview, I don't have that R."id"... so it showed cannot be resolved, I changed it to R.layout.list, which is the layout of my activity in which I want the list... Also, I had some error with the onItemClick,So I deleted the listener entirely, it's not a vital function is it ? Since everytime I open up this activity the app crashes... Any idea as to how to fix it ? –  Jul 19 '13 at 12:52
  • You have to post the explicit exception that is thrown (and logged in LogCat (see http://stackoverflow.com/questions/7188115/what-is-logcat-in-eclipse for how to use LogCat). – Alexander Pacha Jul 19 '13 at 21:10
  • Its seem you are very new in coding.. You need some training I think no one can fix these small bug.. – Manish Srivastava Jul 20 '13 at 05:45
  • You are able to display item in list view? If yes just use list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View v, final int position, long id) { System.out.println(v+position); Intent intent = new Intent(A.this, B.class); startActivity(intent); } }); And run your project and see log cat as Alexander suggest you a link.. – Manish Srivastava Jul 20 '13 at 05:46
0

You have to use

listview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            // start a new activity here        
        }
    });

where position is the position of the item in the list.

Alexander Pacha
  • 8,494
  • 3
  • 64
  • 100
shreyas
  • 2,146
  • 3
  • 17
  • 30
0

Take a look at this beautiful Introduction to Android ListViews. It contains extensive explanations and example source-code that can be downloaded.

Of course you can ask beginners question as long as they state a clear question to a problem that has not been solved many times before. If an answer to your question can be found by a simple web-search, you should do this research effort before asking it on Stackoverflow.

Alexander Pacha
  • 8,494
  • 3
  • 64
  • 100
  • Thank you, I was able to atleast get a list from this, Now reading up on the onclick().. –  Jul 19 '13 at 13:27