2016-11-08 53 views
3

我正在使用自定义CursorAdapter并在数据库中有值,但问题是值不会显示在自定义ListView中。我在SO搜索,但无法找到我的答案。我的自定义CursorAdapter不显示项目

通过调试,我发现游标适配器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)); 
    } 

} 

EDIT 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

的问题是你有没有设置LinearLayout的方向在主布局,因此它DEFA超过horizontal

这意味着ListView毗邻Toolbar放置,但Toolbar的宽度被设定为match_parent,所以没有空间留给ListView

添加以下的属性到LinearLayoutactivity_main.xml中,所以ListView将放在以下Toolbar

android:orientation="vertical" 

此外,下面的通话可能需要从bindView()删除:

setListViewHeight(ll,context); 

ListView的高度已经在XML中正确设置,这个调用可能会搞砸了(我可以在onl y假设,因为你没有公布实施setListViewHeight())。

0

检查您Cursor是空的......如果你是确保它不是空的,然后打补丁调用的最后一行下面的代码中出Activity - >onCreate() ...

dataCursor.notifyDataSetChanged(); 
相关问题