我通过asynctask获得了一些数据,我将它们存储到List中(doInBackground):
HashMap<String, String> orden = new HashMap<String, String>();
orden.put("ref",ref);
orden.put("of",of);
orden.put("amaituta",amaituta);
lLinea1.add(orden);
之后我在onPostExecute
上填充了这样的Listview 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,我需要删除文本。
我该怎么做?
感谢
答案 0 :(得分:2)
这不适用于SimpleAdapter。您需要覆盖getView部分并自己填充TextView以执行以下操作:
TextView item = (TextView) v.findViewById(R.id.textView);
item.setPaintFlags(item.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
答案 1 :(得分: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.如何使用自定义适配器:
list=(ListView)findViewById(android.R.id.list);
CustomAdapter adapter = new CustomAdapter(MyActivity.this, lLinea1,);
list.setAdapter(adapter);