简介:
我在突出显示 Android 应用程序中
ListView
项目的背景颜色时遇到问题。尽管我尝试以编程方式修改背景颜色,但这些更改并未反映在 UI 中。
问题描述:
我试图根据某些条件动态更改
ListView
项目的背景颜色,例如匹配特定的歌曲名称。但是,即使我的代码执行时没有错误并且日志表明匹配逻辑正常运行,但 UI 并未反映更改。背景颜色保持不变。
ListView
和Adapter
的初始化:
ListView songsListView = findViewById(R.id.songsListView);
SongsAdapter songsAdapter = new SongsAdapter(context, songNames);
songsListView.setAdapter(songsAdapter);
尝试更改循环内的背景颜色:
// Find and highlight the song using the adapter
for (int i = 0; i < songsAdapter.getCount(); i++) {
String tempSongName = songsAdapter.getItem(i);
// Log information about the song
Log.d("SttList", "(SetListMasterViewActivity) line #222 - ChildViewInfo - Child view index: " + i);
Log.d("SttList", "(SetListMasterViewActivity) line #223 - ChildViewInfo - tempSongName: " + tempSongName + ", lastViewedSongName: " + (i + 1) + ") " + lastViewedSongName);
// Check if the song name matches the last viewed song name
if (tempSongName != null && tempSongName.equalsIgnoreCase((i + 1) + ") " + lastViewedSongName)) {
// Highlight the corresponding ListView item
View childView = songsAdapter.getView(i, null, songsListView);
if (childView instanceof TextView) {
TextView textView = (TextView) childView;
textView.setBackgroundColor(Color.YELLOW);
// Log the match
Log.d("SttList", "(SetListMasterViewActivity) line #226 - ChildViewInfo - Match found for song: " + lastViewedSongName);
// Exit the loop once the match is found
break;
}
}
}
xml:
<!-- list_item_song.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Song Number -->
<TextView
android:id="@+id/itemNumberTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginEnd="8dp"/>
<!-- Song Name -->
<TextView
android:id="@+id/songNameTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp" />
<!-- Divider -->
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#888888" />
</LinearLayout>
在我的
adapter
班上 - SongListAdapter
:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_song, parent, false);
}
// Get the item for this position
String songName = getItem(position);
// Find views in the layout
TextView itemNumberTextView = convertView.findViewById(R.id.itemNumberTextView);
TextView songNameTextView = convertView.findViewById(R.id.songNameTextView);
// Set the song number
itemNumberTextView.setText((position + 1) + ")");
// Set the song name
songNameTextView.setText(songName);
// Set background color
int backgroundColor = mBackgroundColors.get(position, 0); // Default is transparent
convertView.setBackgroundColor(backgroundColor);
return convertView;
}
我已经尝试过:
尝试从
Adapter
类更改背景颜色:
public void updateItemBackground(int position, int color) {
mBackgroundColors.put(position, color);
notifyDataSetChanged();
}
在类级别定义适配器:
private SongListAdapter adapter;`
并填充在这里:
adapter = new SongListAdapter(this, songNames);
并用于在我的循环中调用此处的
updateItemBackground
方法:
if (childView instanceof TextView) {
adapter.updateItemBackground(i, Color.YELLOW);
// Log the match
Log.d("SttList", "(SetListMasterViewActivity) line #226 - ChildViewInfo - Match found for song: " + lastViewedSongName);
// Exit the loop once the match is found
break;
}
但是我遇到了
NullPointerException
错误 - 我可以通过 @Upendra Shah's
评论修复该错误,但仍然无法突出显示 ListView
中的项目。
感谢您的阅读!
我有这个类级别变量:
private List<Songs> songsList;
但我还有另一个
List
变量,并且混合使用来访问我的 List
。我删除了多余的对象并重写了代码以仅使用 songsList
对象。通过这样做,我能够隔离我的对象,而不会与执行相同功能的多个对象混淆。
另外,使用这个到我的父类来调用
Adapter
类(SongListAdapter
),使用这个变量:
private SongListAdapter songsAdapter;
并进入我的
Adapter
班:
songsAdapter = new SongListAdapter(this, new ArrayList<>());
songsListView.setAdapter(songsAdapter);
我能够在我的
onActivityResult
(父类)中调用此方法:
public void updateItemBackground(int position, int color) {
// Reset all background colors to transparent
mBackgroundColors.clear();
notifyDataSetChanged();
// Log the reset action
Log.d("SttList", "(UpdateBackground) Resetting background colors");
int adjustmentFactor = 50;
int lighterColor = Color.rgb(
Math.min(Color.red(Color.DKGRAY) + adjustmentFactor, 255),
Math.min(Color.green(Color.DKGRAY) + adjustmentFactor, 255),
Math.min(Color.blue(Color.DKGRAY) + adjustmentFactor, 255)
);
// Set the background color of the specific item
mBackgroundColors.put(position, lighterColor);
notifyDataSetChanged();
}
这些操作能够突出显示适当数组索引处的
List
。