Weird Nerd Weird Nerd - 1 month ago 10
Android Question

My custom CursorAdapter does not display the items

I am using a custom

CursorAdapter
and have values in DB but the problem is that the values are not displaying in the custom
ListView
. I searched in SO but could not find my answer.

By debugging I found out that the 2 methods in cursor adapter
bindView()
and the
newView()
are not executing but the constructor is executing.I am not sure what is happening overall. So my question is why are the
ListView
items not getting displayed ?

Here is my code and I am only posting relevant code so if there is any additional code needed please comment so that I will edit accordingly.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//a listview object
notesView = (ListView)findViewById(R.id.listView);
//an object of SQLiteOpenHelper class
dbhelper = new DataBaseHelper(this);

//cursor object
passCursor = dbhelper.fetchAllNotes();
// the custom cursor adapter class object
dataCursor = new CustomCursor(this,passCursor);
notesView.setAdapter(dataCursor);
notesView.setOnItemClickListener(this);
bar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(bar);
}


Here is the
CursorAdapter
source code:

public class CustomCursor extends CursorAdapter {
private static final String NOTE_TITLE = "title";
private static final String RECORD_ID = "_id";
private static final String RECORD_DATE = "date";
private static final String DELETE_FLAG="deleteflag";
LayoutInflater inflater;
TextView tv, recordID, dateET;
LinearLayout ll;
String getText, existsRecordID;
long datevalue;
SimpleDateFormat dateFormatter;
String listViewHeight;
Context cont;
int getDeleteFlag;
String listHeightValue;
LinearLayout.LayoutParams params;
Cursor getCursor;

CustomCursor(Context context, Cursor c) {
super(context, c, 0);
cont= context;
getCursor= c;
//inflater = LayoutInflater.from(context);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
dateFormatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");
}


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.customlistview, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
getDeleteFlag = cursor.getInt(cursor.getColumnIndex(DELETE_FLAG));
ll = (LinearLayout)view.findViewById(R.id.listViewLayout);
setListViewHeight(ll,context);
getText = cursor.getString(cursor.getColumnIndex(NOTE_TITLE));
existsRecordID = cursor.getString(cursor.getColumnIndex(RECORD_ID));
datevalue = cursor.getLong(cursor.getColumnIndex(RECORD_DATE));
Date newdate = new Date(datevalue);
recordID = (TextView) view.findViewById(R.id.recordID);
tv = (TextView) view.findViewById(R.id.content);
dateET = (TextView) view.findViewById(R.id.date);
tv.setText(getText.trim());
recordID.setText(existsRecordID);
dateET.setText(dateFormatter.format(newdate));
}

}


EDIT 1

Here is the main layout,

<?xml version="1.0" encoding="utf-8"?>


<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragplacement"
xmlns:android="http://schemas.android.com/apk/res/android">


<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:gravity="top|left"
android:descendantFocusability="blocksDescendants"
tools:context="com.random.simplenotes.MainActivity">




<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"



android:id="@+id/toolbar"
>




</android.support.v7.widget.Toolbar>


<ListView
android:layout_width="match_parent"
android:clickable="true"
android:layout_height="match_parent"
android:id="@+id/listView"

android:scrollbars="vertical"
/>
</LinearLayout>

</FrameLayout>


Here is the layout for the listView item,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:orientation="vertical"

android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:background="@color/PeachPuff"

android:id="@+id/listViewLayout"
android:layout_margin="7dp">



<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Here is the content.."
android:gravity="center"
android:id="@+id/content"
android:textColor="@color/black"
android:focusable="false"
android:ellipsize="end"
android:lines="1"
android:maxLines="1"
android:layout_gravity="center_horizontal" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="12/09/15"
android:layout_margin="10dp"
android:focusable="false"
android:id="@+id/date" />

<TextView
android:layout_width="1dp"
android:layout_height="1dp"
android:visibility="gone"
android:focusable="false"
android:id="@+id/recordID" />


</LinearLayout>


Code for the method
setListViewHeight()


private void setListViewHeight(LinearLayout ll, Context con) {

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(con);

listHeightValue = sp.getString(con.getResources().getString(R.string.listViewHeightkey),Constants.DEFAULT_VALUE_LISTVIEW_HEIGHT);


switch (listHeightValue)
{

case "Tiny":

params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,200);
ll.setLayoutParams(params);

break;

case "Medium":
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,220);
ll.setLayoutParams(params);

break;

case "Large":
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,250);
ll.setLayoutParams(params);

break;
default:
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,150);
ll.setLayoutParams(params);

}




}

Answer

The problem is your haven't set the orientation of the LinearLayout in your main layout, so it defaults to horizontal.

This means the ListView is placed next to the Toolbar, but the width of the Toolbar is set to match_parent, so there is no room left for the ListView.

Add the following attribute to the LinearLayout in activity_main.xml, so the ListView will be placed below the Toolbar (as it should be):

android:orientation="vertical"

Also, the following call might need to be removed from bindView():

setListViewHeight(ll,context);

The height of the ListView is already set properly in XML, this call might mess it up (i can only assume, since you haven't posted the implementation of setListViewHeight()).