Android : managing checkbox visibility on listview onItemClick

on Friday, October 10, 2014


I have listview and list_item layout contains a imageview, textview and checkbox. my adapater contains sectionheader. When I click on listview item make that checkbox visble and when I again click on that item make checkbox invisible.



ListView lv = (ListView) findViewById(R.id.friendsList);
lv.setFastScrollEnabled(true);
fbAdapter = new FbFriendsAdapter(this, 0);
lv.setAdapter(fbAdapter);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub

fbAdapter.getSelectedFriend();

String s = (String) ((TextView) view
.findViewById(R.id.friendName)).getText();
CheckBox cb = (CheckBox) view.findViewById(R.id.iv_check);
cb.setVisibility(View.VISIBLE);

cb.setChecked(true);

if (cb.isChecked()) {

Toast.makeText(FbFriendsListActivity.this, "selected " + s,
Toast.LENGTH_SHORT).show();
}
else{
//not working when i tried . setvisibilty gone of checkbox.

}
});


listitemlayout:



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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp" >

<com.hashmash.utils.RoundedImageView
android:id="@+id/friendPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:src="@drawable/fb_profileimgsm"
android:scaleType="centerCrop"
app:corner_radius="80dp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:orientation="horizontal" >

<TextView
android:id="@+id/friendName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:text="#Name"
android:textColor="@color/instablue"
android:textSize="14sp" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right" >

<CheckBox
android:id="@+id/iv_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:visibility="visible" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>

</LinearLayout>

0 comments:

Post a Comment