Saturday, August 11, 2018

Static ListView with SimpleAdapter

package net.samaysoftware.listviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.*;

import java.util.ArrayList;
import java.util.HashMap;

public class AnotherListViewActivity extends AppCompatActivity {

    ListView lvAnother;
    String[] from = {"a","b"};
    int[] to = {R.id.tvState, R.id.tvCapital};
    ArrayList<HashMap<String, String>> data = new ArrayList<>();

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another_list_view);
        lvAnother = (ListView) findViewById(R.id.lvanother);

        prepareData();
        SimpleAdapter sa = new SimpleAdapter(this, data, R.layout.repeating_unit_2_items,from, to);
        lvAnother.setAdapter(sa);


    }

    private void prepareData() {
        HashMap<String, String> map1 = new HashMap<>();
        map1.put("a", "Gujarat");
        map1.put("b", "Gandhinagar");

        HashMap<String, String> map2 = new HashMap<>();
        map2.put("a", "Rajasthan");
        map2.put("b", "Jaipur");

        HashMap<String, String> map3 = new HashMap<>();
        map3.put("a", "Maharasthra");
        map3.put("b", "Mumbai");

        HashMap<String, String> map4 = new HashMap<>();
        map4.put("a", "Madhyapradesh");
        map4.put("b", "Bhopal");

        data.add(map1);
        data.add(map2);
        data.add(map3);
        data.add(map4);


    }
}

No comments:

Post a Comment

Near by App

https://drive.google.com/file/d/0B2ag35s4X53Eb2pSQVI1SzNudE0/view