Display dropdown on click of image in android - android

In my android application, i am displaying images of different categories.When i click on these images i would like to get a small list of the items in that particular category.
What should i use for that.I am not sure which control will satisfy this and how can i use it.
Could any one please suggest me with a solution?
Please share your valuable suggestions.
Thanks in advance:)

Use ExpandableListView with a CursorAdapter. The group view can be your images and children can be the text.
Layout:
<?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">
<ExpandableListView android:id="#id/android:list"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:id="#id/android:empty" android:layout_width="fill_parent" android:gravity="center"
android:layout_height="wrap_content" android:text="Sorry, no data" />
</LinearLayout>
public class myListy extends ExpandableListActivity {
Cursor mCursor;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Define group data in mCursor
startManagingCursor(mCursor);
ExpandableListView mExpandableListView = (ExpandableListView) findViewById(android.R.id.list);
mExpandableListView.setGroupIndicator(null);
EAdapter adapter = new EAdapter(mCursor, getApplicationContext());
mExpandableListView.setAdapter(adapter);
}
private class EAdapter extends CursorTreeAdapter {
public EAdapter(Cursor cursor, Context context) {
super(cursor, context);
}
#Override
protected void bindChildView(View view, Context context, Cursor cursor,
boolean isLastChild) {
// Add your data to the child.
}
#Override
protected void bindGroupView(View view, Context context, Cursor cursor,
boolean isExpanded) {
// Add your data to the group.
}
#Override
protected View newChildView(Context context, Cursor cursor,
boolean isLastChild, ViewGroup parent) {
View view = getLayoutInflater().inflate(
R.layout.text_layout, null);
return view;
}
#Override
protected View newGroupView(Context context, final Cursor cursor,
boolean isExpanded, ViewGroup parent) {
View view = getLayoutInflater().inflate(
R.layout.images_layout, null);
return view;
}
#Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
// data for childern
return cursor;
}
}

Related

getView() method of custom adapter never gets called

