Search code examples
androidlistviewandroid-custom-view

How to change Custom Listview row color change alternatively?


I have created custom LISTVIEW for showing searched result list want to show each row with background color I tried out with some code but not working as per my requirement please help me out

here is my code

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;
}

}

calling adapter

 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);

}

here is output img

enter image description here

want to show Blue color area to be in gray alternately how to make it?

Custome row view code

  <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>

Solution

  • Here is my solution with Table layout instead 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">
    

    in adapter

    holder.myTable = (TableLayout) convertView.findViewById(R.id.myTable);
    
    if (position % 2 == 0) {
            holder.myTable.setBackgroundColor(Color.WHITE);
        } else {
            holder.myTable.setBackgroundColor(Color.GRAY);
        }