Tuesday, January 8, 2013

ListView with Checkbox and Click

Android ListView items with CheckBox and Click Action. I searched a lot and found this example for duel action of ListView Item.

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="@string/some_text"
        android:textSize="20sp" />

    <Button

        android:id="@+id/findSelected"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_String" />

    <ListView

        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>


listitem.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="6dip" >

    <CheckBox

        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:focusable="false"
        android:focusableInTouchMode="false" />

    <TextView

        android:id="@+id/code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/checkBox1"
        android:layout_alignBottom="@+id/checkBox1"
        android:layout_toRightOf="@+id/checkBox1" >
    </TextView>

</RelativeLayout>


String.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">CheckBoxList</string>

    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="some_text">Some Mobile OS!</string>
    <string name="btn_String">Find OS that are Selected</string>

</resources>



MainActivity.java


public class MainActivity extends Activity 
{
MyCustomAdapter dataAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Generate list View from ArrayList
displayListView();
checkButtonClick();
}

private void displayListView() 
{
//Array list of countries
ArrayList<Mobile> MobileOSList = new ArrayList<Mobile>();
Mobile MobileOS = new Mobile("AND","Android",false);
MobileOSList.add(MobileOS);
MobileOS = new Mobile("IPH","Iphone",true);
MobileOSList.add(MobileOS);
MobileOS = new Mobile("BLB","Blackberry",false);
MobileOSList.add(MobileOS);
MobileOS = new Mobile("WIN","Windows",true);
MobileOSList.add(MobileOS);
MobileOS = new Mobile("SYM","Symbian",true);
MobileOSList.add(MobileOS);
MobileOS = new Mobile("BADA","Bada",false);
MobileOSList.add(MobileOS);
MobileOS = new Mobile("OTH","Others",false);
MobileOSList.add(MobileOS);

//create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this,R.layout.listitem, MobileOSList);
ListView listView = (ListView) findViewById(R.id.listView1);

// Assign adapter to ListView
listView.setAdapter(dataAdapter);

listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
// When clicked, show a toast with the TextView text
Mobile country = (Mobile) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),"Clicked on Row: " + country.getName(),Toast.LENGTH_LONG).show();
}
});
}

private class MyCustomAdapter extends ArrayAdapter<Mobile> 
{
private ArrayList<Mobile> MobileOSList;
public MyCustomAdapter(Context context, int textViewResourceId,ArrayList<Mobile> countryList) 
{
super(context, textViewResourceId, countryList);
this.MobileOSList = new ArrayList<Mobile>();
this.MobileOSList.addAll(countryList);
}

private class ViewHolder 
{
TextView code;
CheckBox name;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
ViewHolder holder = null;

if (convertView == null) 
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.listitem, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.code);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.name.setOnClickListener( new View.OnClickListener() 
{
public void onClick(View v) 
{
CheckBox cb = (CheckBox) v ;
Mobile country = (Mobile) cb.getTag();
Toast.makeText(getApplicationContext(),"Clicked on Checkbox: " + cb.getText() +" is " + cb.isChecked(),Toast.LENGTH_LONG).show();
country.setSelected(cb.isChecked());
}
});
}
else 
{
holder = (ViewHolder) convertView.getTag();
}
Mobile MobileOS = MobileOSList.get(position);
holder.code.setText(" (" +  MobileOS.getCode() + ")");
holder.name.setText(MobileOS.getName());
holder.name.setChecked(MobileOS.isSelected());
holder.name.setTag(MobileOS);
return convertView;
}
}

private void checkButtonClick() 
{
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new OnClickListener() 
{
@Override
public void onClick(View v) 
{
StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected...\n");
ArrayList<Mobile> MobileOSList = dataAdapter.MobileOSList;
for(int i=0;i<MobileOSList.size();i++)
{
Mobile MobileOS = MobileOSList.get(i);
if(MobileOS.isSelected())
{
responseText.append("\n" + MobileOS.getName());
}
}
Toast.makeText(getApplicationContext(),responseText, Toast.LENGTH_LONG).show();
}
});
}
}

Mobile.java


public class Mobile 
{
String code = null;
String name = null;
boolean selected = false;

public Mobile(String code, String name, boolean selected)
{
super();
this.code = code;
this.name = name;
this.selected = selected;
}

public String getCode() 
{
return code;
}

public void setCode(String code) 
{
this.code = code;
}

public String getName() 
{
return name;
}

public void setName(String name) 
{
this.name = name;
}

public boolean isSelected() 
{
return selected;
}

public void setSelected(boolean selected) 
{
this.selected = selected;
}
}