Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging backround color of row in listview for Android app
    primarykey
    data
    text
    <p>I have a list view with multi columns... Want to change the background color of a row item that is selected or clicked. Have referred to various articles on stackoverflow but they dont seem to help...What i ve done in OnItemClick is .setBackgroundColor. Cannot use the selector as would want to remember the color change in the sense the row that I have changed color for, when the application relaunches should remember the change...I put the flag value in database and can set the color in getView but how to do it for first time? Here is the code.....</p> <pre><code>public class AdminMultiColumn extends Activity { private ArrayList&lt;HashMap&lt;String,String&gt;&gt; list; ArrayList&lt;String&gt; al; AlertDialog.Builder alertDialogBuilder; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.admin_checkoutlist); lview = (ListView)findViewById(R.id.admin_list); populateList(); Admin_checkoutlist adapter = new Admin_checkoutlist(this,list); lview.setAdapter(adapter); } private void populateList() { list = new ArrayList&lt;HashMap&lt;String,String&gt;&gt;(); al = new ArrayList&lt;String&gt;(); testday = c.get(Calendar.DAY_OF_MONTH); testmonth = c.get(Calendar.MONTH)+1; testyear = c.get(Calendar.YEAR); dateget = testday+"-"+testmonth+"-"+testyear; mdb.open(); va.open(); Cursor c1 = va.getNameTimeinTimeout(dateget); DatabaseUtils.dumpCursor(c1); if(c1.moveToFirst()) { do{ idfromdb = c1.getString(0); al.add(idfromdb); namefromdb = c1.getString(1); al.add(namefromdb); timefromdb = c1.getString(2); al.add(timefromdb); timeoutfromdb = c1.getString(3); al.add(timeoutfromdb); }while(c1.moveToNext()); } c1.close(); va.close(); mdb.close(); Log.d("pavan","data retrived "+namefromdb+" "+timeoutfromdb+" "+timefromdb+" "+idfromdb); for(int i = 0;i&lt;al.size();i=i+4) { temp = new HashMap&lt;String,String&gt;(); temp.put(FIRST_COLUMN, al.get(i)); temp.put(SECOND_COLUMN, al.get(i+1)); temp.put(THIRD_COLUMN, al.get(i+2)); temp.put(FOURTH_COLUMN, al.get(i+3)); Log.d("pavan","test here for admin"); list.add(temp); } } //adapter class goes here//////////// public class Admin_checkoutlist extends BaseAdapter implements OnItemClickListener { public ArrayList&lt;HashMap&lt;String,String&gt;&gt; list; Activity activity; public Admin_checkoutlist(Activity activity, ArrayList&lt;HashMap&lt;String,String&gt;&gt; list) { super(); this.activity = activity; this.list = list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int pos) { // TODO Auto-generated method stub return list.get(pos); } @Override public long getItemId(int pos) { // TODO Auto-generated method stub return 0; } public class ViewHolder { TextView txtFirst; TextView txtSecond; TextView txtThird; TextView txtFOURTH; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = activity.getLayoutInflater(); if (convertView == null) { convertView = inflater.inflate(R.layout.temp, null); holder = new ViewHolder(); holder.txtFirst = (TextView)convertView.findViewById(R.id.uid); holder.txtSecond = (TextView)convertView.findViewById(R.id.nametext); holder.txtThird = (TextView)convertView.findViewById(R.id.checkintext); holder.txtFOURTH = (TextView)convertView.findViewById(R.id.checkouttext); convertView.setTag(holder); lview.setOnItemClickListener(this); } else { holder = (ViewHolder) convertView.getTag(); } HashMap&lt;String, String&gt; map = list.get(position); holder.txtFirst.setText(map.get(FIRST_COLUMN)); holder.txtSecond.setText(map.get(SECOND_COLUMN)); holder.txtThird.setText(map.get(THIRD_COLUMN)); holder.txtFOURTH.setText(map.get(FOURTH_COLUMN)); return convertView; }//getview ends here. @Override public void onItemClick(final AdapterView&lt;?&gt; parent, final View view, int pos, long id) { // TODO Auto-generated method stub Object listItem = parent.getFirstVisiblePosition()+pos+1; //lview.getChildAt(pos).setBackgroundColor(R.color.blue); view.setBackgroundColor(pos); idtosend = listItem.toString(); Log.d("pavan","id is"+idtosend); /////alert displaing block starts here//////////////////////////////// alertDialogBuilder = new AlertDialog.Builder(AdminMultiColumn.this); alertDialogBuilder.setTitle("Alert"); alertDialogBuilder .setMessage("Check out now") .setCancelable(false) .setPositiveButton("Yes",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // if this button is clicked, close // current activity Toast.makeText(getApplicationContext(), "Checkout successfull", Toast.LENGTH_LONG).show(); testhour = c.get(Calendar.HOUR_OF_DAY); testmin = c.get(Calendar.MINUTE); testsec = c.get(Calendar.SECOND); timings = testhour+":"+testmin+":"+testsec; mdb.open(); va.open(); va.upDate(idtosend,timings); Toast.makeText(getApplicationContext(), "update the checkout options "+timings, Toast.LENGTH_SHORT).show(); va.close(); mdb.close(); /*Intent go = new Intent(AdminMultiColumn.this,MultiColumn.class); startActivity(go);*/ } }) .setNegativeButton("No",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // if this button is clicked, just close // the dialog box and do nothing dialog.cancel(); } }); // create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); ///////alert displaing block ends here////////////////////////////////// } } } </code></pre> <p>What I have done now is within the getView method, taking the convertView and setting the background resource to a color, that too is not working...below is the code...</p> <pre><code> if(holder.txtFOURTH!=null) { convertView.setBackgroundColor(R.drawable.image_border); } else { Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload