我的自定义CursorAdapter不显示项目

时间:2016-11-08 11:17:23

标签: android listview android-cursoradapter

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

通过调试我发现游标适配器bindView()newView()中的2个方法没有执行但构造函数正在执行。我不确定整体上发生了什么。所以我的问题是为什么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);

        }




    }

2 个答案:

答案 0 :(得分:2)

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

这意味着ListView位于Toolbar旁边,但Toolbar的宽度设置为match_parent,因此{ {1}}。

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

Toolbar

此外,可能需要从android:orientation="vertical" 删除以下调用:

bindView()

setListViewHeight(ll,context); 的高度已在XML中正确设置,此调用可能会搞砸(我只能假设,因为您尚未发布ListView的实现)。

答案 1 :(得分:0)

检查您的Cursor是否为空...如果您确定它不为空,那么要修补,请在Activity的最后一行调用以下代码 - &gt; onCreate() ...

dataCursor.notifyDataSetChanged();
相关问题