2016-11-08 40 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(); 
相關問題