List view with checkedTextView not working as intended - android

I have this xml code for a list view:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/searchView"
android:background="#drawable/list_selector"
android:choiceMode="singleChoice" />
This is the list_selector drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_shortAnimTime">
<item android:drawable="#drawable/button_pressed" android:state_activated="true"/>
</selector>
And the list_view has a checkedTextView as each item as follows:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/checkList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center_vertical"
android:orientation="vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white"
android:paddingLeft="6dip" />
There are two problems with this setup.
1.) I can check the box next to each item but I cant uncheck it.
2.) Before I implemented the checkbox, the list selector makes the item go bluish when you select it, now when you select the item, the list_selector doesnt do anything, only the checkbox becomes ticked.
So is there anyway to be able to solve 1. and make it so as, I have to click just the checkbox to tick/untick it and when I click anywhere else within the listview item, list_selector comes into play?
EDIT: My custom adapter: Also, I have solved the marking/unmarking. It Works. The only problem now is 2.) as above!
public class ContactsAdapter extends BaseAdapter {
private List<Contact> mData;
private LayoutInflater mInflater;
private Context context;
public ContactsAdapter(List<Contact> mData, Context context) {
this.mData = mData;
this.context = context;
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
switch (context.getSharedPreferences("settings",
HomeScreenActivity.MODE_PRIVATE).getInt("displayChoice", -1)) {
case 1:
return mData.get(position).getLastName() + " "
+ mData.get(position).getFirstName();
case 2:
return mData.get(position).getFirstName() + " "
+ mData.get(position).getMobileNumber();
case 3:
return mData.get(position).getLastName() + " "
+ mData.get(position).getMobileNumber();
default:
return mData.get(position).getFirstName() + " "
+ mData.get(position).getLastName();
}
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
CheckedTextView result;
if (convertView == null) {
result = (CheckedTextView) mInflater.inflate(
R.layout.home_screen_contacts_view, parent, false);
} else {
result = (CheckedTextView) convertView;
}
final String contactRow = getItem(position);
result.setText(contactRow);
result.setBackgroundResource(R.drawable.list_selector);
return result;
}
}

Related

TextView in ListView showing up blank

I am using a pattern that I have used all over my application to populate a ListView, but this time the TextView in each cell of the ListView are showing up blank. There are the correct number of cells (I can see by the dividers), but there is no text. Please see my layout XML and the code of my adapter:
The List Item XML, edit_list_item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:focusable="true"
android:id="#+id/list_item_textview"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:id="#+id/list_item_checkbox"
android:focusable="false"
android:paddingRight="#dimen/activity_horizontal_margin"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
My Adapter class:
public class EditGroupObjectListAdapter extends BaseAdapter {
public static String LOG_TAG = EditGroupObjectListAdapter.class.getSimpleName();
private LayoutInflater mInflater;
private Context context;
private ArrayList<EditBuddy> allEditBuddies;
private List<String> buddiesInGroup;
public EditGrouphObjectListAdapter(Context context, List<String> buddiesInGroup, List<Buddy> allBuddies) {
this.context = context;
mInflater = LayoutInflater.from(context);
this.allEditBuddies = new ArrayList<>();
this.buddiesInGroup = buddiesInGroup;
for (Buddy buddy : allBuddies) {
EditBuddy editBuddy = new EditBuddy(buddy);
if (this.buddiesInGroup.contains(editBuddy.get_buddy_username())) {
editBuddy.setIsInBunch(true);
editBuddy.setIsSelected(true);
}
allEditBuddies.add(editBuddy);
}
}
public ArrayList<EditBuddy> getAllBuddies() {
return this.allEditBuddies;
}
#Override
public int getCount() {
return allEditBuddies.size();
}
#Override
public Object getItem(int position) {
return allEditBuddies.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (mInflater == null)
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = mInflater.inflate(R.layout.edit_list_item, null);
// Another way of instantiating convertView
// LayoutInflater inflater = (LayoutInflater) this.context
// .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// convertView = inflater.inflate(R.layout.edit_bunch_list_item, parent, false);
EditBuddy editBuddy = (EditBuddy)getItem(position);
Log.d(LOG_TAG, "Name: " + editBuddy.get_buddy_full_name());
Log.d(LOG_TAG, "UserName: " + editBuddy.get_buddy_username());
TextView buddyFullNameTextview = (TextView)convertView.findViewById(R.id.list_item_textview);
buddyFullNameTextview.setText(editBuddy.get_buddy_full_name());
// Checkbox code omitted
return convertView;
}
}
The print statements before setting the text of the TextView are indeed printing the correct data. I have two buddies in the list, and I can see the spaces for them but no actual text.
Any help would be greatly appreciated.
Well, in this instance it seems to have been the text color was the same as the background, but there's no reason for that to have been the case. The Activity inherits the AppTheme, which has a TextColor set to black. The font of the ListView is also smaller than other ListViews in the application - so I'll have to see where the disconnect is, and then update the answer.

