Reputation: 4532
An ArrayList holds string values in the following order: "GREEN", "GREEN", "RED", "RED", "GREEN", "GREEN". The values are correctly displayed in the ListView of the ListActivity.
I've set it so that when any item in the ListView is clicked, all items with string value "RED" are deleted from the list. However, the problem is that the ListView, despite its value container now only containing "GREEN" values, visually shows the removal of two "GREEN" values, so now the ListView shows "GREEN", "GREEN", "RED", "RED", even though the adapter contains the values "GREEN", "GREEN", "GREEN", "GREEN.
If the adapter contains the values R, R, G, G, R, R, G, G and an item is clicked, it now contains R, R, G, G.
Is there an error in my code? The debugger showing all "GREEN" string values is really perplexing because the ListView is showing something different.
public class MyActivity extends ListActivity implements AdapterView.OnItemClickListener {
ArrayList<String> listValues;
bugtestadapter adapter;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listValues = new ArrayList<String>();
listValues.add("GREEN");
listValues.add("GREEN");
listValues.add("RED");
listValues.add("RED");
listValues.add("GREEN");
listValues.add("GREEN");
adapter = new bugtestadapter(this, R.layout.adapter, listValues);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
deleteRedValues();
adapter.notifyDataSetChanged();
}
public void deleteRedValues() {
for (Iterator<String> iterator = listValues.iterator(); iterator.hasNext();) {
String c = iterator.next();
if (c.equals("RED")) {
iterator.remove();
}
}
}
}
bugtestadapter.java
public class bugtestadapter extends ArrayAdapter<String> {
Context mContext;
ArrayList<String> adapterList;
LayoutInflater inflater;
public bugtestadapter(Context context, int resource, ArrayList<String> objects) {
super(context, resource, objects);
mContext = context;
adapterList = (ArrayList)objects;
inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if ( convertView == null ) {
convertView = inflater.inflate( R.layout.adapter, null );
TextView tv = (TextView) convertView.findViewById(R.id.adapterItem);
tv.setText(adapterList.get(position));
if (tv.getText().equals("GREEN"))
tv.setTextColor(Color.GREEN);
else if (tv.getText().equals("RED"))
tv.setTextColor(Color.RED);
}
return convertView;
}
}
Upvotes: 0
Views: 447
Reputation:
In getView method initialize the views in the If condition i.e., if(convertView==null) and set teh data outside of the loop.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if ( convertView == null ) {
holder=new Holder();
convertView = inflater.inflate( R.layout.adapter, null );
holder.tvName = (TextView) convertView.findViewById(R.id.adapterItem);
}else{
holder=convertView.getTag();
}
tv.setText(adapterList.get(position));
if (tv.getText().equals("GREEN"))
tv.setTextColor(Color.GREEN);
else if (tv.getText().equals("RED"))
tv.setTextColor(Color.RED);
return convertView;
}
and add an Holder to it.
private class Holder{
TextView tvName;
}
Upvotes: 2
Reputation: 7439
You have to do it with Holder class like below:
public class bugtestadapter extends ArrayAdapter<String> {
Context mContext;
ArrayList<String> adapterList;
LayoutInflater inflater;
public bugtestadapter(Context context, int resource, ArrayList<String> objects) {
super(context, resource, objects);
mContext = context;
adapterList = (ArrayList)objects;
inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if ( convertView == null ) {
holder = new ViewHolder();
convertView = inflater.inflate( R.layout.adapter, null );
holder.tv = (TextView) convertView.findViewById(R.id.adapterItem);
holder.tv.setText(adapterList.get(position));
if (holder.tv.getText().equals("GREEN"))
holder.tv.setTextColor(Color.GREEN);
else if (tv.getText().equals("RED"))
holder.tv.setTextColor(Color.RED);
}else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
class ViewHolder {
TextView tv;
}
}
Upvotes: 1