Unfortunately I really think I have to explain my situation a little bit extensively.
I am writing an android app regarding the ancient Latin language: my goals are showing to users the entire conjugation of latin verbs, and giving them the right verbal analysis when they search for a specific inflected form. This is my manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android_application.app_name"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ConjActivity"
android:label="@string/app_name"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.android_application.app_name.MainActivity"/>
</activity>
</application>
</manifest>
Just know that in order to achieve the first goal I automatically create ALL the forms of every verb as strings, and every conjugation is showed as a simple ListView with only one TextView string as item: one item = one inflected verbal form.
Now, I need to customize my items, modifying some times their textStyle, some times their alignment, etc. In order to do so, I created my custom ListAdapter this way:
private static class ConjAdapter extends ArrayAdapter<String> {
private ArrayList<Long> ids;
private HashMap<String, Long> mIdMap;
public ConjAdapter(Context context, int textViewResourceId, List<String> objects) {
super(context, textViewResourceId, objects);
ids = new ArrayList<Long>();
mIdMap = new HashMap<String, Long>();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
ViewHolder holder;
if(convertView==null){
convertView = LayoutInflater.from(context).inflate(R.layout.simple_list_item_1, parent, false);
holder = new ViewHolder();
holder.txt = (TextView) convertView.findViewById(R.id.text1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
long id = getItemId(position);
if(sel_vb!=null){
if(ids.contains(id)){ v.setBackgroundColor(context.getResources().getColor(R.color.row_bckgr_RED));
} else {
v.setBackgroundColor(Color.TRANSPARENT);
}
}
for(Map.Entry<String, Long> map : mIdMap.entrySet()){
if(id==map.getValue()){
holder.txt.setText(map.getKey());
break;
}
}
String item = (String) holder.txt.getText();
if(item.equals(context.getResources().getString(R.string.ind))||
item.equals(context.getResources().getString(R.string.subj))||
item.equals(context.getResources().getString(R.string.imp))||
item.equals(context.getResources().getString(R.string.inf))||
item.equals(context.getResources().getString(R.string.pt))||
item.equals(context.getResources().getString(R.string.ger))||
item.equals(context.getResources().getString(R.string.gerv))||
item.equals(context.getResources().getString(R.string.sup))){
holder.txt.setGravity(Gravity.CENTER);
holder.txt.setTextSize(20f);
holder.txt.setTypeface(null, Typeface.BOLD);
} else if(item.equals(context.getResources().getString(R.string.pres))||
item.equals(context.getResources().getString(R.string.impf))||
item.equals(context.getResources().getString(R.string.fut))||
item.equals(context.getResources().getString(R.string.pf))||
item.equals(context.getResources().getString(R.string.ppf))||
item.equals(context.getResources().getString(R.string.futant))){
holder.txt.setTypeface(null, Typeface.ITALIC);
holder.txt.setTextSize(19f);
} else {
holder.txt.setPadding(10, 0, 0, 0);
}
return convertView;
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
static class ViewHolder {
TextView txt;
}
}
The problem is that after the 19th item something goes wrong in the sense that I receive no error messages nor the app crashes, but my custom code doesn't work anymore and items, that should have some features, have on the contrary other ones. And this situation worsens when I scroll my list up and down.
After what I read, I really think that this problem regards the recycling goal of the convertview variable called by the getView() of my custom adapter.
And here is my question: why does it happen even with stable ids (that I store in mIdMap along with the associated item strings) and how can I detach my item from the incorrect position variable?
Update
Here is the code with which I populate mIdMap and ids:
HashMap<String, Long> tempMap = conjadapt.mIdMap;
for(int i=0, j=0; i<displ_conj.size(); i++, j++){
tempMap.put(displ_conj.get(i), (long) j);
}
if(sel_vb!=null){
for(Map.Entry<String, Long> map : tempMap.entrySet()){
if(map.getKey().equals(sel_vb))
conjadapt.ids.add(map.getValue());
}
}
where displ_conj is the ArrayList in which I store my data. mIdMap stores a long variables because the getItemId() has to return a long and with that I need to do some stuff somewhere else.