listView项目行背景自动更改

时间:2015-01-13 13:34:55

标签: android android-listview sharedpreferences

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

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

    @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);
        }

}

SessionManager

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);

    }
});

我必须为访问过的链接创建会话。在listView我有网址,一旦访问了网址,它的backgroundColor必须被替换。我尝试过多种方式但没有任何反应。 一旦我点击任何项目,位置0背景颜色也会改变,因为它是一个访问过的链接。

2 个答案:

答案 0 :(得分:2)

您只将backgroundcolor设置为绿色。下次getView被调用时,convertView将是某种副本,因此您不必对其进行充气并执行一些findViewById,但背景颜色也已设置为绿色。 如果添加else语句将颜色设置为白色(例如),它将起作用。

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

答案 1 :(得分:0)

您必须执行以下操作: -

    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 ViewHolder {
    public TextView header,location,space,price;
    public ImageView main_image,logo_image;
}

Listview回收他的元素,这就是你面临问题的原因。