Selected ListView items become unselected automatically - android

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);
}
}

Related

CursorAdapter not displaying elements in ListView

I am trying to get a column of SQLITE database to a listview using a SimpleCursorColumn. I am getting the accurate number of rows in the list view, but the text is not appearing in the list. I printed out the content of the cursor and it contains the correct elements.
Why is the text not showing up in the list? Surely it must be easy that I am being stupid about.
public class Tab3Fragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int LOADER_ID = 42;
private CursorAdapter _adapter;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab3_frag, container, false);
ListView lv = view.findViewById(R.id.student_view);
_adapter = new SimpleCursorAdapter(getContext(),
R.layout.container_list_item_view, null,
new String[] { Contract.Student_Table.STUDENTS_COLUMN_NAME},
new int[] { R.id.list_item });
lv.setAdapter(_adapter);
getLoaderManager().initLoader(LOADER_ID, null, this);
return view;
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (id != LOADER_ID) {
return null;
}
return new CursorLoader(getContext(),
Contract.Student_Table.CONTENT_URI,
new String[] { Contract.Student_Table.STUDENTS_COLUMN_ID, Contract.Student_Table.STUDENTS_COLUMN_NAME}, null, null,
null);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
_adapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
_adapter.swapCursor(null);
}
}
tab3_frag.xml
<?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="match_parent"
android:background="#c2f6ff"
android:orientation="vertical">
<ListView
android:id="#+id/simple_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/student_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#android:color/black" />
</LinearLayout>
Better use loader. Try below example:
public class LanguageListActivity extends ListActivity implements
LoaderCallbacks<Cursor> {
private static final int LOADER_ID = 42;
private CursorAdapter _adapter;
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.container_list);
_adapter = new SimpleCursorAdapter(this,
R.layout.container_list_item_view, null,
new String[] { DatabaseConstants.COL_LANG_NAME },
new int[] { R.id.list_item });
setListAdapter(_adapter);
getLoaderManager().initLoader(LOADER_ID, null, this);
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (id != LOADER_ID) {
return null;
}
return new CursorLoader(LanguageListActivity.this,
LanguageContentProvider.CONTENT_URI,
new String[] { DatabaseConstants.COL_LANG_ID, DatabaseConstants.COL_LANG_NAME }, null, null,
null);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
_adapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
_adapter.swapCursor(null);
}
}
container_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
container_list_item_view.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="fill_parent"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="2dip" >
<TextView
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dip" />
</LinearLayout>

Android GridView leave trail when scrolling

