2010-06-03 55 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