基于SQLite数据切换ListView中按钮背景样式的实现需求
Hey there! Let's get this button background switching working for your ListView. Here are the key changes you need to make to your ListAdapter code:
1. Update Your ViewHolder Class
First, let's optimize your ViewHolder to hold all UI elements—this avoids repeated findViewById() calls and fixes potential list item recycling issues:
public class ViewHolder { RelativeLayout relativeLayout; TextView songUrl; TextView songName; TextView songTime; ImageView songThumbnail; Button favoriteBtn; }
2. Modify the getView() Method
Next, adjust the getView() method to fetch the number value from your data and set the button background accordingly. I've added comments to highlight critical changes:
@Override public View getView(final int position, View convertView, final ViewGroup parent) { ViewHolder viewHolder; if (convertView == null) { viewHolder = new ViewHolder(); convertView = layoutInflater.inflate(R.layout.list_item, parent, false); // Initialize all UI elements via ViewHolder viewHolder.relativeLayout = convertView.findViewById(R.id.listview_item); viewHolder.songUrl = convertView.findViewById(R.id.song_url); viewHolder.songName = convertView.findViewById(R.id.song_name); viewHolder.songTime = convertView.findViewById(R.id.song_time); viewHolder.songThumbnail = convertView.findViewById(R.id.song_thumbnail); viewHolder.favoriteBtn = convertView.findViewById(R.id.favorite); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } // Get the current SongList item from your dataset SongList currentItem = mList.get(position); // Populate existing UI elements as before Bitmap bitmap = BitmapFactory.decodeByteArray(currentItem.get_img(), 0, currentItem.get_img().length); viewHolder.songThumbnail.setImageBitmap(bitmap); viewHolder.songUrl.setText(currentItem.get_url()); viewHolder.songName.setText(currentItem.get_name()); viewHolder.songTime.setText(currentItem.get_time()); // --- Core Logic for Button Background --- // Fetch the number value (make sure SongList has a getNumber() method!) int numberValue = currentItem.getNumber(); // Replace with your actual getter method // Set background based on number value if (numberValue == 0) { viewHolder.favoriteBtn.setBackgroundResource(R.drawable.icon1); } else if (numberValue == 1) { viewHolder.favoriteBtn.setBackgroundResource(R.drawable.icon2); } // Set click listener for the button viewHolder.favoriteBtn.setTag(position); viewHolder.favoriteBtn.setOnClickListener(this); return convertView; }
3. Critical Checks
- Add
getNumber()toSongList: Make sure yourSongListmodel class has a method likepublic int getNumber()that returns thenumberfield value from your SQLite row. - Avoid recycling bugs: Always set the button background directly in
getView()—this prevents old background images from sticking to reused list items. - Clean up unused variables: I removed standalone variables like
_urlsince we're using the ViewHolder now, which makes the code more efficient and readable.
That's all! With these changes, your button's background will automatically switch based on the number value from each SQLite row.
内容的提问来源于stack exchange,提问作者StarCue
相关产品推荐
相关产品推荐

