Android: ListView with expanding LinearLayouts as items causes duplicates - android

I'm trying to write a test application that consists of a few fragments.
One fragment should contain a listView of all music artists from the device.
Each item of this list is a linearlayout starting with a TextView with the artist name and an empty linearlayout under it as follows:
The list is of this layout:
<ListView
android:id="#+id/artistsLists"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ListView>
Each item is of the following layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/artistName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="" />
<LinearLayout
android:id="#+id/artistsAlbums"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
I'm populating the list using a SimpleCursorAdapter in the following way:
public class MusicTabFragment extends Fragment
{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_music_tab,container,false);
Cursor artistsCursor = getActivity().getContentResolver().query(Audio.Artists.EXTERNAL_CONTENT_URI, new String[]{Audio.Artists.ARTIST,Audio.Artists._ID}, null, null,Audio.Artists.ARTIST);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(view.getContext(), R.layout.music_artist_list_item_layout, artistsCursor, new String[]{Audio.Artists.ARTIST},new int[]{R.id.artistName},0 );
ListView lView = (ListView)view.findViewById(R.id.artistsLists);
lView.setAdapter(adapter);
lView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
((LinearLayout)view.findViewById(R.id.artistsAlbums)).removeAllViews();
Cursor albumsCursor = getActivity().getContentResolver().query(Audio.Artists.Albums.getContentUri("external", ((Cursor)parent.getItemAtPosition(position)).getLong(1)), new String[]{Audio.Albums.ALBUM, Audio.Albums._ID},null,null,null);
LinearLayout artistLayout = (LinearLayout)view.findViewById(R.id.artistsAlbums);
for(albumsCursor.moveToFirst();!albumsCursor.isAfterLast();albumsCursor.moveToNext())
{
View albumView = LayoutInflater.from(view.getContext()).inflate(android.R.layout.simple_list_item_1,artistLayout,false);
((TextView)albumView.findViewById(android.R.id.text1)).setText(albumsCursor.getString(0));
artistLayout.addView(albumView);
}
Log.d("POPULATE","populated again!");
albumsCursor.close();
}
});
return view;
}
}
This works just fine. when i click an artist name, the linearlayout populates with all of this artist album names.
the problem is, that once a linearLayout scrolls out of view, it shows again from the other edge of the view (PacMan Style) as if another list item's linearLayout was populated.
It happens every time the expanded layout goes out of sight. the funny part is that some times when scrolling back up, the linearLayout shows under a different artist name.
example
I'll be glad to hear how should I implement this fragment. But i will also like to know why this behavior is caused.
Thanks,
Maor.

I have found the solution here at stackoverflow.com
It appears that the view shouldn't hold any data, since it is being used for different data when i scroll back and fourth.
I think holding an external data structure to save each virtual view state is not nice programming. is there a way to keep this data anyway? (for this i will be looking now)

Related

How to create a fragment with a list without ListFragment

