NullPointerException error in my ListView - android

I have a fragment which I want to display a ListView
here's a portion of my code
public class ExpensesDaily extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
lvExpenses = (ListView) getActivity().findViewById(android.R.id.list);
Cursor expensesListCursor;
DbControl dbc = new DbControl(getActivity());
try {
dbc.open();
expensesListCursor = dbc.listExpenses();
String[] from = new String[] {"description", "cost" };
ListAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.list_expenses, expensesListCursor, from, new int[] { R.id.tvDescription, R.id.tvCost },0);
lvExpenses.setAdapter(adapter);
}catch (SQLException e){
}
dbc.close();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.expenses_daily, container, false);
return rootView;
}
}
** DbControl class**
public class DbControl {
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
public DbControl(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Cursor listExpenses(){
Cursor cursor;
cursor = database.query(MySQLiteHelper.TABLE_EXPENSES, new String[] {"id _id", "description, cost"}, "forDate='" + date + "'", null, null, null, "id asc");
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
expenses_daily.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ll_expensesDaily"
android:baselineAligned="false">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</LinearLayout>
list_expenses.xml
<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="wrap_content"
android:orientation="horizontal"
android:padding="7dip">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:id="#+id/tvDescription" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:id="#+id/tvCost" />
</LinearLayout>
</LinearLayout>
but everytime I load the fragment, it will display this error
FATAL EXCEPTION: main java.lang.NullPointerException at com.code.imin.mymoneymanaged.ExpensesDaily.onCreate(ExpensesDaily.java:60)
which points to the line
lvExpenses.setAdapter(adapter) in class ExpensesDaily.

lvExpenses = (ListView) getActivity().findViewById(android.R.id.list);
You can't call getActivity().findViewById on onCreate because the view was not created yet. Change that logic to onActivityCreated

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>

Alphabet Index in ListView with SimpleCursorAdapter very slow

I am trying to implement the Alphabet-Index in ListView with SimpleCursorAdapter.
It is working but it is very slow, when i am trying to scroll the list up and down the phone is getting strucked. I think its is happening because of my way of coding due to lack of knowledge. so that I need to know what is the mistake or what is the wrong of this code. And also if there is any other way than this way, please let me know.
This is set of code of fragment which has the above problem.
public class EnFragment extends Fragment {
private TestAdapter dbHelper;
private myCursorAdapter dataAdapter;
public EnFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbHelper = new TestAdapter(getContext());
dbHelper.createDatabase();
dbHelper.open();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_en, container, false);
setListView(view);
return view;
}
private void setListView(View view) {
Cursor cursor = dbHelper.getAllWord("enDic");
String[] columns = new String[]{
"word",
"definition",
"_id",
"favourite"
};
int[] to = new int[]{
R.id.txt_word,
R.id.txt_def,
R.id.txt_id
};
dataAdapter = new myCursorAdapter(
getContext(), en_word_row,
cursor,
columns,
to,
0);
Button btnSubmitEng = (Button) view.findViewById(R.id.btnSubmitEng);
final ListView listView = (ListView) view.findViewById(R.id.listView);
listView.setAdapter(dataAdapter);
listView.setEmptyView(btnSubmitEng);
listView.setFastScrollEnabled(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
final String word = cursor.getString(cursor.getColumnIndexOrThrow("word"));
Toast.makeText(getContext(), word, Toast.LENGTH_SHORT).show();
}
});
}
public void textSearch(String searchTxt){
String newStr = searchTxt.replace(" ","");
dataAdapter.getFilter().filter(newStr);
dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return dbHelper.getWordByName("enDic",constraint.toString());
}
});
}
private class myCursorAdapter extends SimpleCursorAdapter implements SectionIndexer {
private final AlphabetIndexer mAlphabetIndexer;
public myCursorAdapter(Context context, int layout,
Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
mAlphabetIndexer = new AlphabetIndexer(c, 1, "අBCDEFGHIJKLMNOPQRSTUVWXYZ");
}
public View newView(Context _context, Cursor _cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(en_word_row, parent, false);
}
#Override
public void bindView(View view, Context context, final Cursor cursor) {
super.bindView(view, context, cursor);
final ImageButton imgFav = (ImageButton) view.findViewById(R.id.img_fav);
final String _id = cursor.getString(cursor.getColumnIndex("_id"));
final Integer fav = Integer
.valueOf(cursor.getString(cursor.getColumnIndex("favourite")));
if (fav == 1)
imgFav.setImageResource(R.drawable.fav_on);
else
imgFav.setImageResource(R.drawable.fav_off);
imgFav.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (dbHelper.isFavourite("enDic", _id)){
imgFav.setImageResource(R.drawable.fav_off);
dbHelper.favUpdate("enDic", _id,0);
}
else{
imgFav.setImageResource(R.drawable.fav_on);
dbHelper.favUpdate("enDic", _id,1);
}
}
});
}
#Override
public Object[] getSections() {
return mAlphabetIndexer.getSections();
}
#Override
public int getPositionForSection(int sectionIndex) {
return mAlphabetIndexer.getPositionForSection(sectionIndex);
}
#Override
public int getSectionForPosition(int position) {
return mAlphabetIndexer.getSectionForPosition(position);
}
}
}
fragment_en.xml (layout of the above fragment)
<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"
tools:context="com.sldroid.mecdic_v21.fragment.EnFragment"
android:orientation="vertical">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/transparent"
android:listSelector="#android:color/transparent"
android:scrollbarStyle="outsideOverlay"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit new word"
android:layout_marginTop="55dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="#+id/btnSubmitEng"/>
</LinearLayout>
en_word_row.xml
<LinearLayout
android:id="#+id/cardView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="3"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_word"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Word"
android:textSize="18dp"
android:textColor="#424242"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_def"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="definition"
android:textSize="10dp"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:textColor="#FF757575"
android:lines="1"/>
</LinearLayout>
<TextView
android:id="#+id/txt_id"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="invisible"/>
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.5">
<ImageButton
android:id="#+id/img_fav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/fav_off"
android:scaleType="centerInside"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
style="#style/Base.Widget.AppCompat.Button.Borderless"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
method of TestAdapter
public Cursor getAllWord(String table)
{
Cursor mCursor = mDb.query(table, new String[] {"_id","word","definition","favourite"},
null, null, null,null, " word COLLATE NOCASE");
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getWordByName(String table, String inputText) throws SQLException {
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = mDb.query(table, new String[] {"_id","word","definition","favourite"},
null, null, null,null, " word COLLATE NOCASE");
}
else {
mCursor = mDb.query(true, table, new String[] {"_id","word","definition","favourite"},
"word" + " like '" + inputText + "%'", null,
null, null, " word COLLATE NOCASE",null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
GetAllWord and getWordByName should be called in the doInBackground method and in the postExecute you shoul update your adapter.
private class GetAllWord extends AsyncTask<Object, Object, Cursor> {
#Override
protected Cursor doInBackground(Object... params) {
return GetAllWord("your text");
}
#Override
protected void onPostExecute(Cursor result) {
if (result != null) {
//update the adapter with the result
}
}
}

Android: SimpleCursorAdapter.ViewBinder, listview, error

I would like to display a two textView in my ListView depending on the database value.
But in result nothing is displayed (error)
This is my code:
public class ListbookActivity extends ListActivity implements OnQueryTextListener{
private DBHelper database;
private Intent intent;
private ListView listav;
private SimpleCursorAdapter adapter;
private String strclickmenu;
private Cursor cursor;
private Toast t;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] from = new String[] { "titolo", "autore" };
int[] to = new int[] { R.id.name, R.id.surname};
database = new DBHelper(getApplicationContext());
SQLiteDatabase db = database.getReadableDatabase();
//query
cursor = db.query("libreria", null, null, null, null, null, "titolo", null);
cursor.moveToFirst();
listav = (ListView) findViewById(R.id.list);
adapter = new SimpleCursorAdapter(this,R.layout.list_row, cursor,from,to,0);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == R.id.name) {
String userFullName = cursor.getString(cursor.getColumnIndex("titolo"));
TextView tit = (TextView)view;
tit.setText(userFullName);
return true;
} else if (view.getId() == R.id.surname) {
String userEmail = cursor.getString(cursor.getColumnIndex("titolo"));
TextView aut = (TextView)view;
aut.setText(userEmail);
return true;
} else {
return false;
}
}
});
listav.setAdapter(adapter);
list_row.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:orientation="horizontal" >
<TextView
android:id="#+id/name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.51"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/surname"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.51"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
activity_listbook.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:orientation="vertical" >
<ListView android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:smoothScrollbar="false"
android:fadingEdge="none"/>
</LinearLayout>
This is the error:
Unable to start activity componentInfo{com.example.mybooks.ListBookActivity} : Java.lang.nullPointerException

