我必须设置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背景颜色也改变,因为它是一个访问过的链接。
非常感谢您宝贵的时间^ _ ^ – Nepster