Problems with the method setChoiceMode () in my ListView

Please note, this is really weird.
For some reason, the method setChoiceMode (ListView.CHOICE_MODE_SINGLE) no results.
I use it like this:
list = (ListView) findViewById(R.id.list);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setOnItemClickListener(OnItemClickListenerObject);
list.setAdapter(adapter);
The fact is that after I put the method setChoiceMode (), nothing has changed, RadioButtons not appeared.
I'm using a custom adapter and I have no problems with it? But I do not understand why Radiobuttons not shown.
Any ideas? (If you need additional code, ask and I'll post it.)
My adapter code:
public class ContactAdapter extends BaseAdapter {
private LayoutInflater inflater;
private ArrayList<Contact> contacts;
private View view;
public ContactAdapter(Context context, ArrayList<Contact> contacts) {
this.contacts = contacts;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return contacts.size();
}
#Override
public Object getItem(int position) {
return contacts.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private Contact getContact(int position) {
return (Contact) getItem(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.contact_item, parent, false);
}
Contact c = getContact(position);
((TextView) view.findViewById(R.id.lv_name)).setText(c.getName() + " " + c.getSurname());
((ImageView) view.findViewById(R.id.lv_img)).setImageBitmap(c.getPhoto());
return view;
}
}
Below shows the layout that I use for ListView item.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp">
<ImageView
android:id="#+id/lv_img"
android:layout_width="75dp"
android:layout_height="75dp"
android:src="#drawable/default_user"
android:layout_weight="0"/>
<TextView
android:id="#+id/lv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="16dp"
android:layout_gravity="center_vertical"
android:textSize="24sp"/>
</LinearLayout>
Setting the choice mode does not automatically make radio buttons appear. All it does is add behavior which toggles a ListView row's Checkable or activation state. It's still up to the row's View to decide how to react to that. You'll need to create your own layout to inflate for a row that supports the activation state or the Checkable interface.
Android does provide some simple pre-made views that you can use. Here's just a couple you can choose from:
android.R.layout.simple_list_item_activated_1.xml
android.R.layout.simple_list_item_checked.xml

Navigation drawer selector disable not working

I need to disable the second element in my navigation drawer until a certain task is finished. Here is how I disable the second element:
nav_item_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/dark_green"
android:state_pressed="true" />
<item android:drawable="#android:color/transparent"
android:state_enabled="true" />
<item android:drawable="#android:color/darker_gray"
android:state_enabled="false" />
</selector>
nav_fragment.xml (wrapped in a DrawerLayout)
<ListView
android:id="#+id/left_drawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:cacheColorHint="#0000"
android:background="#drawable/gradient_radial_backround"
android:choiceMode="singleChoice"
android:divider="#color/green_light"
android:dividerHeight="1dp" />
nav_list_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nav_list_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/nav_item_selector"
android:orientation="horizontal"
android:padding="#dimen/standard_padding">
<ImageView
android:id="#+id/nav_list_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#null"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/nav_list_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="sans-serif-light"
android:gravity="center_vertical"
android:minHeight="#dimen/listPreferredItemHeightSmall"
android:paddingLeft="#dimen/activity_horizontal_small_margin"
android:text="List Item"
android:textSize="#dimen/text_nav_list_entry"
android:textColor="#color/dark_green" />
</LinearLayout>
Activity:
public class NavItemAdapter extends ArrayAdapter<String> {
LayoutInflater inflater;
private String[] listItems;
public NavItemAdapter(Context context, int textViewResourceId, String[] listItems) {
super(context, textViewResourceId, listItems);
this.listItems = listItems;
inflater = LayoutInflater.from(context);
}
#Override
public boolean isEnabled(int position)
{
if(position == 1)
return false;
else
return true;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = new NavItemView(getContext());
NavItemView navItemView = (NavItemView) convertView;
switch (position) {
case 0:
navItemView.setUpItem(R.drawable.ic_nav_1_checked, listItems[position], R.drawable.ic_nav_1);
break;
case 1:
navItemView.setUpItem(R.drawable.ic_nav_2_checked, listItems[position], R.drawable.ic_nav_2);
break;
case 2:
navItemView.setUpItem(R.drawable.ic_nav_3_checked, listItems[position], R.drawable.ic_nav_3);
break;
case 3:
navItemView.setUpItem(R.drawable.ic_nav_4_checked, listItems[position], R.drawable.ic_nav_4);
break;
case 4:
navItemView.setUpItem(R.drawable.ic_nav_5_checked, listItems[position], R.drawable.ic_nav_5);
break;
case 5:
navItemView.setUpItem(R.drawable.ic_nav_6_checked, listItems[position], R.drawable.ic_nav_6);
break;
}
return convertView;
}
}
class NavItemView extends LinearLayout implements Checkable {
private View v;
private ImageView iconView;
private TextView textView;
private Boolean checked = false;
private int mImageChecked;
private int mImage;
public NavItemView(Context context) {
super(context);
LayoutInflater inflater = LayoutInflater.from(context);
v = inflater.inflate(R.layout.list_item_navdrawer, this, true);
assert v != null;
iconView = (ImageView) v.findViewById(R.id.nav_list_icon);
textView = (TextView) v.findViewById(R.id.nav_list_text);
}
#Override
public boolean isChecked() {
return checked;
}
#Override
public void setChecked(boolean checked) {
this.checked = checked;
if (isChecked()) {
setBackgroundColor(getResources().getColor(R.color.transparent));
textView.setTextColor(getResources().getColor(R.color.light_pink));
iconView.setImageResource(mImageChecked);
} else {
//Reset to default
setBackgroundColor(getResources().getColor(R.color.transparent));
textView.setTextColor(getResources().getColor(R.color.green_lighter));
iconView.setImageResource(mImage);
}
}
#Override
public void toggle() {
checked = !checked;
}
private void setUpItem(int imageChecked, String title, int image) {
mImageChecked = imageChecked;
mImage = image;
textView.setText(title);
iconView.setImageResource(image);
}
}
What happens is the item is disabled, but the selector does not kick in (it stays the same colour). Not only that but that item's divider disappears which is even worse! What is happening here?
I actually can't believe this but the only solution seems to be a bit of a weird hack. There are several threads on this question but the only solid answer I've found is from user Jason Lin located here.
It seems there were two pieces of the puzzle missing, the first is that returning false for the item I want to disable in isEnabled() merely makes the item unclickable and unfocusable. To actually disable it I need to perform the same check in getView() and call setEnabled(false) on the item...
if(position == 1)
convertView.setEnabled(false);
AND
Set android:duplicateParentState="true" on the linear layout of the row.
This was the most important part as obviously I tried to called setEnabled(false) in getView() first of all, but it took me all day to learn I need the combination of all 3 of these things - returning false in isEnabled(), setting enabled to false in getView(), and setting duplicateParentState to true :)