I've read various SO threads on the topic but none of them seem to apply to my code.
I'm trying to populate a fragment with a ListView with my custom NearbyAdapter. However, my getView() method never gets called (as demonstrated by my logs not showing up). The view itself seems to be appropriately attached to my fragment, as demonstrated by the button in the view showing up, but not the ListView.
Relevant NearbyListFragment.java code:
public class NearbyListFragment extends ListFragment {
private int mImageSize;
private boolean mItemClicked;
private NearbyAdapter mAdapter;
private List<Place> places;
private LatLng mLatestLocation;
private static final String TAG = "NearbyListFragment";
public NearbyListFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "NearbyListFragment created");
View view = inflater.inflate(R.layout.fragment_nearby, container, false);
return view;
}
//TODO: Do asynchronously?
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
//Load from data source (NearbyPlaces.java)
mLatestLocation = ((NearbyActivity) getActivity()).getmLatestLocation();
//FIXME: Hardcoding mLatestLocation to Michigan for testing
//mLatestLocation = new LatLng(44.182205, -84.506836);
places = loadAttractionsFromLocation(mLatestLocation);
mAdapter = new NearbyAdapter(getActivity(), places);
ListView listview = (ListView) view.findViewById(R.id.listview);
//setListAdapter(mAdapter);
listview.setAdapter(mAdapter);
Log.d(TAG, "Adapter set to ListView");
mAdapter.notifyDataSetChanged();
}
private class NearbyAdapter extends ArrayAdapter {
public List<Place> placesList;
private Context mContext;
public NearbyAdapter(Context context, List<Place> places) {
super(context, R.layout.item_place);
mContext = context;
placesList = places;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Place place = (Place) getItem(position);
//FIXME: This never gets called
Log.d(TAG, "Place " + place.name);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_place, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvDesc = (TextView) convertView.findViewById(R.id.tvDesc);
// Populate the data into the template view using the data object
tvName.setText(place.name);
tvDesc.setText(place.description);
// Return the completed view to render on screen
return convertView;
}
#Override
public long getItemId(int position) {
return position;
}
The layout file of the fragment, fragment_nearby.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/btn_New">
</ListView>
<Button
android:id="#+id/btn_New"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="20dp"
android:text="Button"
android:width="170dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
And the layout file of the item, item_place.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<TextView
android:id="#+id/tvDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Desc" />
</LinearLayout>
Edit: Does anyone want to actually include a reason for the downvote? Especially when something like Custom Adapter for List View has 129 upvotes?
The issue is that ArrayAdapter does not know about List places:
Use this to fix it:
private static class NearbyAdapter extends ArrayAdapter<Place> {
public NearbyAdapter(Context context, List<Place> places) {
super(context, R.layout.item_place, places);
mContext = context;
placesList = places;
}
}
P/s: in this case, I think you need more control to set your place data to your view. Consider using BaseAdapter instead of ArrayAdapter.
Add following to your adapter:
#Override
public int getCount() {
return placesList.size();
}
After this you most likely encounter error with getItem so you will need to override that as well to return your object from the list.
You have to override getCount() method in ArrayAdapter to initialized listview like this:
#Override
public int getCount() {
return placesList.size();
}

How to create two different LinearLayout in a same ListView?

i want to combine two LinearLayout, both have different TextView arrangement in them, in a single ListView. so the final look should be like below:
and i run it with my code, but the app wouldn't start. below are my code.
Activity class:
public class MainActivity extends Activity {
// Create list of items
String[] publicModeItems = {
"AAAAA",
"BBBBB"
};
String[] publicModeParameters = {
"YES",
"NO"
};
String[] publicModeResetExe = {
"CCCCC",
"DDDDD"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateListView();
}
private void populateListView() {
CustomList adapter1 = new CustomList(this, publicModeItems, publicModeParameters);
// Build adapter
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
this, // context for the activity
R.layout.text_view_test, // layout to use (create)
publicModeResetExe); // items to display
// Configure the list view
ListView list = (ListView) findViewById(R.id.PublicModeListView);
list.setAdapter(adapter1);
list.setAdapter(adapter2);
}
private class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] publicModeItems;
private final String[] publicModeParameters;
public CustomList(Activity context,
String[] publicModeItems,
String[] publicModeParameters) {
super(context, R.layout.text_views_1, publicModeItems);
this.context = context;
this.publicModeItems = publicModeItems;
this.publicModeParameters = publicModeParameters;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutinflater = context.getLayoutInflater();
View rowView = layoutinflater.inflate(R.layout.text_views_1, null, true);
TextView txtPublicModeItems = (TextView) rowView.findViewById(R.id.PublicModeItems);
TextView txtPublicModeParameters = (TextView) rowView.findViewById(R.id.PublicModeParameters);
txtPublicModeItems.setText(publicModeItems[position]);
txtPublicModeParameters.setText(publicModeParameters[position]);
return rowView;
}
}
}
text_views_1 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/PublicModeLayoutForTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/PublicModeItems"
android:layout_width="200sp"
android:layout_height="wrap_content"
android:textColor="#drawable/text_color_change" />
<TextView
android:id="#+id/PublicModeOpenBracket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="["
android:textColor="#drawable/text_color_change" />
<TextView
android:id="#+id/PublicModeParameters"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:gravity="end"
android:textColor="#drawable/text_color_change" />
<TextView
android:id="#+id/PublicModeCloseBracket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="]"
android:textColor="#drawable/text_color_change" />
</LinearLayout>
text_view_test xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#drawable/text_color_change" >
</TextView>
activity_main xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/PublicModeListViewLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/black"
android:baselineAligned="true"
android:orientation="vertical"
tools:context="com.example.mycalendar.MainActivity" >
<LinearLayout
android:id="#+id/PublicModeListViewLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/custom_border"
android:padding="5dp" >
<ListView
android:id="#+id/PublicModeListView"
android:layout_width="308dp"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:listSelector="#color/yellow"
android:smoothScrollbar="true" >
</ListView>
</LinearLayout>
</LinearLayout>
Problem : I just run my code just now, and only 'CCCCC' and 'DDDDD' is showing. so do you have any idea on this?
You should inherit your list adapter from BaseAdapter rather than ArrayAdapter.
Rewrite adapter's getView() method to inflate layout according position, like below.
public View getView(int position, View convertView, ViewGroup parent) {
...
View rootView = null;
if (position == 0 || position == 1) {
rootView = layoutinflater.inflate(R.layout.text_views_1, null, true);
} else {
rootView = layoutinflater.inflate(R.layout.text_views, null, true);
}
...
return rootView;
}
You set the list adapter twice
list.setAdapter(adapter1);
list.setAdapter(adapter2);
in this case the current adapter is the second, not them both.
Use ListView with SimpleAdapter or a custom adapter and that will solve all your problems, check out this for SimpleAdapter tutorial to learn a the basics and this tutorial to learn how you can make multiple views instead of just 1. This tutorial is about creating custom adapter
BTW, I have few comments on your code and xml layout
First, there is no need for 2 TextViews for your bracket, you can easily add them programmatically to the String[] or even to YES/NO TextView.
Second, You're not using a ViewHolder in your CustomList adapter and this is not a good practice as every time you scroll your list, it creates new view although it can use already existing one.
I already solve this problem. but it seems not so convenient to use this method.
public class PublicModeActivity extends Activity {
// Create list of items
String[] publicModeItems = {
"AAAAA",
"BBBBB"
};
String[] publicModeParameters = {
"YES",
"NO"
String[] publicModeResEx = {
"",
"",
"CCCCC",
"DDDDD"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateListView();
}
private void populateListView() {
CustomList adapter = new CustomList(this, publicModeItems, publicModeParameters, publicModeResEx);
// Configure the list view
ListView list = (ListView) findViewById(R.id.PublicModeListView);
list.setAdapter(adapter);
}
private class CustomList extends BaseAdapter {
private final Activity context;
private final String[] publicModeItems;
private final String[] publicModeParameters;
private final String[] publicModeResEx;
public CustomList(Activity context,
String[] publicModeItems,
String[] publicModeParameters,
String[] publicModeResEx) {
super();
this.context = context;
this.publicModeItems = publicModeItems;
this.publicModeParameters = publicModeParameters;
this.publicModeResEx = publicModeResEx;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutinflater = context.getLayoutInflater();
View rowView = null;
Log.i("PublicModeActivity", "" + position);
if (position < 2) {
rowView = layoutinflater.inflate(R.layout.text_views_1, null, true);
TextView txtPublicModeItems = (TextView) rowView.findViewById(R.id.PublicModeItems);
TextView txtPublicModeParameters = (TextView) rowView.findViewById(R.id.PublicModeParameters);
txtPublicModeItems.setText(publicModeItems[position]);
txtPublicModeParameters.setText(publicModeParameters[position]);
} else {
rowView = layoutinflater.inflate(R.layout.text_view_test, null, true);
TextView txtPublicModeResEx = (TextView) rowView.findViewById(R.id.PublicModeResEx);
txtPublicModeResEx.setText(publicModeResEx[position]);
}
return rowView;
}
#Override
public int getCount() {
return 4;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
}
}
I need to put that blank string "", to match the number of row with the upper layout (text_views_1) even-though those blank string are use in different layout. so it is not so convenient especially when you want to display so many data. If someone here have much simpler method than this. feel free to share with me/us. i'm eager to learn. thank you!

ListFragment item not clickable. Content Provider and Custom Cursor Adapter

I have this simple program that populates data using Content Provider and using Custom cursor adapter. My problem is that the item of the listfragment is not clickable even though the item has a "android:textIsSelectable="true". I have done a debugging and i could see that onListItemClick is not called.
These are my XML
activity_item_list.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dream , Believe and Achieve!"
android:id="#+id/achieveButton"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/believe"
/>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/item_list"
android:name="com.toksis.lawofattraction.ItemListFragment" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" tools:context=".ItemListActivity"
tools:layout="#layout/fragment_item_detail"
android:descendantFocusability="blocksDescendants"
/>
</LinearLayout>
Fragment_item_detail.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/item_detail"
style="?android:attr/textAppearanceLarge" android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="16dp" android:textIsSelectable="true"
tools:context=".ItemDetailFragment"
/>
ItemlistFragment Oncreate snippet.
// Create an array to specify the fields to display in the list
// (only TITLE)
String[] from = new String[] { WishContentProvider.COLUMN_WISH };
// and an array of the fields to bind those fields to (in this
// case, just text1)
int[] to = new int[] { R.id.item_detail };
// Now create a simple cursor adapter and set it to display
/* mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, null, from, to, 0); */
mAdapter = new WishCursorAdaptor(getActivity(),
getActivity().getContentResolver()
.query(WishContentProvider.CONTENT_URI, from, null, null, null),
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setHasOptionsMenu(true);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
Cursor adapter :WishCursorAdapter
public class WishCursorAdaptor extends CursorAdapter {
private LayoutInflater inflater;
public WishCursorAdaptor(Context context, Cursor c,int autoRequry) {
super(context, c,autoRequry);
inflater = LayoutInflater.from(context);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.fragment_item_detail,parent,false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView wish = (TextView)view.findViewById(R.id.item_detail);
wish.setText(cursor.getString(cursor.getColumnIndex("wish")));
}
}
This is how.
public class ItemListFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.hello, container, false);
return v;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
//Do your stuff..
}
}
Reference

