AZEEM AHMED AZEEM AHMED - 1 month ago 14
Android Question

How to change Background Color of row in List View Using List View Column Field

I want to change the back ground of my list view row if the column value is same the the list view back ground would be change here is my code.

public class FillList extends AsyncTask<String, String, String> {
String z = "";

List<Map<String, String>> prolist = new ArrayList<Map<String, String>>();

@Override
protected void onPreExecute() {

}

@Override
protected void onPostExecute(String r) {

Toast.makeText(getActivity(), r, Toast.LENGTH_SHORT).show();

String[] from = {"C", "D", "E"};
int[] views = {R.id.accname, R.id.openingbalance, R.id.closingblanace};
final SimpleAdapter ADA = new SimpleAdapter(getActivity(),
prolist, R.layout.reportlayout, from,
views);
lstpro.setAdapter(ADA);


}


@Override
protected String doInBackground(String... params) {

try {
Connection con = connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
} else {
final String stringlevel = getArguments().getString("level");
String query = "select * from GLSCOADetail where AccLevelNo<='"+ stringlevel +"'";
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();


ArrayList<String> data1 = new ArrayList<String>();
while (rs.next()) {
Map<String, String> datanum = new HashMap<String, String>();
datanum.put("C", rs.getString("AccName"));
datanum.put("D", rs.getString("OpeningBalance"));
datanum.put("E", rs.getString("ClosingBalance"));

prolist.add(datanum);
}

z = "Success";
}
} catch (Exception ex) {
z = ex.getMessage();

}
return z;
}
}


I want to change my list view background on database filed if my database field match with list view then color of row would change please help me out.

Answer

Main (Activity)

    public class Main extends Activity
{
    private CustomAdapter adapter;
    public  ListView mListView = null;
    public ArrayList<ListModel> CustomListViewValuesArr = new ArrayList<ListModel>();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_layout);
        mListView = (ListView) findViewById(R.id.list);
        setListData();
        adapter = new CustomAdapter(this, CustomListViewValuesArr);
        mListView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    }

    /****** Function to set data in ArrayList *************/
    public void setListData()
    {
        CustomListViewValuesArr.add(new ListModel("No1"));
        CustomListViewValuesArr.add(new ListModel("No2"));//same
        CustomListViewValuesArr.add(new ListModel("No3"));
        CustomListViewValuesArr.add(new ListModel("No4"));
        CustomListViewValuesArr.add(new ListModel("No2"));//same

    }

}

listview_layout.xml (Activity xml)

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

CustomAdapter (Adapter)

    public class CustomAdapter extends BaseAdapter
{

    /*********** Declare Used Variables *********/
    private Activity mActivity;
    private ArrayList<ListModel> mList;
    private static LayoutInflater mInflater = null;
    private ListModel tempValues = null;
    private int i=0;

    /*************  CustomAdapter Constructor *****************/
    public CustomAdapter(Activity a, ArrayList d) {

        /********** Take passed values **********/
        mActivity = a;
        mList = d;
        /***********  Layout inflator to call external xml layout () ***********/
        mInflater = ( LayoutInflater ) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /******** What is the size of Passed Arraylist Size ************/
    public int getCount() {

        if(mList.size()<=0)
            return 1;
        return mList.size();
    }

    public Object getItem(int position) {
        return position;
    }

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

    /********* Create a holder Class to contain inflated xml file elements *********/
    public static class ViewHolder
    {
        public TextView text;

    }

    /****** Depends upon mList size called for each row , Create each ListView row *****/
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View vi = convertView;
        ViewHolder holder;
        if(convertView==null){

            /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
            vi = mInflater.inflate(R.layout.tabitem, null);

            /****** View Holder Object to contain tabitem.xml file elements ******/

            holder = new ViewHolder();
            holder.text = (TextView) vi.findViewById(R.id.textView);

            /************  Set holder with LayoutInflater ************/
            vi.setTag( holder );
        }
        else
            holder=(ViewHolder)vi.getTag();

        if(mList.size() <= 0)
        {
            holder.text.setText("No Data");
        }
        else
        {
            tempValues = null;
            tempValues = ( ListModel ) mList.get( position );
            holder.text.setText( tempValues.getName() );
        }

        if(isDuplicate(tempValues.getName()))// this Method to check duplicate.
        {
            // Set a background color for ListView regular row/item
            vi.setBackgroundColor(Color.parseColor("#FFB6B546"));
        }
        else
        {
            // Set the background color for alternate row/item
            vi.setBackgroundColor(Color.parseColor("#FFCCCB4C"));
        }
        return vi;
    }

    private boolean isDuplicate(String item)
    {
        int count = 0;
        for (int i = 0; i < mList.size(); i++)
        {
            ListModel listModel = mList.get(i);
            if(item.equals(listModel.getName()))
                count++;
        }
        if(count > 1)
            return true;
        else
            return false;
    }
}

ListModel (Model Class)

    public class ListModel {
    private  String name ="";

    /*********** Set Methods ******************/

    public ListModel(String name)
    {
        this.name = name;
    }

    public void setName(String CompanyName)
    {
        this.name = CompanyName;
    }

    /*********** Get Methods ****************/

    public String getName()
    {
        return this.name;
    }

}

tabitem (ListView Adapter xml)

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:id="@+id/textView" />
</LinearLayout>