2012-02-05 50 views
0

在列表視圖改變圖像沒有運氣,我再次來到這裏相當多的谷歌搜索後,打擾你所有的:)的Android的Java:根據內容

我的ListView,在我的應用程序,通過從光標SQLitle分貝填充。 。看起來是這樣的:

public void updateShiftList(String x) { 
    db.openToRead(); 

    listShifts = (ListView) findViewById(R.id.listShifts); 

    // get data 

    cursor = db.getPlan(x); 
    startManagingCursor(cursor); 
    // set adapter 
    String[] from = { dataManager.KEY_SHIFT, dataManager.KEY_DATE }; 
    int[] to = { R.id.rowShift, R.id.rowDate }; 

    adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to); 

    listShifts.setAdapter(adapter); 
    db.close(); 

} 

在row.xml我有圖像的每一行之前,我想實現的功能或創建該ListView控件的,將取決於一些參數,我將測試改變圖像的過程中檢查方法,例如R.id.rowDate == TodayDate(在該文本字段中有日期)或者如果R.id.rowDate是從今天開始的下一個日期......

我該怎麼做,或者在哪裏...有沒有可以使用的一些Override?我對這個簡單的項目學習java爲我做這麼溫柔:)

感謝所有, 弗拉德

回答

0

下面是從我的代碼的摘錄。我在我的很多項目中使用它,從數據庫中收集數據並將它們顯示在ListView中。有用。您需要將其稍微調整爲適合您的項目。無論如何,我會建議重寫你的代碼。 圖片的東西是在底部:

package my.package; 

import android.app.Activity; 


public class MyActivity extends Activity { 


private DBAdapter mDb; 

private ListView mListView; 
private Cursor mCursor; 



@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.groups_list);  

    mDb = new DBAdapter(this); 
    mDb.open();  

    populateListView(); 
}  

@Override 
public void onDestroy() { 
    super.onDestroy(); 

    if(mCursor != null) 
     mCursor.close(); 
    mCursor = null; 

    if(mDb != null) 
     mDb.close();   
    mDb = null;  
}  


private void populateListView(){   

    if(mCursor!=null) //close the old one first 
     mCursor.close(); 
    mCursor = mDb.getSomeDataCursor();     

    MainAdapter adapter = new MainAdapter(this, R.layout.groups_list_item, mCursor, false);   
    mListView.setAdapter(adapter); 
}  


private class MainAdapter extends ResourceCursorAdapter{ 

    private int mLayout; 
    private int mColumnIndexGroup; 

    public MainAdapter(Context pContext, int pLayout, Cursor pCursor, boolean pAutoRequery) { 
     super(pContext, pLayout, pCursor, pAutoRequery); 

     mLayout = pLayout; 
     mColumnIndexGroup = (pCursor.getColumnIndex(DBAdapter.COL_NAME)); 
    }   

    @Override 
    public View newView(Context pContext, Cursor pCursor, ViewGroup pParent) { 
     LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     return li.inflate(mLayout, pParent, false); 
    }   

    @Override 
    public void bindView(View pView, Context pContext, Cursor pCursor) {   
     TextView tvGroup = (TextView)pView.findViewById(R.id.groupsListTVGroup); 
     tvGroup.setText(pCursor.getString(mColumnIndexGroup)); 

     //Image stuff - an example 
     if(pCursor.getInt(pCursor.getColumnIndex(SOMETHING)) == SOME_VAL){ 
      ImageView img = (ImageView) pView.findViewById(R.id.myImage); 
      img.setImageResource(R.drawable.myImage) 
     } 
    }  

} 

}

+0

謝謝,我現在看到。我不知道你可以製作你自己的適配器。 – VladoPortos 2012-02-05 10:38:21

+0

不客氣。祝你好運。 – Yar 2012-02-05 10:40:32

+0

我設法使它工作!還有一個問題,那就是'newView'覆蓋了什麼?在很多例子中,我已經看到幾乎與'bindView'中的代碼相同,我甚至試圖在沒有它的情況下使用它,它的工作原理...不知道它是什麼。我會很感激簡單的解釋,謝謝。 – VladoPortos 2012-02-05 18:03:14