2014-06-16 46 views
0

兜兜在這裏 - 道歉,如果下面的代碼看起來很亂,但我不斷改變它下面從這裏沒有什麼工作舊/新意見...問題有一個ListView和一個自定義的CursorAdapter

只是想使用遊標適配器從數據庫填充listView,該遊標必須是自定義的,因爲我想訪問每行中的各個字段。

充其量,我有listView閱讀和正確顯示數據,但onClick不工作(根本沒有響應,沒有logcat錯誤)。我已經閱讀了許多線索並嘗試過各種嘗試,但無法使其工作,更糟糕的是,現在我無法回到第一個半工作版本。

所以,我必須是這樣的 - 主要活動:

public OnItemClickListener mTripClickHandler; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Create/prepare database (code removed for clarity) 

    //Display trips - first get any entries  
    tripCur = mpgDB.query(dbHistory.TABLE_NAME, TRIP_PROJ, whereClause, null, null, null, null); 
    int tmpCnt = tripCur.getCount(); 

    if (tmpCnt > 0) { 
     tripCur.moveToFirst(); 

     // build a listView adapter 
     Log.i(LOGTAG,"About to create listView..."); 
     //tripList = (ListView) findViewById(android.R.id.list); 
     tripList = getListView(); 
     Log.i(LOGTAG,"ListView created..."); 

     Log.i(LOGTAG,"About to create adapter..."); 
     tripAdapter = new tripListAdapter(this, tripCur); 
     setListAdapter(tripAdapter); 
     setContentView(R.layout.activity_trip_display); 

     //tripList.setOnItemClickListener(mTripClickHandler); 

     tripList.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
       // Do something in response to the click 
       // Enable other buttons 
       togBtns(true); 
} 
     }); 

定義適配器:

// custom list adaptor class 
private class tripListAdapter extends CursorAdapter { 

    //private final Cursor dataC; 
    private final LayoutInflater vi; 
    public View theView; 

    public tripListAdapter(Context con, Cursor c) { 
     // super constructor thingy 
     super(con, c); 
     //dataC = c; 
     vi = LayoutInflater.from(con); 

    } 

    @Override 
    public View getView (int position, View v, ViewGroup parent){ 

     Log.i(LOGTAG,"In getView..."); 
     // Create a message handling object as an anonymous class. 
     //mTripClickHandler = new OnItemClickListener() { 

     Log.i(LOGTAG,"Leaving getView..."); 
     return v; 
    } 

    @Override 
    public void bindView(View v, Context arg1, Cursor dataC) { 
     Log.i(LOGTAG,"In bindView..."); 
     // get data from cursor 
     TextView dateTV = (TextView) v.findViewById(R.id.dateFld); 
     TextView distTV = (TextView) v.findViewById(R.id.distFld); 
     TextView mpgTV = (TextView) v.findViewById(R.id.mpgFld); 

     String tmpMPG = dataC.getString(dataC.getColumnIndexOrThrow(dbHistory.TRIP_MPG)); 
     Double tmpNum = Double.valueOf(tmpMPG); 
     tmpMPG = String.format("%.2f", tmpNum); 

     dateTV.setText(dataC.getString(dataC.getColumnIndexOrThrow(dbHistory.TRIP_DATE))); 
     distTV.setText(dataC.getString(dataC.getColumnIndexOrThrow(dbHistory.TRIP_MILES))); 
     mpgTV.setText(tmpMPG);      

     Log.i(LOGTAG,"Leaving bindView..."); 
    } 

    @Override 
    public View newView(Context arg0, Cursor arg1, ViewGroup arg2) { 
     // TODO Auto-generated method stub 
     Log.i(LOGTAG,"In newView..."); 
     theView = vi.inflate(R.layout.trip_row, arg2, false); 

     Log.i(LOGTAG,"Leaving newView..."); 
     return theView; 
    } 

...和logcat的(上幾行唯一的 - 所有線路涉及到系統的代碼,不是我的):

06-16 09:49:38.110: I/WIBBLE(1184): About to create listView... 
06-16 09:49:38.150: I/WIBBLE(1184): ListView created... 
06-16 09:49:38.150: I/WIBBLE(1184): About to create adapter... 
06-16 09:49:38.240: I/WIBBLE(1184): OnResume with this many rows returned by cursor: 3 
06-16 09:49:38.240: I/WIBBLE(1184): In the loop, so cursor is not null.... 
06-16 09:49:38.320: I/WIBBLE(1184): In getView... 
06-16 09:49:38.320: I/WIBBLE(1184): Leaving getView... 
06-16 09:49:38.330: D/AndroidRuntime(1184): Shutting down VM 
06-16 09:49:38.330: W/dalvikvm(1184): threadid=1: thread exiting with uncaught exception (group=0x414c4700) 
06-16 09:49:38.452: E/AndroidRuntime(1184): FATAL EXCEPTION: main 
06-16 09:49:38.452: E/AndroidRuntime(1184): java.lang.NullPointerException 
06-16 09:49:38.452: E/AndroidRuntime(1184):  at android.widget.AbsListView.obtainView(AbsListView.java:2179) 
06-16 09:49:38.452: E/AndroidRuntime(1184):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1247) 
06-16 09:49:38.452: E/AndroidRuntime(1184):  at a ndroid.widget.ListView.onMeasure(ListView.java:1159) 
06-16 09:49:38.452: E/AndroidRuntime(1184):  at  android.view.View.measure(View.java:15848) 
06-16 09:49:38.452: E/AndroidRuntime(1184):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008) 
06-16 09:49:38.452: E/AndroidRuntime(1184):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 

任何想法?

回答

0

你還沒有從getView()方法返回視圖。當前你返回空值。這就是爲什麼你得到空指針異常在getView()內調用newview並返回適當的視圖。

下面閱讀更多相關資訊太問題

What bindView() and newView() do in CursorAdapter

+0

Doh !! - 非常感謝你,雖然我現在回到了半工作版本 - 但至少沒有崩潰。再次感謝... – Nelmo

0

public View getView(int position,View v,ViewGroup parent)不調用super.getView(position,v,parent),它是創建並綁定View實例的人。正如你可以在CursorAdapter類中看到的:

/** 
* @see android.widget.ListAdapter#getView(int, View, ViewGroup) 
*/ 
public View getView(int position, View convertView, ViewGroup parent) { 
    if (!mDataValid) { 
     throw new IllegalStateException("this should only be called when the cursor is valid"); 
    } 
    if (!mCursor.moveToPosition(position)) { 
     throw new IllegalStateException("couldn't move cursor to position " + position); 
    } 
    View v; 
    if (convertView == null) { 
     v = newView(mContext, mCursor, parent); 
    } else { 
     v = convertView; 
    } 
    bindView(v, mContext, mCursor); 
    return v; 
} 
0

我在那裏的項目點擊不工作,在列表視圖中使用自定義適配器時,同樣的問題。我發現這裏的解決方案:http://syedasaraahmed.wordpress.com/2012/10/03/android-onitemclicklistener-not-responding-clickable-rowitem-of-custom-listview/

基本上,你該補充:

android:descendantFocusability="blocksDescendants" 

單個項目到您的自定義佈局的父視圖列表(R.layout.trip_row),因爲孩子的意見阻止了這一事件。

相關問題