Cannot see list of contacts in ListFragment with LoaderManager

I am trying to make a contacts list view fragment. Here is the code:
public class ContactsListFragment extends RoboListFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
private static final int CONTACTS_LIST_LOADER = 0x01;
CursorAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("onCreate", "called");
String[] fromColumns = {ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.ADDRESS};
int[] toViews = { R.id.contact_list_item_id,
R.id.contact_list_item_name,
R.id.contact_list_item_email};
getLoaderManager().initLoader(CONTACTS_LIST_LOADER, null, this);
adapter = new SimpleCursorAdapter(
getActivity().getApplicationContext(), R.layout.contacts_list_item,
null, fromColumns, toViews,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("onCreateView", "called");
View rootView = inflater.inflate(R.layout.contacts_list_fragment, container, false);
return rootView;
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Log.d("onCreateLoader", "called");
return new CursorLoader(getActivity(),
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null, null, null);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
Log.d("onLoadFinished", "called");
adapter.swapCursor(cursor);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
Log.d("onLoaderReset", "called");
adapter.swapCursor(null);
}
}
but although all of the logs are printed, and there are no Exceptions, I see nothing on the screen.
The fragment layout:
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.Fragments.ContactsListFragment">
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/contact_list_empty"/>
</RelativeLayout>
The list item layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/contact_list_item_id"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/contact_list_item_name"
android:layout_below="#id/contact_list_item_id"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/contact_list_item_email"
android:layout_below="#id/contact_list_item_name"/>
</RelativeLayout>
I am at a bit of a loss here. My parent activity includes the fragment thus:
<fragment
android:id="#+id/contact_list_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.example.Fragments.ContactsListFragment"
tools:layout="#layout/contacts_list_fragment"
android:layout_below="#id/fakeView"/>