There's very little literature on this topic, and google's documents don't account for the possibility of customization (listviewanimation) of the fragment's list using ListFragment extension. Therefore, I'm going to ask this question, and then answer it as best as possible, because I also want 50 reputation points so I can finally thank great explainers on this website through comments.
For the purpose of this comment, I will have components from the listviewanimation lib laced in:
https://github.com/nhaarman/ListViewAnimations
Answer:
We will need to set up 4 components to have a proper fragment with a listview component
The Activity Creating the fragment through the activity's fragment manager.
The Fragment class which will be pretty basic fragment stuff, it will have the listview, and it will link that listview with an arrayadapter.
The Adapter class which for our purposes will only handle strings.
WITHIN THE ADAPTER CLASS the final fourth component will be a viewholder class which will allow the rows within the list to be created faster, since each row's individual components will be wrapped up in a class that will allow for quicker object instantiation.
Ok so, first will be the code for the activity, this code can be called by a button click or some other event. When the event happens, the fragment manager will be created, and that fragment manager will create a transaction which is a fancy way of saying, the manager will communicate between the activity and the newly formed fragment to get everything set up properly.
Here's the code that should be placed in your activity where the event occurs:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
GenericFragment fragment = new GenericFragment();
fragmentTransaction.add(R.id.pager, fragment);
//Replace R.id.pager with the view that you want your fragment to go in.
fragmentTransaction.commit();
That's it! Not so bad, is it? Now let's move on to the GenericFragment class, which you can create a different name of course. I won't be posting all the code for this, but I'll step through everything you need for a fragment class that has a listview:
Have your Fragment class extend Fragment
Have an empty constructor for this class (google requires it... -__- )
Create a newInstance method which will handle the passing of data from the activity to the fragment when a 'new instance' of the fragment is created from the activity:
I'll help you with this one:
public static GenericFragment newInstance(String StuffYouWantGetsPassedFromActivityToFragment) {
GenericFragment GenericFragment = new GenericFragment();
Bundle args = new Bundle();
GenericFragment.setArguments(args);
return GenericFragment;
}
Again not so bad, right? We're still not done, we still need to override onCreateView and onCreate, then we'll be done with this simple step!
Ok for onCreateView:
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.generic_fragment_layout, container, false);
addGoalButton = (Button) view.findViewById(R.id.btn_newRow); //Created for testing purposes
lv = (ListView) view.findViewById(R.id.GenericListView);
addGoalButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { //Created for testing purposes
genericAdapter.add("Goal");
genericAdapter.notifyDataSetChanged();
}
});
lv.setAdapter(genericAdapter);
return view;
}
That above code may seem like a monstrosity, and you're right! The high level overview is that you're getting the layout file that you want the fragment to look like. From that layout file, you're getting the listview and creating a variable to hold it in. Then you're calling that listView's 'setAdapter' method to add the next step, the adapter class. For testing purposes, I added that button, so that you can mentally extend this tutorial l8er. (delete all button code if you'd like just a list)
Ok, one last step in the fragment class: Overriding OnCreate!
The OnCreate method is where you want to instantiate all your private variables like the genericAdapter variable or anything that you'd like to use over the multiple parts of the Fragment class.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> exampleItemList = new ArrayList<String>();
exampleItemList.add("item1");
exampleItemList.add("item2");
exampleItemList.add("item3");
exampleItemList.add("item4");
exampleItemList.add("item5");
exampleItemList.add("item6");
exampleItemList.add("item7");
exampleItemList.add("item8");
exampleItemList.add("item9");
exampleItemList.add("item10");
exampleItemList.add("item11");
exampleItemList.add("item12");
genericAdapter = new genericAdapter(getActivity(), 0, exampleItemList);
setHasOptionsMenu(true); // Allows the fragment to change the menu buttons
}
I added the example items to an arrayList to make this tutorial a bit more transparent about where data is coming from, and where it's going.
That's it! You're Fragment is done! It's almost over, I promise.
Let's knock these last two steps out together, creating a GenericAdapter class that extends ArrayAdapter and has a private inner ViewHolder class to wrap all the layout components in:
public class GenericAdapter extends ArrayAdapter<String>
LayoutInflater layoutInflater;
//Used to get the correct LayoutInflater to inflate each row item
public GenericAdapter(Context context, int resource, List<String> objects) {
super(context, 0, objects);
layoutInflater = layoutInflater.from(context);
}
/**
* #param position The position in the list to get the data for that row item.
* #param convertView The view for the row item that will be shown in the list.
* #param parent Having this object allows you to use the LayoutInflater for the parent.
* #return
*/
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final GenericViewHolder GenericViewHolder;
final String item = getItem(position);
if(convertView == null){
LinearLayout rootView = (LinearLayout) layoutInflater.inflate(R.layout.item_row, parent, false);
genericViewHolder = genericViewHolder.create(rootView);
rootView.setTag(genericViewHolder);
}
else{
genericViewHolder = (genericViewHolder) convertView.getTag();
}
genericViewHolder.textView.setText(item);
return genericViewHolder.rootView;
}
/**
* ViewHolder's allow for a single object to maintain a Goal row item, so that the row item
* doesn't have to create each individual component (textview layout etc.) each time the
* row object is created/recreated. Allows for fast scrolling with little latency.
*/
private static class GenericViewHolder {
public final LinearLayout rootView;
public final GripView gripView;
public final TextView textView;
private GoalViewHolder(LinearLayout rootView, GripView gripView, TextView textView) {
this.rootView = rootView;
this.gripView = gripView;
this.textView = textView;
}
public static GoalViewHolder create(LinearLayout rootView){
TextView textView = (TextView)rootView.findViewById(R.id.list_row_draganddrop_textview);
GripView gripView = (GripView)rootView.findViewById(R.id.list_row_draganddrop_touchview);
return new GenericViewHolder(rootView, gripView, textView);
}
}
}
That was again, a monstrosity, let's look at the high level overview, we created an adapter class, and a viewholder class for the adapter class to use. In the adapter's constructor we got a layoutinflater to help with inflating each row's item. Then, we created the getView method which get's called thousands of times in your app, because it handles making the each row appear when it's viewable by the user. The getView method sees if the view to be converted into a row is null or not. If it is, it will create a new data entry (a viewholder), but if it's not null, then that viewholder has already been created, so we get whatever was inside the viewholder already, so that we don't have to create a new row item.
phew! I don't expect you to understand any of that, but congrats if you do.
Ok so that's it. You should be set, and when your activity's event get's called, the fragment will show up in whatever view is containing the fragment. I'll post my xml files in my answer so that I can get those delicious upvotes (or not, I may be completely incorrect, but this worked for me!)
enjoy life, don't give up!
The activity xml, most of it is irrelevant to you the reader, but the container view for the fragment is pager:
<RelativeLayout 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"
tools:context=".MainActivity">
<!--Navigation Drawer Still Under Construction-->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#ffff"/>
</android.support.v4.widget.DrawerLayout>
<!--Navigation Drawer Still Under Construction-->
<!--Customizations on README at: https://github.com/astuetz/PagerSlidingTabStrip-->
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/tabs"
android:layout_width="wrap_content"
android:layout_height="48dip"
app:pstsShouldExpand="true"
app:pstsIndicatorHeight="5dip"
app:pstsDividerPadding="0dip"
app:pstsDividerColor="#ff6d00"
app:pstsUnderlineColor="#ff5722"
app:pstsIndicatorColor="#ff5722"/>
<!--To scale the viewpager vertically, android:layout_above="#+id/[viewname]" -->
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tabs"
tools:context=".MainActivity" />
</RelativeLayout>
The xml layout for the fragment:
<?xml version="1.0" encoding="utf-8"?>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Item"
android:id="#+id/btn_newItem"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<com.nhaarman.listviewanimations.itemmanipulation.DynamicListView
android:id="#+id/GenericListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/btn_newGoal" />
The specific row item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="UseCompoundDrawables">
<com.nhaarman.listviewanimations.itemmanipulation.dragdrop.GripView
android:id="#+id/list_row_draganddrop_touchview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:color="#android:color/darker_gray"
android:paddingBottom="4dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp" />
<TextView
android:id="#+id/list_row_draganddrop_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_vertical"
android:minHeight="48dp"
android:textColor="?android:attr/textColorSecondary"
android:textSize="20sp"
tools:ignore="UnusedAttribute" />
</LinearLayout>
The layout portion of the 2nd code snippet got cut off and SO wasn't agreeing with my ctrl K'ing, but the long and short of it, is that it doesn't matter, because the listview is there, so it doesn't matter whether you put it in a linear layout or a relative layout.
Good luck bro's happy coding

