2014-10-28 17 views
2

從來就得到了一些數據女巫我通過的AsyncTask讓我把它們存儲到這樣的(doInBackground)名單:大膽一些文字,如果列表視圖條件

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

,我填充一個ListView這樣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); 

一切工作正常,但我需要的,如果「amaituta」變量等於1

我如何能做到這一點,以刪除線的文本?

謝謝

+1

使用CustomAdapter和getView實現粗體字代碼()。 – 2014-10-28 13:03:29

+0

+1 Haresh。您必須創建一個自定義適配器來實現它。 – 2014-10-28 13:05:52

+0

@ PareshMayani,驚喜謝謝你先生對我的好成績。 – 2014-10-28 13:08:54

回答

2

試試這個辦法,希望這將幫助你解決你的問題。

1.創建一個XML爲列表項:

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.創建定製適配器與ViewHolder和在getView列表項目數據條件中設定的TextView花柱基部( ):

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使用自定義適配器:

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

WoW!完美的作品,現在我可以做更多的東西;-)非常感謝! – 2014-10-29 14:19:40

+0

@ KiokoKiaza,很高興幫助你,親愛的... – 2014-10-29 14:32:35

2

這不適用於您的SimpleAdapter。你需要覆蓋getView部分並填充自己的TextView爲了做這樣的事情:

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