Innaz Review, Samsung Galaxy, price and specifications, Flash Android, Games For Your Apple, Jailbreak Tool For iOS, IMEI Unlock Method

Sabtu, 29 September 2012

Android Multiple Select ListView

Android Multiple Select ListView - In the past, when I started to start blogging, many thoughts disturbed me. I want to have a blog with a nice and interesting look. I am constantly looking for basic tutorials from some web and blogs on the internet. And thankfully, one by one I started to do it, and of course have to go through some confusion process first, but the most important of a blog that is content, yes on the blog Innaz Review we will discuss a lot of information about gadgets that are very in need by you, now we will discuss first about Android Multiple Select ListView please refer to the information we will convey until completion:

Articles : Android Multiple Select ListView
full Link : Android Multiple Select ListView

You can also see our article on:


Android Multiple Select ListView



Android Multiple Choice ListView Using Custom Adapter.This listview you can use when you send sms to more than one friend or any notification to your friends.


So using Custom Adapeter we have to create row.xml layout which has checkbox.

row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@android:color/white" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_margin="5dp"
        android:layout_toLeftOf="@+id/checkbox"
        android:text="title"
        android:textColor="@android:color/black"
        android:textSize="20dp"
        android:textStyle="bold" >
    </TextView>

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dip"
        android:background="@drawable/chk"
        android:button="@null"
        android:focusable="false"
        android:focusableInTouchMode="false" >
    </CheckBox>
</RelativeLayout>

 Now Using Adapter we can bind data to ListView.But we need to learn easy way to bind data with custom ListView and also hadle CheckBox Listener For Each Row.

So here i have RSS Feed to get data from web and then i add it in List<NameBean>.NameBean is bean class which contains value of Rss Feed Element and we have to maintain one boolean for checkbox state either checked or not.And generate Getters And Setters.

NameBean.java


public class NameBean {

private String name;
private boolean selected;

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;
}

}

Now when you get data through XML Parsing you have to create Object of NameBean and you can set data and add Object to List.


for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);

                  if (nNode.getNodeType() == Node.ELEMENT_NODE) {

   Element eElement = (Element) nNode;

    NameBean objItem = new NameBean();

   objItem.setName(getTagValue("name", eElement));
                 
   list.add(objItem);

}
}



Now list  is fill up with NameBean Objects.And you can retrive data in Adapter so when you set ListView you just retrieve data in getView(...) method of Adapter.

In getView(...) method put checkBox listener to listen checkbox is check or not, if its checked then set boolean true for that object of NameBean And if its not checked then setSelected false to Namebean object.

So when you start this example in listview and its get data from webservice and bind data to listview using NameAdapter Class.



NameAdapter.java


import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class NamesAdapter extends ArrayAdapter<NameBean> {

private List<NameBean> list;
private LayoutInflater inflator;

public NamesAdapter(Activity context, List<NameBean> list) {
super(context, R.layout.row, list);
this.list = list;
inflator = context.getLayoutInflater();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;
if (convertView == null) {
convertView = inflator.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.chk = (CheckBox) convertView.findViewById(R.id.checkbox);
holder.chk
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton view,
boolean isChecked) {
int getPosition = (Integer) view.getTag();
list.get(getPosition).setSelected(view.isChecked());

}
});
convertView.setTag(holder);
convertView.setTag(R.id.title, holder.title);
convertView.setTag(R.id.checkbox, holder.chk);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.chk.setTag(position);

holder.title.setText(list.get(position).getName());
holder.chk.setChecked(list.get(position).isSelected());

return convertView;
}

static class ViewHolder {
protected TextView title;
protected CheckBox chk;
}
}


When your listView is bind with data you can see checkbox in row with name and now you can get Selected name using NameBean Object and you can retrieve it from list.


                // Retrive Data from list Using for-each loop
                StringBuffer sb = new StringBuffer();
for (NameBean bean : items) {

if (bean.isSelected()) {
sb.append(bean.getName());
sb.append(",");
}
}



Now you get All Selected name from ListView.So its so easy to write Multiple Choice ListView in Android.


You Can Download Full Source Code



so much information Android Multiple Select ListView

hopefully Android Multiple Select ListView information can provide benefits for you in determining the gadgets that fit your needs in daily life.

you just read Android Multiple Select ListView if you feel this information is useful and want to bookmark or share it please use link https://innaz2.blogspot.com/2012/09/android-multiple-select-listview.html if you want more information please search on other pages this blog.

Tag :
Share on Facebook
Share on Twitter
Share on Google+
Tags :

Related : Android Multiple Select ListView

0 komentar:

Posting Komentar