Example of ListView and GridView

ListView

ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.

Layout

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">

    <ListView android:id="@+id/lvColors"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_width="match_parent" />

    <Button android:id="@+id/btnSubmit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="Submit"/>
</LinearLayout>

Code

public class MainActivity extends Activity {
    private ListView lvColors;
    private ArrayAdapter adColors;

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

        // init data
        ArrayList alColors = new ArrayList<>();
        String[] colors = new String[] {"Balck", "White", "Grey"};
        alColors.addAll(Arrays.asList(colors));
        adColors = new ArrayAdapter(this, android.R.layout.simple_list_item_checked, alColors);

        // init ListView
        lvColors = (ListView)findViewById(R.id.lvColors);
        lvColors.setAdapter(adColors);
        lvColors.setChoiceMode(lvColors.CHOICE_MODE_MULTIPLE);
        lvColors.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View v, int pos, long id) {
                String item = (String) lvColors.getItemAtPosition(pos);
                Toast.makeText(getApplicationContext(), item, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void doClick(View v) {
        int amount = lvColors.getCount();
        SparseBooleanArray items = lvColors.getCheckedItemPositions();
        for (int i = 0; i < amount; i++) {
            if (items.get(i)) {
                String item = (String)lvColors.getItemAtPosition(i);
                Toast.makeText(getApplicationContext(), item, Toast.LENGTH_SHORT).show();
            }
        }
    }
}    

Also you can create anonymous BaseAdapter as shown following, to display the contents of the colors array in the ListView.

lvColors.setAdapter(new BaseAdapter() {
    public int getCount() {
        return colors.length;
    }
    public Object getItem(int position) {
        return colors[position];
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.list_row, null);
        TextView tvColor = (TextView) view.findViewById(R.id.tvColor);
        tvColor.setText(colors[position]);
        return view;
    }
});

GridView

GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. The grid items are automatically inserted to the layout using a ListAdapter.

Layout

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">

    <GridView android:id="@+id/gvColors"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:padding="10dp"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:columnWidth="100dp"
        android:stretchMode="columnWidth"
        android:gravity="center" />
</LinearLayout>

Code

public class MainActivity extends Activity {
    private GridView gvColors;
    private ArrayAdapter adColors;

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

        // init data
        ArrayList alColors = new ArrayList<>();
        String[] colors = new String[] {"Balck", "White", "Grey"};
        alColors.addAll(Arrays.asList(colors));
        adColors = new ArrayAdapter(this, android.R.layout.simple_list_item_1, alColors);

        // init ListView
        gvColors = (GridView)findViewById(R.id.gvColors);
        gvColors.setAdapter(adColors);
        gvColors.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View v, int pos, long id) {
                String item = (String) gvColors.getItemAtPosition(pos);
                Toast.makeText(getApplicationContext(), item, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
comments powered by Disqus