(Android) How do i access and modify a specific element in a custom listview, seeing as it uses a custom adapter?

I have a custom list view, here is its layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/playerToken"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/playerName"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/playerMoney"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And here is the custom Adapter which takes an array of a Player Object which stores information like the players name, his token to represent him, and his money balance. the adapter takes that information and populates my custom list, as the layout above.
public class MyAdapter extends ArrayAdapter<Player> {
public MyAdapter(Context context, Player[] values) {
super(context, R.layout.activity_banking_layout, values);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater theInflater = LayoutInflater.from(getContext());
View theView = theInflater.inflate(R.layout.activity_banking_layout, parent, false);
Player player = getItem(position);
TextView playerNameText = (TextView) theView.findViewById(R.id.playerName);
TextView playerMoneyText = (TextView) theView.findViewById(R.id.playerMoney);
ImageView playerToken = (ImageView) theView.findViewById(R.id.playerToken);
playerNameText.setText(player.getName());
playerMoneyText.setText(Integer.toString(player.getMoney()));
int rId = theView.getResources().getIdentifier(player.getToken(), "drawable",
getContext().getPackageName());
playerToken.setImageResource(rId);
return theView;
}
}
This is just the layout showing the listView that us being adapted:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bankListView" />
</LinearLayout>
So basically, my list is created and adapter in the onCreate method of the activity showing the list. After that my list items can open a context menu and depending on what is selected the player objects are manipulated. I want my list to reflect these changes, so i was wondering how i can access a specific part of my custom list and edit it. For example my custom list has a player icon, and to the right, the players name and below the name a money amount. Lets say i want to change the money amount of a specific player and reflect that change on the list, how do i access that specific TextView, within that specific position in that ListView ?
Since your adapter 'MyAdapter' extends ArrayAdapter, you can try using getItem(int position) which return the list item at the specified position; a Player object in your case. From there, you can modify your object's data member (amount of money) and then refresh the list to reflect your changes with notifyDataSetChanged().
In case you wanted to know the index/position of the clicked list item, then your question would be a duplicate of this one.
instead of manipulating the textview, consider the following approach:
make the Player[] array a global variable, instead of passing it to constructor,. then manipulate the Player object directly, then call
notifyDataSetChanged()
on your adapter which will automatically update your correct textview.
private Player[] values;
public void onCreate(Bundle s){
/*your onCreate things*/
MyAdapter adapter = new MyAdapter(this);
// update player 5 from array
values[4].money = newMoneyVal;
// after updating, call notifyDataSetChanged()
adapter.notifyDataSetChanged();
}
public class MyAdapter extends ArrayAdapter<Player> {
public MyAdapter(Context context) {
super(context, R.layout.activity_banking_layout, values);
}
/*rest of your adapter class*/
}
let me know if this works or if you have any questions
Yuo just have to implement a onlistitemclick listener, like this:
#Override
protected void onListItemClick(ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);
l[pos].findViewById(R.id.yourTagName).setText("ChangedValue");//to be adapted
}

