gpt4 book ai didi

java - 在ListView/ArrayAdapter中更新图像

转载 作者:行者123 更新时间:2023-12-01 16:18:59 24 4
gpt4 key购买 nike

想知道是否有人可以帮助我-我有一个自定义ArrayAdapter,它显示对象列表(在本例中为“ Assets”:List<Asset>)。我希望能够以编程方式(没有onClick事件)更新ListView中使用此适配器的项目。特别;我有一个在后台进行的蓝牙扫描,我想在ListView行中更新通过蓝牙查看的资产的图像。请有人解释我该怎么做?

这是我在适配器中的行布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="60dip"
android:layout_margin="5dip"
android:src="@drawable/logo" // <--- I want to change this image src when the asset was seen
android:adjustViewBounds="true"
android:id="@+id/icon"/>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="6dip"
android:orientation="vertical">
<TextView android:id="@+id/text1"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/text_size_normal"
android:textColor="#000000"
android:text="Test name"
/>

<TextView android:id="@+id/text2"
android:textSize="@dimen/text_size_small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#333333"
android:text="Test address"
/>
<TextView android:id="@+id/text3"
android:textSize="@dimen/text_size_small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#333333"
android:text="Temperature: 12. Battery: 14"
/>

</LinearLayout>

</LinearLayout>


和我的自定义适配器:

    adapter = new ArrayAdapter(getBaseContext(), R.layout.three_line_list_item_with_icon, R.id.text1, assets) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text1 = (TextView)view.findViewById(R.id.text1);
TextView text2 = (TextView)view.findViewById(R.id.text2);
TextView text3 = (TextView)view.findViewById(R.id.text3);

ImageView iv = (ImageView)view.findViewById(R.id.icon);
iv.setImageResource(R.drawable.smartie_logo);

Asset thisAsset = assets.get(position);
text1.setText(thisAsset.name);
text2.setText("Unseen");
text3.setText("");

if (position % 2 == 0) {
view.setBackgroundResource(R.drawable.table_dark);
} else {
view.setBackgroundResource(R.drawable.table_light);
}
return view;
}
};


然后,当看到资产时,我将运行一个函数:

void UpdateAssetRow(Asset asset) {
// Do something here to update the image in the specific row containing this asset
adapter.notifyDataSetChanged();
}

最佳答案

请忽略此答案,因为我无法删除它!

关于java - 在ListView/ArrayAdapter中更新图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62320215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com