mercredi 21 janvier 2015

how to display only checked values in android

Hi in the below code I am displaying listview with checkboxes.In that list it includes friends of the users with checkbox when i want only checked data to pass the parameters as friend array.


GroupList.java



public class GroupList extends ListActivity
{

boolean checkBoxState[];
private IAppManager imService = null;

private FriendListAdapter friendAdapter;

public String ownusername = new String();

private class FriendListAdapter extends BaseAdapter
{
@SuppressWarnings("unused")

class ViewHolder {
TextView text;
ImageView icon;
CheckBox check1;



}
private LayoutInflater mInflater;
private Bitmap mOnlineIcon;
private Bitmap mOfflineIcon;

private FriendInfo[] friends = null;




public FriendListAdapter(Context context) {
super();

mInflater = LayoutInflater.from(context);

mOnlineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenstar);
mOfflineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.redstar);

}

public void setFriendList(FriendInfo[] friends)
{
this.friends = friends;

}


public int getCount() {

return friends.length;
}


public FriendInfo getItem(int position) {

return friends[position];
}

public long getItemId(int position) {

return 0;
}

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

final ViewHolder holder;


if (convertView == null)
{
convertView = mInflater.inflate(R.layout.grouplist, null);


holder = new ViewHolder();

holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.check1 = (CheckBox) convertView.findViewById(R.id.checkBox1);

convertView.setTag(holder);

}

else {

holder = (ViewHolder) convertView.getTag();
}


holder.text.setText(friends[position].userName);
holder.icon.setImageBitmap(friends[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);
checkBoxState = new boolean[friends.length];
holder.check1.setChecked(checkBoxState[position]);
holder.check1.setOnCheckedChangeListener(new OnCheckedChangeListener(){

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkBoxState[position]=isChecked;



Toast.makeText(getApplicationContext(),friends[position].userName+"checked", Toast.LENGTH_LONG).show();
}
});


return convertView;
}
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
setContentView(R.layout.group_list_screen);
friendAdapter = new FriendListAdapter(this);
Button create=(Button)findViewById(R.id.create);
create.setOnClickListener(new OnClickListener() {

@SuppressWarnings("unused")
@Override
public void onClick(View v) {
String groupname = getIntent().getStringExtra("nick");


try {

FriendInfo[] friend=FriendController.getFriendsInfo();

System.out.print(friend.length);


String result1 = imService.CreateGroup(groupname,imService.getUsername(),friend);
} catch (UnsupportedEncodingException e) {

e.printStackTrace();
}


Toast.makeText(getApplicationContext(), "Group Created Sucessfully",Toast.LENGTH_LONG).show();

}
});




Aucun commentaire:

Enregistrer un commentaire