1

I have two problems in making a ListView in Android studio, first of all, I have a layout in which I want to make a ListView with around 100 elements that I can click on and take me to another layout.

1. This is the content of my layout:

<?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="#ffff7e00">


    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"
        tools:listitem="@android:layout/simple_list_item_2" />

</RelativeLayout>

In my Preview, next to the XML code, a list of default elements appear, but when I test it in the emulator, is not there, why?

2. I don't know how to edit the ListView in the java file, and how to set the number of elements I want there to be, and how to make them lead me to a respective layout.

pdsafs df apk
  • 741
  • 1
  • 6
  • 9

2 Answers2

1

Hi pdsafs df apk I answer post with similar problem do a long time, my post it's complet read this, when I said a custom layout, you need to create a layout for all rows, if yout need a list view that contains in each rows one button and two text views then create a layout with this controls and put this layout inside adapter with this code that you can find in this url: how to use an ArrayAdapter in android of custom objects

In this post you can find too how create a correct click event in listview or other methods or controls!!

You need to declare your custom adapter in MainActivity, example:

//Declaration of variables
Custom_Adapter adapter = new Custom_Adapter(getActivity(), yourParams... );

//Put adapter inside listview
yourListView.setAdapter(adapter);

Please after that post a new Question find a little in this big community a lot of people programming with android and u can find a similar problems... I wait I can helps pdsafs df apk with my answer, and remember help this community with give a check for a correct answer or points if the people helps you. Good luck!

Community
  • 1
  • 1
0

This may sound daunting at first, but it's not too bad. You're going to create an ArrayList with the data you want to populate each row with. Then, using an ArrayAdapter, you'll populate the ArrayList and fill up a custom layout. Then, you'll want to override the getView() method and have it add an onClickListener to each layout that'll take it to the respective class that you provide.

While I could provide code, it really isn't necessary, as Android provides a step-by-step tutorial on how to do this here: http://developer.android.com/guide/topics/ui/layout/listview.html

Alex K
  • 8,269
  • 9
  • 39
  • 57
  • I will do that, thanks, but I still don't know why was my layout not seen in the emulator. – pdsafs df apk Dec 21 '14 at 16:14
  • No errors, With that I placed in the xml file, the Preview showed me a list of elements, I went to the emulator expecting to see the list, but I didn't – pdsafs df apk Dec 21 '14 at 16:25
  • 1
    The preview is just an example screen. By specifying the `tools:listitem` attribute, you are simply asking the IDE to render what that layout would look IF you used that layout in code. Even if you didn't specify that attribute, it's normal for the preview screen to show some random content filling the `ListView`. It's simply to show how it may look. It bears no meaning to what you will actually see when running the app. – Ifrit Dec 22 '14 at 01:34