headerView does not scroll with the listview Android

I am using a DragSortListView and i want a header which will scroll down and up with the list. I have no idea why the header is not scrolling with the list. I use the listview in a fragment and i added the header like this:
public void onViewCreated(View view, final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
DragSortListView cursListView = (DragSortListView) view.findViewById(R.id.drag_list);
LayoutInflater inflater = (LayoutInflater) Utils.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
titleLayout = inflater.inflate(R.layout.title_row, null);
cursListView.addHeaderView(titleLayout);
setTitle(titleName, titleValue);
cursListView.setAdapter(cursorAdapter);}
setTitle sets the values for the header and Utils.getContext() return the context of the Application.
public void setTitle(String currency, float value) {
((TextView) titleLayout.findViewById(R.id.titleName)).setText(currency);
((EditText) titleLayout.findViewById(R.id.titleValue)).setText(String.valueOf(value));
((EditText) titleLayout.findViewById(R.id.titleValue)).setImeActionLabel(getString(R.string.convert), EditorInfo.IME_ACTION_DONE);
((EditText) titleLayout.findViewById(R.id.titleValue)).setOnEditorActionListener(convertCurrencies);
restartLoader();
}
Why not create the illusion of a header?
<?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="vertical" >
<TextView
android:id="#id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
This way the TextView, in this case, will always stay on top, the ListView can still scroll, which is the desired result if I understand your question correctly.
i think you have first understand what is Header of Footer. Header means that is Always on Top and visible in any Condition same as Footer which is always on bottom and visible.that same concept use for HeaderView and FooterView in listview in android.
So Achive your goal Add one item as header and get it in your Adapterclass getView method and Display it. if there are only one header then at 0 position Your Header available and other position your item available. and it is scroll with your list.
Thats it...

Android ListView select items to delete with 3 column layout

