0

I want to display a very simple Key->value in Listview by using HashMap and simpleAdapter. The xml file contains 2 Textview. The reason i use Hashmap is because I need to use the "key" as reference when clicking on them and pass it to another Activity later on.

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    String[] animal_id = new String[] {
            "1",
            "2",
            "3"

    };
    String[] animal_names = new String[] {
            "mouse",
            "dragon",
            "tiger"
    };

    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

    for(int i=0;i<3;i++){
        HashMap<String, String> hm = new HashMap<String,String>();
        hm.put("ID",animal_id[i]);
        hm.put("Name",animal_names[i]);
        aList.add(hm);
    }

    String[] from = { "ID","Name"};

    int[] to = { R.id.id, R.id.name};

    SimpleAdapter adapter = new SimpleAdapter(this, aList, android.R.layout.simple_list_item_1, from, to);

    ListView listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);



}

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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.volley.Main2Activity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="id"
        android:id="@+id/id"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="name"
        android:id="@+id/name"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView" />
</RelativeLayout>
gosulove
  • 1,645
  • 2
  • 26
  • 40
  • 1
    You've got an extra argument in the `SimpleAdapter` constructor call. Drop the `getBaseContext()`. – Mike M. Sep 10 '16 at 04:40
  • As @MikeM. says, you've an extra argument in constructor, to fix it, just replace `this, getbaseContext()` with `getApplicationContext()` only. – Apurva Sep 10 '16 at 04:42
  • @MikeM. thx. But this line "setListAdapter(adapter);" still red colour. Any idea ? Looks like I shortage of some coding in order to display it in listview? – gosulove Sep 10 '16 at 04:43
  • @Apurva but "setListAdapter(adapter);" still red colour. Any idea? – gosulove Sep 10 '16 at 04:44
  • 2
    You're in an `AppCompatActivity`, not a `ListActivity`. There is no `setListAdapter()` method. You need to call `setAdapter()` directly on your `ListView`. – Mike M. Sep 10 '16 at 04:44
  • @MikeM. it still doesn't work. I have uploaded the xml file as well. Can help me see what else wrong? . When the app runs, the list view is empty. No data displayed and no errors prompt as well. – gosulove Sep 10 '16 at 05:01
  • 1
    First, you generally don't want `wrap_content` for a `ListView`. I would guess that you don't really need those ``s in your layout, so you can probably delete them, and make your `ListView` `match_parent` both ways. Then, you probably want to use `android.R.layout.simple_list_item_2` for the `Adapter` instead, since you seem to want two things in each item, and your `to` array would be `{android.R.id.text1, android.R.id.text2}`. – Mike M. Sep 10 '16 at 05:08
  • @MikeM. getting confuse, any example in coding? – gosulove Sep 10 '16 at 05:22
  • Yeah, sure. Gimme a little bit. I'm on a phone. – Mike M. Sep 10 '16 at 05:24

1 Answers1

1

The TextViews used in your SimpleAdapter are going to be created as needed when the given layout is inflated for the rows, so you don't need the id and name <TextView>s in your layout. We'll also change the <ListView>'s width and height to match_parent.

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.volley.Main2Activity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView" />

</RelativeLayout>

Then, the layout you're giving the SimpleAdapter - simple_list_item_1 - is only a single TextView, but you want to display two Strings for each item, so we'll use simple_list_item_2 instead. The TextViews in that layout have system resource IDs text1 and text2, so we need to change your to array, as well.

String[] from = { "ID", "Name" };
int[] to = { android.R.id.text1, android.R.id.text2 };

SimpleAdapter adapter = new SimpleAdapter(this, aList, android.R.layout.simple_list_item_2, from, to);

ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);

Following suggestions in the comments, you already dropped the extraneous argument in the constructor call, and you're now calling setAdapter() directly on your ListView, so everything else should be good to go.


The simple_list_item_2 does stack its TextViews vertically, so if you want both on a single, horizontal line, you'll need to use your own layout. For a simple, generic example, we'll use a LinearLayout and two TextViews with weighted widths, to keep the columns aligned.

In your res/layout/ folder, list_item.xml:

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

    <TextView
        android:id="@+id/text_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/text_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4" />

</LinearLayout>

And the few changes in the code:

int[] to = { R.id.text_id, R.id.text_name };

SimpleAdapter adapter = new SimpleAdapter(this, aList, R.layout.list_item, from, to);
Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • Great! Your coding shows 2 rows within a row. For my example, actually I want to display it in two columns within a row. So that means each row, left side shows ID, right side shows Name. If were to do that, is it has to create another xml template for it? OR do you need me to post another question so that you can answer from there? – gosulove Sep 10 '16 at 05:56
  • 1
    Yeah, in that case, you'd have to create your own XML layout to give the `Adapter`, instead of the `android.R.layout` one. The ones provided by the system are pretty generic, and limited in their variety. A simple `LinearLayout` with two `TextView`s would do what you want. You'll need to make sure to change the `to` array again, to use your `TextView`s `R.id`s, instead of the `android.R.id`s. I can add some simple examples to this answer. Gimme a minute. – Mike M. Sep 10 '16 at 06:02
  • 1
    thx alot. I going to work with onclick item and hope it goes well if not i will post it on SOF within 30 min. Feel free to take a look if you are still around :) – gosulove Sep 10 '16 at 06:26
  • Hey Mike, I posted something complicated and see if you have time to have a look? The question is here -> http://stackoverflow.com/questions/39424112/android-convert-jsonobject-to-hashmap-and-display-in-listview-with-simple-adapte – gosulove Sep 10 '16 at 08:22