listView项目行背景自动改变

listView item row background change automatically

我必须设置 visitedItem(listView 行的图像视图)的背景颜色。

它显示了一个项目列表,如果您访问 (itemclick) 行,则必须更改其项目背景颜色。我为此使用 sharedPreferences。并且还对其进行调试(在调试中它显示为 false)。但是不知道为什么第一次在任何 listView 行上单击 itemClick 后第一个项目设置为绿色。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View view = convertView;

        if(view == null)
        {
            view = inflater.inflate(R.layout.item_ads, parent, false);
        }

        final HashMap<String, String> map = list.get(position);

        ImageView imageAd = (ImageView)view.findViewById(R.id.ad_image);

        if(sessionManager.ItemVisited(position))// && position!=0)// && !sessionManager.getFirstRun())
        {
            imageAd.setBackgroundColor(Color.GREEN);
        }

}

会话管理器

public class SessionManager 
{    
    public static ArrayList<Boolean> listBoolTrain = new ArrayList<Boolean>();

    private int giftRemaining;

    private SharedPreferences prefs;

    // Editor for Shared preferences
    Editor editor;

    // Context
    Context _context;

    // Shared pref mode
    int PRIVATE_MODE = 0;


    // Sharedpref file name
    private static final String PREF_NAME = "AndroidHivePref";

    public SessionManager(Context context)
    {   
         this._context = context;

         prefs = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);

         editor = prefs.edit();
    }

    public void setNumberOfGits(int numberOfGifts) 
    {
        editor.putInt("numberOfGifts", numberOfGifts);

        editor.commit();
    }

    public int getNumberOfGits() 
    {
        int nog = prefs.getInt("numberOfGifts", -5);

        return nog;
    }

    public void initializerBooleans(int arraySiz)
    {
        int arraySize = prefs.getInt("arraySize", 10);

        for(int x = 0 ; x < arraySize; x++)
        {
            editor.putBoolean("Bool"+x, false);

            editor.commit();
        }
    }

    public void setItemVisited(int x)
    {
        editor.putBoolean("Bool"+x, true);  

        editor.commit();
    }

    public boolean isItemVisited(int x)
    {
        return prefs.getBoolean("Bool"+x, false);   
    }

    public int getUnVisitedItemCount()
    {
        int count = 0;

        int arraySize = prefs.getInt("arraySize", 10);

        for(int x = 0 ; x < arraySize ; x++)// listBoolTrain.size(); x++)
        {
            boolean bol= prefs.getBoolean("Bool"+x, false);

            if(!bol)
            {
                count++;
            }
        }

        return count;
    }

    public void remainingGift()
    {
    }

    public void setFirstRun(boolean status)
    {   
        editor.putBoolean("firstrun", status);

        editor.commit();
    }

    public boolean getFirstRun()
    {
        return prefs.getBoolean("firstrun", true);
    }

    public void removeAllPreferences()
    {
        prefs.edit().clear().commit();
    }

    public void removeKey(String keyName)
    {
        prefs.edit().remove(keyName).commit();
    }

    public void showAll()
    {
        Map<String,?> keys = prefs.getAll();

        for(Map.Entry<String,?> entry : keys.entrySet())
        {
            Log.d("map values",entry.getKey() + ": " +  entry.getValue().toString());             
        }
    }

    public void setArraySize(int boolSize) 
    {
        editor.putInt("arraySize", boolSize);

        editor.commit();

        initializerBooleans(boolSize);
    }

    public int getArraySize() 
    {
        return prefs.getInt("arraySize", -1);
    }

    public boolean ItemVisited(int position) 
    {
        return prefs.getBoolean("Bool"+position, false);

    }
}

和 listView itemClicked..

listView.setOnItemClickListener(new OnItemClickListener() 
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long arg3) 
    {   
        Log.e("TAG_ADS","Item Visited " + position);

        sessionManager.setItemVisited(position);

        view.findViewById(R.id.ad_image).setBackgroundColor(Color.BLUE);

        final String appPackageName = arl.get(position).get("packageName"); //map.get("packageName"); // getPackageName() from Context or Activity object

         Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+ appPackageName));

         startActivity(marketIntent);

    }
});

我必须为访问过的 link 创建会话。在 listView 中我有 urls,一旦访问 url,它的背景颜色必须是 replaced.I 已经尝试了多种方法但没有任何反应。 一旦我点击任何项目,位置 0 的背景颜色也会改变,因为它是已访问的 link.

您必须执行以下操作:-

    View rowView = convertView;
    if (rowView == null) 
    {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.partner_list_element, null);
        // configure view holder
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.price = (TextView) rowView.findViewById(R.id.price);
        viewHolder.header = (TextView) rowView.findViewById(R.id.header);
        viewHolder.location = (TextView) rowView.findViewById(R.id.location);
        viewHolder.space = (TextView) rowView.findViewById(R.id.space);
        viewHolder.main_image = (ImageView) rowView.findViewById(R.id.main_image);
        viewHolder.logo_image = (ImageView) rowView.findViewById(R.id.logo_image);
        rowView.setTag(viewHolder);
    }

    // fill data
    ViewHolder holder = (ViewHolder) rowView.getTag();

在适配器

下 class
class ViewHolder {
    public TextView header,location,space,price;
    public ImageView main_image,logo_image;
}

Listview 回收他的元素,这就是您遇到问题的原因。

您只将背景颜色设置为绿色。下次调用 getView 时,convertView 将是某种副本,因此您不必膨胀它并做一些 findViewById,但背景颜色也已设置为绿色. 如果您添加 else 语句将颜色设置为白色(例如),它将起作用。

if(sessionManager.ItemVisited(position))// && position!=0)// && !sessionManager.getFirstRun())
{
    imageAd.setBackgroundColor(Color.GREEN);
} else {
    imageAd.setBackgroundColor(Color.WHITE);
}