I'd like to implement a Listview in android in which I have the possibility to enable a delete mode, in which the user can select the entries to delete. It should be similar to the message application in android.
I already have a ListActivity which has an icon on the left and text on the right side. I now like to add a CheckBox floating on the right side of the list entry. The ListActivity is listed in another question by a friend of mine: android listactivity background color .
The layout should be:
Left Picture
Center List item
Right Checkbox for delete selection
How can I achieve this? Is there a standard ListView item in the framework I could use?
I guess you want a CheckBox to appear(only) when is time to delete items from the ListView. Assuming you use the layout from the other question:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#color/darkbluelogo" >
<ImageView android:id="#+id/list_image"
android:layout_width="48dip"
android:layout_height="48dip"
android:contentDescription="#id/list_image"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:padding="5dp"
android:background="#color/darkbluelogo"
android:scrollingCache="false"
android:cacheColorHint="#00000000" >
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/title" >
</TextView>
<TextView
android:id="#+id/datetime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/datetime" >
</TextView>
</LinearLayout>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
When the ListView starts the CheckBox will not be present and the content TextViews will occupy all the space. Add a flag in the getView method of your adapter that will signal that the CheckBox must be shown(here you will set the CheckBox's visibility from the layout to visible). When its time to delete items modify the flag and then call notifyDataSetChanged() so the ListView redraws its children, this time with the CheckBox present.
Note:
You'll have to store the status of the CheckBoxes yourself.
First of all you need a custom layout for your list entries. A simple RelativeLayout including an ImageView , a TextView and a CheckBox should be enough.
Then you might want to build your own custom adapter which can extend BaseAdapter (or SimpleAdapter or CursorAdapter or ArrayAdapter or...). The adapter will bind the list's data to your custom layout. If for example your data is contained in a Cursor it will look like:
private class MyCustomAdapter extends CursorAdapter {
public MyCustomAdapter(Context context) {
super(context, null);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
//Return a list item view
return getLayoutInflater().inflate(R.layout.my_custom_list_item_layout, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
//Get views from layout
final ImageView imageView = (ImageView) view.findViewById(R.id.list_item_image);
final TextView textView = (TextView) view.findViewById(R.id.list_item_text);
final CheckBox checkBox = (CheckBox) view.findViewById(R.id.list_item_checkbox);
//Get data from cursor
final String text = cursor.getString(...);
//Add listener to the checkbox
checkBox.setOnClickListener(new OnClickListener() {...});
//Bind data
textView.setText(text);
}
}

How to wrap text using simple_list_item_multiple_choice?

I'm using a SimpleCursorAdapter to display a single CheckedTextView. I know this is done best using simple_list_item_multiple_choice and android.R.id.text1.
adapter = new SimpleCursorAdapter(getApplicationContext(),
android.R.layout.simple_list_item_multiple_choice, rules,
new String[]{Constants.KEY_RULE}, new int[]{android.R.id.text1});
If the text from KEY_RULE is more than two lines, android doesn't wrap, instead it hides it. Is there an easy work-around for this without having to implement my own adapter?
Find my xml code below:
<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/header" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="#string/rule_selection_for_revision_list_title"
android:padding="4dip" android:background="#000000" android:textColor="#ffffff" android:textStyle="bold" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_weight="1.0" />
<TextView android:textSize="18sp" android:textStyle="bold"
android:id="#id/android:empty" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center_vertical|center_horizontal"
android:text="There are no rules to consider." />
</LinearLayout>
Is there any way I can at least reduce the font size so that it fits to two lines?
There are several options.
You can define your custom layout for item in the list. This allows you to fully customize your item UI. More details can be found in List View tutorial.
The other approach can be used if you still want to use standard item layout but only fix one small thing in it. You can extend Adapter and modify the view in getView function. Here is an example:
class MySimpleCursorAdapter extends SimpleCursorAdapter
{
public MySimpleCursorAdapter(Context ctx, int layout,
Cursor cursor, String [] from, int [] to)
{
super(ctx, layout, cursor, from, to);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
TextView textView = (TextView)view.findViewById(android.R.id.text1);
//You can increase number of lines in the text view
textView.setMaxLines(5);
LayoutParams params = textView.getLayoutParams();
if(params.height > 0)
{
int height = params.height;
params.height = LayoutParams.WRAP_CONTENT;
textView.setLayoutParams(params);
textView.setMinHeight(height);
}
//Or you can make your font smaller
textView.setTextSize(customTextSize);
//or whatever you like, even apply new custom style
//...
return view;
}
}
You should probably choose the first approach (custom layout), but sometimes the second approach is feasible too.
You don't need to implement a custom Adapter. Just define a custom layout for the list items and pass it to the adapter instead of android.R.layout.simple_list_item_multiple_choice.
You can search the Android sources (come with the SDK on you computer) to how the original simple_list_item_multiple_choice layout looks like and adapt it to your needs.

Categories

Resources