我创建了自定义LISTVIEW以显示搜索结果列表想要显示每行的背景颜色我尝试了一些代码但不能按照我的要求工作请帮帮我
这是我的代码
public class ListCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResult> searchArrayList;
private LayoutInflater mInflater;
public ListCustomBaseAdapter(Context context,
ArrayList<SearchResult> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.custNm);
holder.txtProsNm = (TextView) convertView.findViewById(R.id.prosNm);
holder.txtFrmPort = (TextView) convertView
.findViewById(R.id.frmPort);
holder.txtToPort = (TextView) convertView.findViewById(R.id.ToPort);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (position % 2 == 0) {
holder.txtName.setBackgroundColor(Color.WHITE);
holder.txtProsNm.setBackgroundColor(Color.WHITE);<----- change bgcolor of textview not whole row of View
holder.txtFrmPort.setBackgroundColor(Color.WHITE);
holder.txtToPort.setBackgroundColor(Color.WHITE);
} else {
holder.txtName.setBackgroundColor(Color.DKGRAY);
holder.txtProsNm.setBackgroundColor(Color.DKGRAY);
holder.txtFrmPort.setBackgroundColor(Color.DKGRAY);
holder.txtToPort.setBackgroundColor(Color.DKGRAY);
}
holder.txtName.setText(searchArrayList.get(position).getCustNm());
holder.txtProsNm.setText(searchArrayList.get(position).getProsNm());
holder.txtFrmPort.setText(searchArrayList.get(position).getFrmPort());
holder.txtToPort.setText(searchArrayList.get(position).getToPort());
return convertView;
}
static class ViewHolder {
TextView txtName;
TextView txtFrmPort;
TextView txtToPort;
TextView txtProsNm;
}
}
调用适配器
public void onCreate(Bundle savedInstanceState) throws SQLException {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Connection object for Spinner
con = new DatabaseConnection(this);
try {
// Get Filtermap from Parent Activity
// this Bundle contains HashMap
Bundle wrapper = getIntent().getBundleExtra("salesActList");
salesLstObj = (Map<String, Object>) wrapper.getSerializable("salesActCriteriaList");
salesLst = con.searchSalesActivity(salesLstObj);
ArrayList<SearchResult> searchResults = getSearchResults();
lv = (ListView) findViewById(R.id.srListView);
lv.setAdapter(new ListCustomBaseAdapter(this, searchResults));
});
} catch (SQLException se) {
se.getStackTrace();
String flag = "fail";
dialogBox(flag);
}
这里是输出img
想要将蓝色区域交替显示为灰色怎么制作?
客户行视图代码
<TableLayout
style="@style/TableLayoutStyle"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<TableRow
android:id="@+id/tblRwCust"
style="@style/TableRowStyle" >
<TextView
android:id="@+id/custNm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF7F24"
android:textSize="18sp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="@+id/tblRwPros"
style="@style/TableRowStyle" >
<TextView
android:id="@+id/prosNm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
</TableRow>
<TableRow
android:id="@+id/tblRwPort"
style="@style/TableRowStyle" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/frmPort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:textColor="#808080" />
<TextView
android:id="@+id/ToPort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#808080" />
</LinearLayout>
</TableRow>
</TableLayout>
答案 0 :(得分:3)
以下是Table layout
代替Relative layout
<TableLayout
style="@style/TableLayoutStyle"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:id="@+id/myTable"
android:background="#FFFFFF">
适配器中的
holder.myTable = (TableLayout) convertView.findViewById(R.id.myTable);
if (position % 2 == 0) {
holder.myTable.setBackgroundColor(Color.WHITE);
} else {
holder.myTable.setBackgroundColor(Color.GRAY);
}
答案 1 :(得分:3)
if(position%2 == 0 ){
holder.convertView.setBackgroundColor(Color.CYAN);
}else{
holder.convertView.setBackgroundColor(Color.YELLOW);
}