2015-01-13 87 views
0

我必须设置visitedItem(listView行的imageView)的背景颜色。listView项行背景自动更改

它显示一个项目列表,如果您访问了(itemclick)行,其项目backgroundColor必须更改。我为它使用sharedPreferences。并调试它(在调试中显示false)。但不知道为什么第一次项目设置为绿色后第一次itemClick点击任何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中,我有urls,一旦访问了一个url,它的backgroundColor必须被替换。我尝试了多种方式,但没有任何反应。 一旦我点击任何项目,位置0背景颜色也改变,因为它是一个访问过的链接。

回答

2

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

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

非常感谢您宝贵的时间^ _ ^ – Nepster

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

列表视图循环,这就是为什么你面临的问题他的元素。