Kioko Kiaza
Kioko Kiaza

Reputation: 1398

Bold some text if condition on listview

I´ve got some data witch I get via asynctask and I store them into List like this (doInBackground):

HashMap<String, String> orden = new HashMap<String, String>();
orden.put("ref",ref);
orden.put("of",of);
orden.put("amaituta",amaituta);
lLinea1.add(orden);

After that I populated a Listview like this onPostExecute

    list=(ListView)findViewById(android.R.id.list);
    ListAdapter adapter = new SimpleAdapter(
            MyActivity.this, lLinea1,
               R.layout.of_list_item, new String[] { "ref","of", "amaituta" }, new int[] { R.id.ref, R.id.of, R.id.amaituta });


    list.setAdapter(adapter);

Everything works fine but I need to strikethrough the text if "amaituta" variable is equals to 1.

How can I do that?

thanks

Upvotes: 2

Views: 1175

Answers (2)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

Try this way,hope this will help you to solve your problem.

1.Create one xml for list item :

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtRef"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/txtOf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"/>

    <TextView
        android:id="@+id/txtAmaituta"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"/>
</LinearLayout>

2.Create Custom Adapter with ViewHolder and set TextView style base on list item data condition in getView() :

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<HashMap<String, String>> data;

    public CustomAdapter(Context context, ArrayList<HashMap<String, String>> data) {
        this.data = data;
        this.context=context;
    }

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

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

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

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.list_item, null);
            holder.txtRef = (TextView) view.findViewById(R.id.txtRef);
            holder.txtOf = (TextView) view.findViewById(R.id.txtOf);
            holder.txtAmaituta = (TextView) view.findViewById(R.id.txtAmaituta);

            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }

        holder.txtRef.setText(data.get(position).get("ref"));
        holder.txtOf.setText(data.get(position).get("of"));
        holder.txtAmaituta.setText(data.get(position).get("amaituta"));

        if (data.get(position).get("amaituta").equals("1")) {
            holder.txtAmaituta.setTypeface(null,Typeface.BOLD);
        } else {
            holder.txtAmaituta.setTypeface(null, Typeface.NORMAL);
        }
        return view;

    }

    class ViewHolder{
        TextView txtRef;
        TextView txtOf;
        TextView txtAmaituta;
    }

}

3.How to use Custom Adapter :

list=(ListView)findViewById(android.R.id.list);
CustomAdapter adapter = new CustomAdapter(MyActivity.this, lLinea1,);
list.setAdapter(adapter);

Upvotes: 2

Carsten
Carsten

Reputation: 806

This will not work with your SimpleAdapter. You need to overwrite the getView part and populate your TextView yourself in order to do something like this:

  TextView item = (TextView) v.findViewById(R.id.textView);
  item.setPaintFlags(item.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

Upvotes: 2

Related Questions