How to control radio button states within a custom SimpleCursorAdapter via bindView / newView for a listfragment

I have a listfragment that manages row(s) of 2 radio buttons per row, where only one button is allowed to be checked, but I've not been able to manage the radio button states properly.
I'm pretty familiar with listview's recycling of views. I've tried many different avenues to no success, I realize I have to keep the states of the radio buttons, list position and probably whether the button has been initialized yet separately, which I can to with an interface. It's where and when to set the radio button states within the adapter that I've been wrestling with. Anything I've tried has one condition or another interfering with the other. Like setting the radio button from the database initially, but when there is a recycled view with no radio button to be check and I do a radiogroup.clearCheck() that seems to override when the user chooses a button.
The state of the radio buttons should be controlled by 2 sources;
Database source:
1) Database column has a value I'd like to set for the radio button initially, which I'd like to show as checked (.setChecked(true)) when the user hasn't had any interaction with that row yet, besides scrolling/no clicks. OR the Database column is 0 and no radio button should be checked.
User Action:
2) The users selects one of the two buttons, this should override the value of the database column.
Following is the rough framework for the fragments and adapters that I have:
The listview row xml file stackoverflow_2_radiobuttons_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:divider="#color/gray"
android:dividerHeight="5dp" >
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/default_text_left_margin"
android:layout_marginRight="#dimen/default_text_left_margin"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rb_1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/radiogroup"
android:layout_weight="1"
android:gravity="top"
android:paddingBottom="#dimen/default_text_top_margin"
android:text="RadioButton 1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:checked="false" />
<RadioButton
android:id="#+id/rb_2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/rb_1"
android:layout_weight="1"
android:gravity="top"
android:paddingBottom="#dimen/default_text_top_margin"
android:text="RadioBUtton 2"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:checked="false"/>
</RadioGroup>
</LinearLayout>
</RelativeLayout>
The list fragment: StackOverFlow.java
public class StackOverFlow extends ListFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
SimpleCursorAdapter adapter;
Activity activity;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] columns = new String[] { Mydb.COL_RB_STATE };
int[] viewfields = new int[] { };
adapter = new StackOverFlowAdapter(getActivity(), R.layout.stackoverflow_2_radiobuttons_row.xml,
null, columns, viewfields);
setListAdapter(adapter);
getLoaderManager().initLoader(0, null, this);
}
Handler handler = new Handler();
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = new String[] {
Mydb.COL_RB_STATE };
CursorLoader loader = new CursorLoader(getActivity(),
MYProvider.CONTENT_URI, projection, null, null, null);
String[] argz = new String[] { "'text'" };
return loader;
}
#Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
((SimpleCursorAdapter) this.getListAdapter()).swapCursor(cursor);
}
#Override
public void onLoaderReset(Loader<Cursor> arg0) {
((SimpleCursorAdapter) this.getListAdapter()).swapCursor(null);
}
}
The custom list fragment adapter: StackOverFlowAdapter.java
public class StackOverFlowAdapter extends SimpleCursorAdapter {
private Context context;
private final LayoutInflater inflater;
private int pos = 0;
RadioGroup radiogroup;
int iRowID = -1;
String row_Id;
Activity TestActivity;
protected static class ViewHolder {
protected TextView h_rb1;
protected TextView h_rb2;
protected TextView h_radiogroup;
protected int h_position;
}
public StackOverFlowAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
inflater = LayoutInflater.from(context);
}
#Override
public boolean hasStableIds() {
return true;
}
ViewHolder holder;
public View newView(Context context, Cursor cursor, ViewGroup parent) {
holder = new ViewHolder();
View view = inflater.inflate(R.layout.stackoverflow_2_radiobuttons_row, parent, false);
holder.h_rb1 = (RadioButton) view.findViewById(R.id.rb_1);
holder.h_rb2 = (RadioButton) view.findViewById(R.id.rb_2);
holder.h_radiogroup = (RadioButton) view.findViewById(R.id.radiogroup);
holder.h_position = (pos);
if (cursor != null) {
bindView(view, context, cursor);
}
view.setTag(holder);
return view;
}
public void bindView(View view, final Context context, Cursor cursor) {
holder = (ViewHolder) view.getTag();
final RadioButton rb_1 = (RadioButton) view
.findViewById(R.id.rb_1);
final RadioButton rb_2 = (RadioButton) view
.findViewById(R.id.rb_2);
final RadioGroup radiogroup = (RadioGroup) view.findViewById(R.id.radiogroup);
radiogroup
.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == rb_1.getId()) {
rb_1.setChecked(true);
} else if (checkedId == rb_2.getId()) {
rb_2.setChecked(true);
}
}
});
}
}
I've seen a lot of answers using getView, but I haven't been able to find a good example or theory using bindView and newView in a custom SimpleCursorAdapter with radio button states. I haven't been successful manipulating anything in newView, I've been working mostly in bindView. So at this point I'm not sure how to proceed. Any help or guidance would be appreciated.
Have a great day.

