You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于SQLite数据切换ListView中按钮背景样式的实现需求

Solution: Toggle Button Background Based on SQLite's number Field

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() to SongList: Make sure your SongList model class has a method like public int getNumber() that returns the number field 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 _url since 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 18:47:28