Welcome to ANDROID Developer!

Friday, May 23, 2014

ListView with multiple choice


By on 7:13 PM

Hi Guys!!
Today i am sharing the code of the multiple choice option select in list view.
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. 
For more details about the List View visit the Android Developer site List View.

Lets Start the coding now.

activty_main.xml

?
1
2
3
4
5
6
7
8
9
<linearlayout android:id="@+id/LinearLayout1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" 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=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
 
    <textview android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/hello_world">
 
    <button android:id="@+id/getchoice" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Get Choice">
 
    <listview android:id="@+id/list" android:layout_height="wrap_content" android:layout_width="fill_parent">
 
</listview></button></textview></linearlayout>

MainActivity.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.sunil.listview;
 
 import android.app.Activity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
 import android.widget.Toast;
  
public class MainActivity extends Activity {
  
    ListView myList;
    Button getChoice;
  
   String[] listContent = {
  
            "January",
            "February",
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December"
  
    };
  
    /** Called when the activity is first created. */
  
    @Override
  
    public void onCreate(Bundle savedInstanceState) {
  
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myList = (ListView)findViewById(R.id.list);
        getChoice = (Button)findViewById(R.id.getchoice);
  
        ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, android.R.layout.simple_list_item_multiple_choice, listContent);
        myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  
        myList.setAdapter(adapter);
        getChoice.setOnClickListener(new Button.OnClickListener(){
  
 
            @Override
            public void onClick(View v) {
              
                String selected = "";
                int cntChoice = myList.getCount();
  
                SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();
                for(int i = 0; i < cntChoice; i++){
                    if(sparseBooleanArray.get(i)) {
                        selected += myList.getItemAtPosition(i).toString() + "\n";
  
                    }
  
                }
  
                Toast.makeText(MainActivity.this, selected, Toast.LENGTH_LONG).show();
  
            }});
  
    }
  
}
</string></string>

Please download the source code ListView with multiple choice

About Theavuth NHEL

Faizan is a 17 year old young guy who is blessed with the art of Blogging,He love to Blog day in and day out,He is a Website Designer and a Certified Graphics Designer.

0 comments:

Post a Comment