在android中运行时更改listview上的图像

时间:2010-06-03 09:12:05

标签: android android-widget

我正在使用LinearLayout来显示一些文本和图像。我有drawable /的图像,我用ListActivity和一些onListItemClick功能来实现这一点。现在我想更改由onclick功能处理的行的图像,以显示处理状态。可以帮助我在这个问题上帮助我在运行时更改图像。

以下是我的意见。

public class ListWithImage扩展ListActivity {     /** 在第一次创建活动时调用。 * /

私人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。但我无法知道如何用代码更改图像。任何人都可以为我提供一个用代码绘制图像的例子,并根据运行时的条件进行更改。

1 个答案:

答案 0 :(得分:0)

我在我的应用程序中做了同样的事情(但是我没有使用cursoradapter而是使用自定义适配器,但逻辑应该是相同的。)

我需要做的是在db中包含一个字段,该字段指定字段是否处理(或某个字段决定显示哪个图像)。然后,您需要将图像地址绑定到该字段。我不知道你是否可以使用simplecursoradapter,或者你需要实现自己的。

然后,当用户单击某个项目时,您只需将该项目设置为在数据库中处理,并告诉您的适配器它已更新。

相关问题