我的自定义 CursorAdapter 不显示项目

My custom CursorAdapter does not display the items

我正在使用自定义 CursorAdapter 并且在数据库中有值,但问题是这些值没有显示在自定义 ListView 中。我在 SO 中搜索但找不到我的答案。

通过调试,我发现游标适配器 bindView()newView() 中的两个方法没有执行,但是构造函数 executing.I 我不确定总体上发生了什么。所以我的问题是为什么 ListView 项没有显示?

这是我的代码,我只发布相关代码,所以如果需要任何其他代码,请发表评论,以便我进行相应的编辑。

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

这里是 CursorAdapter 源代码:

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

}

编辑 1

这是主要布局,

<?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>

这是 listView 项目的布局,

<?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>

方法代码 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);

        }




    }

检查您的 Cursor 是否为空...如果您确定它不为空,则在 out Activity -> [=13 的最后一行中调用以下代码进行修补=] ...

dataCursor.notifyDataSetChanged();

问题是您没有在主布局中设置 LinearLayout 的方向,因此它默认为 horizontal

这意味着ListView放在Toolbar旁边,但是Toolbar的宽度设置为match_parent,所以没有空间了ListView.

将以下属性添加到 activity_main.xml 中的 LinearLayout,因此 ListView 将位于 Toolbar 下方:

android:orientation="vertical"

此外,可能需要从 bindView() 中删除以下调用:

setListViewHeight(ll,context);

ListView 的高度已经在 XML 中正确设置,这个调用可能会搞砸(我只能假设,因为你还没有发布 [=24= 的实现]).