Adding Header in ListView Android

Currently my xml code for listview is as below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/listView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
Now after each 5th item, I want to add a static header. So it is fixed. The xml code for the header is as below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/list_header_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="2dip"
android:paddingBottom="2dip"
android:background="#008000"
android:textColor="#android:color/white"
android:paddingLeft="5dip"
style="?android:attr/listSeparatorTextViewStyle" />
</LinearLayout>
Now how do I merge both? There are online articles on adding headers to list and section list view etc to do it programmatically. But mine is not so complicated.
It is fixed, after every fifth item in the list view, add a header. Any idea how I could merge the both set of codes above?
Make a new project with 2 activities
In activity_main1.xml add this code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="#+id/text"
android:layout_height="50dp"
android:gravity="center_vertical"
android:text="text"
android:visibility="visible"
android:layout_width="fill_parent"
android:textColor="#FF000000"
android:background="#FFFFFFFF" />
</LinearLayout>
In activity_main2.xml add this code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="#+id/textSeparator"
android:layout_height="wrap_content"
android:gravity="center"
android:text="text"
android:visibility="visible"
android:layout_width="fill_parent"
android:textColor="#FFFFFFFF"
android:background="#000" />
</LinearLayout>
In your MainActivity.java file add this
public class MainActivity extends ListActivity implements OnTouchListener{
private MyCustomAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new MyCustomAdapter();
for (int i = 1; i < 50; i++) {
mAdapter.addItem("ListView item " + i);
if (i % 5 == 0) {
mAdapter.addSeparatorItem("Header " + i);
}
}
setListAdapter(mAdapter);
}
//Adapter Class
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final String item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public int getCount() {
return mData.size();
}
public String getItem(int position) {
return mData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.activity_main1, null);
holder.textView = (TextView)convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.activity_main2, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}
Do Ctrl+Shift+O then run it
To create a list with section or header refer this [http://danielme.com/tip-android-27-listview-con-secciones-o-cabeceras/][1]
[1]: http://danielme.com/tip-android-27-listview-con-secciones-o-cabeceras/ .

listview with imagelist not work proper for multiple selection in custome adapter

Hi Please any one Help me out of this..
i am using listview adapter for displaying image list with check box, that allows to select multiple image form list.. code works good for images list not more than screen but if image is more than screen (for scrolling) it throws null pointer exception.. following is my code.
(apterMultiChoice.java)
public class PhotoAdapterMultiChoice extends BaseAdapter
{
public ViewHolder holder;
private Activity activity;
private ArrayList<String> data;
private ArrayList<String> text;
private ArrayList<CheckBox> chkBox;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public PhotoAdapterMultiChoice(Activity a, ArrayList<String> title,ArrayList<String> URL,ArrayList<CheckBox> chkBoxPass)
{
activity = (Activity) a;
data=URL;
text=title;
this.chkBox = chkBoxPass;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount()
{
return data.size();
}
public boolean getSelect(int position)
{
return this.chkBox.get(position).isChecked();
}
public int getCountSelect()
{
return chkBox.size();
}
public Object getItem(int position)
{
return this.chkBox.get(position).isChecked();
}
public long getItemId(int position)
{
return position;
}
public static class ViewHolder
{
public TextView text;
public ImageView image;
public CheckBox checkBox;
}
public View getView(int position, View convertView, ViewGroup parent)
{
System.out.println(this.chkBox.size() +" ---- "+data.size());
if(this.chkBox.size() < data.size())
{
View vi=convertView;
if(convertView==null)
{
System.out.println("convertView is null");
vi = inflater.inflate(R.layout.photoitemlistmultichoice, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.text);
holder.image=(ImageView)vi.findViewById(R.id.image);
holder.checkBox=(CheckBox) vi.findViewById(R.id.check);
//this.chkBox.add((CheckBox) vi.findViewById(R.id.check));
vi.setTag(holder);
}
else
{
System.out.println("convertView is Not null");
this.chkBox.add((CheckBox) vi.findViewById(R.id.check));
holder=(ViewHolder)vi.getTag();
}
System.out.println("Position : "+position);
holder.text.setText(text.get(position));
holder.image.setTag(data.get(position));
//holder.checkBox.setTag(chkBox.get(position));
//this.chkBox.set(position, chkBox.get(position));
//this.chkBox.add(holder.checkBox);
imageLoader.DisplayImage(data.get(position), activity, holder.image,"Photo");
return vi;
}
return null;
}
}
How i call adapter
PhotoAdapter adapter = new PhotoAdapter(this, imageNameList, thumbURLList);
listPhoto.setAdapter(adapter); //listPhoto is my listView
(photolist.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/lblAlbumName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="#+id/listPhoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
(photoitemlist.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="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="50dip"
android:layout_height="50dip" android:src="#drawable/default_photo_icon" android:scaleType="centerCrop"/>
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:textSize="20dip"
android:layout_marginLeft="10dip"
/>
</LinearLayout>
please help..
thanks...
The error is, as you said. When you scroll it poops out. So your creating your views correctly but not recycling them correctly.
this.chkBox.add((CheckBox) vi.findViewById(R.id.check));
Looks like what's causing your error. You don't need to find the Id again as you already have a holder for it.
holder.text.setText(text.get(position));
holder.image.setTag(data.get(position));
You probably want that in your else statement

Categories

Resources