2010-06-03 41 views
0

我正在使用LinearLayout來顯示一些文本和圖像。我有可繪製/的圖像,我用ListActivity和一些onListItemClick功能來實現這一點。現在我想更改由onclick功能處理的行的圖像,以顯示處理後的狀態。有人可以幫助我解決這個問題,在運行時改變圖像。在運行時在ListView上更改圖像android

以下是我的實踐。

public class ListWithImage extends ListActivity {0}當調用第一次創建時調用。 */

private SimpleCursorAdapter myAdapter;

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

    Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null); 
    startManagingCursor(cursor); 

    String[] columns = new String[] {People.NAME, People.NUMBER}; 
    int[] names = new int[] {R.id.contact_name, R.id.contact_number}; 

    myAdapter = new SimpleCursorAdapter(this, R.layout.main, cursor, columns, names); 
    setListAdapter(myAdapter); 

} 


@Override 
protected void onListItemClick(ListView listView, View view, int position, long id) { 
    super.onListItemClick(listView, view, position, id); 

    Intent intent = new Intent(Intent.ACTION_CALL); 
    Cursor cursor = (Cursor) myAdapter.getItem(position); 
    long phoneId = cursor.getLong(cursor.getColumnIndex(People.PRIMARY_PHONE_ID)); 
    intent.setData(ContentUris.withAppendedId(Phones.CONTENT_URI, phoneId)); 

    startActivity(intent); 
} 

}

和main.xml中是:

<LinearLayout 
    android:layout_height="wrap_content" android:orientation="vertical" android:layout_width="250px"> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:orientation="horizontal"> 

    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Name: " /> 

    <TextView android:id="@+id/contact_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
</LinearLayout> 
    <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:orientation="horizontal">   
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Phone: " />  
    <TextView android:id="@+id/contact_number" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
</LinearLayout> 


我認爲添加字段爲DB。但我無法知道如何用代碼更改圖像。任何人都可以爲我提供一個用代碼繪製圖像的示例,並根據運行時的條件對其進行更改。

回答

0

我在我的應用程序中做了同樣的事情(但是我沒有使用cursoradapter,而是使用自定義適配器,但是邏輯應該是相同的)。

我需要做的工作是在數據庫中包含一個字段,指定是否處理該字段(或某些字段決定要顯示哪個圖像)。然後,您需要將您的圖片地址綁定到該字段。我不知道你是否可以用simplecursoradapter來做到這一點,或者如果你需要實現你自己的。

然後,當用戶單擊某個項目時,您只需將該項目設置爲在db中處理,並告訴適配器它已被更新。

+0

我想把這個字段添加到數據庫中。 但我無法知道如何用代碼更改圖像。 任何人都可以爲我提供這個問題的例子,如果知道。 – Raj 2010-06-04 06:13:45

+1

這並不難。你需要在代碼中保存圖像視圖(比如'ImageView iv = findViewById(R.id.image_view_id); iv.setImage ...'我寫了...因爲你可以通過設置圖像使用資源,位圖+++和方法名稱是不同的。 – Alxandr 2010-06-04 10:49:59