Leonard Febrianto Leonard Febrianto - 1 month ago 13
Android Question

Underline TextView in ListView

I have this code in my adapter :

class CustomListView2 extends BaseAdapter {
private ArrayList<ListItem> listData;
private LayoutInflater layoutInflater;
private Context context;
int hasil;
public CustomListView2(Context context, ArrayList<ListItem> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
this.context = context;
}

@Override
public int getCount() {
return listData.size();
}

@Override
public Object getItem(int position) {
return listData.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.history_listview, null);
holder = new ViewHolder();
holder.no_order = (TextView) convertView.findViewById(R.id.no_order);
holder.status_order = (TextView) convertView.findViewById(R.id.status_order);
holder.tgl_antar = (TextView) convertView.findViewById(R.id.tgl_antar);
holder.check_order = (CheckBox) convertView.findViewById(R.id.chkBatal);
holder.layoutHistory = (LinearLayout) convertView.findViewById(R.id.layoutHistory);
convertView.setTag(holder);

} else {
holder = (ViewHolder) convertView.getTag();
}

if (mapStatus.containsKey(position))
{
holder.status_order.setPaintFlags(holder.status_order.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
holder.status_order.setEnabled(true);
}


And i declared this in global variable :

Map<Integer,String> mapStatus = new HashMap<Integer,String>();


What im tying is only to make a textview getting underlined based on position that i have put in HashMap.

But after im scrolling the listview, the other textview is getting underlined too.

How to fix this ?

Answer

It's because your view holder is reusing your view. Try to add a else conditional with an opposite operation that remove the underline:

holder.status_order.setPaintFlags(holder.status_order.getPaintFlags() & !Paint.UNDERLINE_TEXT_FLAG);

Not sure if & is used like this.

Comments