I have a GridView that is implemented within an Activity and a Fragment, and contain its own Adapter. It works great, until scrolling, when I try to scroll it appears to leave a background of the first items loaded.
Here's the code I am implementing:
Activity Class
public class FavoriteActivity extends ActionBarActivity {
private final String LOG_TAG = FavoriteActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorite);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new FavoriteListFragment())
.commit();
}
}
#Override
protected void onResume() {
super.onResume();
FavoriteListFragment fragment = (FavoriteListFragment)getSupportFragmentManager().findFragmentById(R.id.container);
if ( null != fragment ) {
fragment.onRestartLoader();
}
}
}
Fragment Class
public class FavoriteListFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
public static final String LOG_TAG = FavoriteListFragment.class.getSimpleName();
private static final int LOADER = 0;
private static final String SELECTED_KEY = "selected_position";
private ResultListAdapter mAdapter;
private GridView mGridView;
private int mPosition = ListView.INVALID_POSITION;
public static final String[] SMCONTENT_COLUMNS = {
SMDBContract.SMContentEntry.COLUMN_TITLE,
};
static final int COL_TITLE = 0;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
getLoaderManager().initLoader(LOADER, null, this);
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
mAdapter = new ResultListAdapter(getActivity(), null, 0);
mAdapter.setIsFavoriteView(true);
View rootView = inflater.inflate(R.layout.fragment_favorite, container, false);
mGridView = (GridView) rootView.findViewById(R.id.listview_sm_content_favorite);
mGridView.setAdapter(mAdapter);
if (savedInstanceState != null && savedInstanceState.containsKey(SELECTED_KEY)) {
mPosition = savedInstanceState.getInt(SELECTED_KEY);
}
return rootView;
}
void onRestartLoader() {
getLoaderManager().restartLoader(LOADER, null, this);
}
#Override
public void onSaveInstanceState(Bundle outState) {
// When tablets rotate, the currently selected list item needs to be saved.
// When no item is selected, mPosition will be set to Listview.INVALID_POSITION,
if (mPosition != ListView.INVALID_POSITION) {
outState.putInt(SELECTED_KEY, mPosition);
}
super.onSaveInstanceState(outState);
}
#Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
String favs = Utility.getFavoritesIds(getActivity());
Uri resultSearchURI = SMDBContract.SMContentEntry
.buildSMContentMultiple(SMDBContract.CATEGORY_SERIE, favs);
Log.d(LOG_TAG,"PREFS URI: "+resultSearchURI);
return new CursorLoader(getActivity(),
resultSearchURI,
SMCONTENT_COLUMNS,
null,
null,
null);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
mAdapter.setCursor(data);
if (mPosition != ListView.INVALID_POSITION) {
mGridView.smoothScrollToPosition(mPosition);
}
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
Adapter
public class ResultListAdapter extends CursorAdapter {
private Context mContext;
private Cursor mCursor;
public ResultListAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
mContext = context;
mCursor = c;
}
public Cursor getCursor() {
return mCursor;
}
public void setCursor(Cursor mCursor) {
this.mCursor = mCursor;
}
public static class ViewHolder {
public final ImageView iconView;
public final TextView titleView;
public ViewHolder(View view) {
iconView = (ImageView) view.findViewById(R.id.list_item_icon);
titleView = (TextView) view.findViewById(R.id.list_item_title_textView);
}
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.grid_item, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
return view;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
if (viewHolder.iconView != null) {
Picasso.with(context)
.load(cursor.getString(ResultListFragment.COL_POSTER))
.fit()
.centerInside()
.into(viewHolder.iconView);
}
if (viewHolder.titleView != null)
viewHolder.titleView.setText(cursor.getString(ResultListFragment.COL_TITLE));
}
}
Activity Layout
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:name=".FavoriteListFragment"
tools:context=".FavoriteListFragment"
tools:layout="#android:layout/list_content" />
Fragment Layout
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="![enter image description here][1].FavoriteListFragment">
<GridView
android:id="#+id/listview_sm_content_favorite"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</FrameLayout>
Grid Item Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_gravity="center"
>
<ImageView
android:id="#+id/list_item_icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#mipmap/ic_launcher"/>
</FrameLayout>
<TextView
android:id="#+id/list_item_title_textView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:fontFamily="sans-serif-light"
android:layout_gravity="center"
android:textAppearance="?android:textAppearanceMedium"
android:text="My Item Title"/>
</LinearLayout>
UPDATE:
I just found out that by removing this code from the FavoriteActivity.onCreate the error stops... but why!? can somebody tell me
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new FavoriteListFragment())
.commit();
}
Remove following lines from your Grid Item Layout:
from ImageView remove:
android:src="#mipmap/ic_launcher"
from TextView remove:
android:text="My Item Title"

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

Android listView - format specific items

