我正在使用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。但我無法知道如何用代碼更改圖像。任何人都可以爲我提供一個用代碼繪製圖像的示例,並根據運行時的條件對其進行更改。
我想把這個字段添加到數據庫中。 但我無法知道如何用代碼更改圖像。 任何人都可以爲我提供這個問題的例子,如果知道。 – Raj 2010-06-04 06:13:45
這並不難。你需要在代碼中保存圖像視圖(比如'ImageView iv = findViewById(R.id.image_view_id); iv.setImage ...'我寫了...因爲你可以通過設置圖像使用資源,位圖+++和方法名稱是不同的。 – Alxandr 2010-06-04 10:49:59