【问题标题】:changing image on listview at runtime in android在android中运行时更改listview上的图像
【发布时间】:2010-06-03 09:12:04
【问题描述】:

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

以下是我的暗示。

公共类 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>  


我想将该字段添加到数据库。但我不知道如何用代码更改图像。任何人都可以为我提供一个使用代码绘制图像并在运行时根据条件更改它的示例。

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

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

    我需要做的是在数据库中包含一个字段,该字段指定是否处理该字段(或某些字段决定显示哪个图像)。然后您需要将您的图像地址绑定到该字段。我不知道您是否可以使用简单的光标适配器来做到这一点,或者您是否需要实现自己的。

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

    【讨论】:

    • 我想将该字段添加到数据库。但我不知道如何用代码更改图像。如果知道,任何人都可以为我提供这个问题的示例。
    • 这并不难。您需要在代码中获取图像视图(例如ImageView iv = findViewById(R.id.image_view_id); iv.setImage...。我已经写过......因为您可以使用资源、位图+++ 来设置图像,并且方法名称不同。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    相关资源
    最近更新 更多