So I'm relatively new to android programming, and currently working on a custom listView. What I'm trying to achieve is the following:
Retrieve a list of articles from a database.
Get all unique values from the 'category' column to create a list of
categories.
Populate a listView with these categories
Add a listener for each category click which takes the user to the
first article in that category.
I've managed to do all of the above, but now want to take it further. I have a status column in the database, where the value is either 'read' or 'unread'. What I want to do is the following:
If all articles in a category are 'read', to grey-out that category
in the listview, and to ignore clicks on that particular item.
I have absolutely no idea how to format individual items within a listview...any suggestions? My code is as follows:
start.java:
public class Start extends Activity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
public static final String MYDATABASE_NAME = "questions.db";
public static final String MYDATABASE_TABLE = "questions";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_CONTENT = "category";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
// Query database
SQLiteDatabase db;
db = openOrCreateDatabase(
"questions.db"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
ArrayList<String> categoryList = new ArrayList<String>();
Cursor cur = db.query("questions", null, null, null, null, null, null);
cur.moveToFirst();
while (cur.isAfterLast() == false) {
if (categoryList.contains(cur.getString(11))) {
// do nothing
} else {
// add to list
categoryList.add(cur.getString(11));
}
cur.moveToNext();
}
cur.close();
Collections.sort(categoryList);
categoryList.add(0, "All");
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
// Create ArrayAdapter using the category list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, categoryList);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
mainListView.setTextFilterEnabled(true);
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
String clickedCat = (String) mainListView.getItemAtPosition(position);
finish();
Intent myIntent = new Intent(getApplicationContext(), NextClass.class);
myIntent.putExtra("passedCategory", clickedCat);
myIntent.putExtra("startTrigger", "go");
startActivity(myIntent);
}
});
}
}
start.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"
android:background="#android:color/black">
<TextView
android:text="#string/categories"
android:layout_width="fill_parent"
android:layout_height="#dimen/topbar_container"
android:background="#drawable/topgradient"
android:gravity="center_vertical|center_horizontal"
android:textColor="#color/primarytext"
android:textSize="#dimen/topbar_font"
android:textStyle="bold"
android:shadowColor="#000000"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="2"></TextView>
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mainListView"></ListView>
</LinearLayout>
simplerow.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" android:textStyle="bold">
</TextView>
You need to set up a ViewBinder, then call listAdapter.setViewBinder.
You need to create a custom adapter for this:
CustomAdapter.class:
public class CustomAdapter extends SimpleCursorAdapter{
Cursor dataCursor;
LayoutInflater mInflater;
Context context;
ArrayList<String[]> arrayList;
public static HashMap<Integer,String> myList=new HashMap<Integer,String>();
public CustomAdapter(Context context, int layout, Cursor dataCursor, String[] from,
int[] to) {
super(context, layout, dataCursor, from, to);
this.context=context;
this.dataCursor = dataCursor;
mInflater = LayoutInflater.from(context);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView==null)
{
convertView = mInflater.inflate(R.layout.custom_listitem, null);
holder = new ViewHolder();
holder.textview=(TextView)convertView.findViewById(R.id.textview);
holder.layout=(LinearLayout)findViewById(R.id.layout);
convertView.setTag(holder);
}
else
{
holder=(ViewHolder)convertView.getTag();
}
dataCursor.moveToPosition(position);
String id=Integer.toString(dataCursor.getInt(dataCursor.getColumnIndexOrThrow("_id")));
myList.put(position, id);
holder.textview.setText(dataCursor.getString(11));
if(dataCursor.getString(dataCursor.getColumnIndex("index")).equals("read"))
{
holder.layout.setBackgroundColor(Color.GRAY);
}
// other code according to the functionality you want
return convertView;
}
static class ViewHolder
{
TextView textview;
LinearLayout layout;
}
}
custom_listitem.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"
android:background="#FFFFFF"
android:id="#+id/layout"
android:padding="5dip"
>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:gravity="left"
android:padding="5dip"
android:id="#+id/textview"
/>
</LinearLayout>
Now,use it in activity like:
MainActivity.class:
...
mainListView = (ListView) findViewById( R.id.mainListView );
Cursor cursor = db.query("questions", null, null, null, null, null, null);
CustomAdapter ca=new CustomsAdapter(context,R.layout.custom_listitem,cursor,new String[]{"id","index"},new int[]{R.id.textview,R.id.textview});
mainListView.setAdapter(ca);
mainListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
String id=CustomAdapter.myList.get(position);
Cursor findIndex=db.query("questions", null, "_id="+id, null, null, null, null);
findIndex.moveToFirst();
if(findIndex.getString(findIndex.getColumnIndex("index").equals("unread"))
{
// do something for "unread" categories
}
}
});
...
I don't know the way,you can disable click on a particular listitem but yes,you can do the stuff under certain condition to let user feel like some of the items are unclickable.

Display dropdown on click of image in 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;
}
}

Categories

Resources