populate ListFragment with Cursor items without ContentProvider

I'd like to use my own CursorAdapter class to populate a ListFragment. The cursor is returned from my DB API class. I don't use any ContentProvider.
The cursor is loaded properly, and ListFragment.getListAdapter().getCount() returns number of items from the cursor.
There is just no list showing on the UI ...
This is my ListFragment class:
public class ArticlesListFragment extends ListFragmen
public ArticlesListFragment(int userId, ArticleType type, DBAdapter db) {
ArticlesListFragment.userId = userId;
this.type = type;
this.dbAdapter = db;
Log.d(TAG, "Constructor: type=" + type);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
mInflater = LayoutInflater.from(getActivity());
cursor = dbAdapter.getArticles(userId, type);
mAdapter = new ListAdapter(getActivity(), cursor, true);
setListAdapter(mAdapter);
//-----> When I call getListAdapter().getCount() here I get a proper number of articles.
}
}
And this is the ListAdapter::
public class ListAdapter extends CursorAdapter {
private Context mContext;
private final LayoutInflater mInflater;
public ListAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
mInflater = LayoutInflater.from(context);
mContext = context;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title = (TextView) view.findViewById(R.id.article_title);
title.setText(cursor.getString(cursor
.getColumnIndex(Column._TITLE.name)));
TextView text = (TextView) view.findViewById(R.id.article_text);
text.setText(cursor.getString(cursor
.getColumnIndex(Column._TEXT.name)));
TextView date = (TextView) view.findViewById(R.id.article_date);
date.setText(cursor.getString(cursor
.getColumnIndex(Column._CREATED_DATE.name)));
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = mInflater.inflate(R.layout.articles_list_item,
parent, false);
return view;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (!mDataValid) {
throw new IllegalStateException(
"this should only be called when the cursor is valid");
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException(
"couldn't move cursor to position " + position);
}
View v;
if (convertView == null) {
v = newView(mContext, getCursor(), parent);
} else {
v = convertView;
}
bindView(v, mContext, getCursor());
return v;
}
}
Thank you
EDIT:
public class ArticlesFragment extends Fragment implements OnTabChangeListener {
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
dbAdapter = new DBAdapter(activity);
dbAdapter.open();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRoot = inflater.inflate(R.layout.articles_tabs_fragment, null);
return mRoot;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
FragmentManager fm = getFragmentManager();
if (fm.findFragmentByTag(tabId) == null) {
fm.beginTransaction()
.replace(placeholder,
new ArticlesListFragment(userId, ArticleType
.valueOf(tabId), dbAdapter)).commit();
}
}
This is the R.layout.articles_tabs_fragment complete layout file.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" >
<FrameLayout
android:id="#+id/starredArticles"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="#+id/newArticles"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="#+id/readArticles"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</LinearLayout>
This is my list item layout. What I notice while debugging is that CursorAdapter's getView() method is never called..
<LinearLayout
android:id="#+id/first_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp" >
<TextView
android:id="#+id/article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="middle"
android:paddingRight="5dp"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/article_date"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textSize="10dp" />
</LinearLayout>
<TextView
android:id="#+id/article_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/first_row"
android:layout_span="2"
android:textAppearance="?android:attr/textAppearanceSmall" />

Categories

Resources