Selected ListView items become unselected automatically

I just created a ListView that is backed by a CursorLoader and when items in the ListView are clicked, they remain highlighted.
Here is the xml:
<?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="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:choiceMode="singleChoice"
android:listSelector="#android:color/darker_gray"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
My issue is that in another fragment, I edit some data that makes changes to the internal database, and it seems that once my SyncAdapter runs, the selected item becomes unselected automatically. Does anyone have any experience with such problems?
EDIT:
Here's the code for the Fragment:
public class BidItemsFragment extends ListFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
public static final String ITEM_TYPE_BIDITEM= "B";
private SimpleCursorAdapter mAdapter;
private ListView mList;
private Context mContext;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mContext = getActivity();
fillData();
View v = inflater.inflate(R.layout.biditems_list, container, false);
mList = (ListView) v.findViewById(android.R.id.list);
return v;
}
private void fillData() {
String[] from = new String[] { Columns.COLUMN_ITEMNUMBER };
int[] to = new int[] { R.id.biditem_num };
getLoaderManager().initLoader(0, null, this);
mAdapter = new SimpleCursorAdapter(mContext, R.layout.biditems_row, null, from, to, 0);
setListAdapter(mAdapter);
}
#Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
String[] projection = Columns.getColumns(RawContract.PARAM_SEBIDITEM);
String selection = Columns.COLUMN_SYSID + "=?";
String selectionArgs[] = { "1" };
CursorLoader cursorLoader = new CursorLoader(mContext,
BidProvider.CONTENT_URI_SEBIDITEM, projection, selection, selectionArgs, null);
return cursorLoader;
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}

Categories

Resources