更改Android视图列表视图中行的背景颜色

时间:2012-06-14 06:11:48

标签: android

我有一个包含多列的列表视图...想要更改所选或单击的行项目的背景颜色。已经提到了关于stackoverflow的各种文章,但它们似乎没有帮助......我在OnItemClick中所做的是.setBackgroundColor。无法使用选择器,因为我想要记住颜色的变化,我已经改变颜色的行,当应用程序重新启动应该记住更改...我把标志值放在数据库中,可以在getView中设置颜色但是怎么做第一次?这是代码.....

public class AdminMultiColumn extends Activity
{
    private ArrayList<HashMap<String,String>> list;
    ArrayList<String> 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<HashMap<String,String>>();  

        al = new ArrayList<String>();

        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<al.size();i=i+4)
        {
            temp   = new HashMap<String,String>();  
            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<HashMap<String,String>> list;  
        Activity activity;   

        public Admin_checkoutlist(Activity activity, ArrayList<HashMap<String,String>> 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<String, String> 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<?> 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//////////////////////////////////           
        }

    }

}

我现在所做的是在getView方法中,将convertView和背景资源设置为一种颜色,这也不起作用......下面是代码......

 if(holder.txtFOURTH!=null)
   {
    convertView.setBackgroundColor(R.drawable.image_border);
   }
   else
   {
    Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
   }

1 个答案:

答案 0 :(得分:0)

有两点 -

  1. pos不是一个很好的颜色选项

  2. 而不是view.setBackgroundColor(pos);起诉

  3. LinearLayout row = (LinearLayout)view.findViewById(R.id.list_row);
            if(null!=row){
                //<listAdapter>.notifyDataSetChanged();
                row.setBackgroundResource(R.drawable.);
            }
    
    • 假设您的列表行在root中具有ID为“list_row”的LinearLayout,请根据您的布局对其进行处理。

    但会出现问题,因为当你点击第二行时它也会被选中,并且最好拨打list.notifydatasetchaged还有更多选项可以查看它们,可能会满足你的要求

    1. android-selected-state-listview-example

    2. android-how-do-i-highlight-a-row-in-listview

    3. highlighting-the-selected-item-in-the-listview-in-android