1

I have a ListView that consists of booked appointments and unbooked appointments,I can change the color when its clicked but i want to know how to change the color of booked by default.

When the data is fetched and shown in listview the booked and unbooked should be displayed in different colors.Is it possible?

Can anyone help me out please? Thanks in advance! Just a guidance is enough!

Sharath Kumar
  • 175
  • 1
  • 14

3 Answers3

3

You should follow the steps as follow.

  1. You first need 1 column in your database where you can check the status of appointment i.e. booked or unbooked [boolean field is expected] or Can use ENUM if more than two types of status with varchar column.

  2. After that you need to get data form database to array adaptor.

          Class MyAdaptor extends ArrayAdaptor<YourEntityClass>{
           ...............
          } 
    
  3. After that you need to override the getView method inside array adaptor class if you are using custom adaptor.

      public View getView(int position, View convertView, ViewGroup parent){
    
           LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           View rowView = inflater.inflate(R.layout.Your_TextView_File, parent, false);
       ........
       return rowView;
      }
    
  4. Now inside getView check if the if(object.getAppoinmentStatus == true) show some changes else show some changes.

        final YourEntityClass a = getItem(position);
        if(a.getAppoinmentStatus == true){
             //Change required text color
        }else{
           //Default
        }
    
  5. Get On item Click listener in class

     public class ListActivity extends Activity{
          private MyAdaptor adaptor;
          private ListView lv;
         @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.YourActivityLayout);
        adaptor = new MyAdaptor(this);
        lv = (ListView)findById(R.id.YourListView);
        onItemClickListener();
        }
    
    public void onItemClickListener(){
     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
            if (adaptor.getItem(i).getAppoinmentStatus() == 0){
                 //Do Some Work
              }else{
                  //Do some Work
                 }
            }
     }
    

    This might help you

0

You can check the data type in your Adapter class before displaying.Set a bit for booked and unbooked data.Check the bit before setting data to ListView and change the color accordingly.See the below code snippet. Here, status is the bit whose value you can pass through the constructor.

@Override
public View getView(int position, View convertView, ViewGroup parent) {  
View view = super.getView(position, convertView, parent);  
if (status == 1) {
    view.setBackgroundColor(Color.BLUE);  
} else {
    view.setBackgroundColor(Color.CYAN);  
}

return view;  
}
Jas
  • 3,207
  • 2
  • 15
  • 45
0

In your getView() of Adapter method try something like this,

public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            final ListContent holder;
            View v = convertView;
            if (v == null) {
                v = mInflater.inflate(R.layout.row_list_item, parent, false);
                holder = new ListContent();

                holder.line_main = (LinearLayout) v.findViewById(R.id.line_main);
                v.setTag(holder);
            } else {
                holder = (ListContent) v.getTag();
            }

            Model pi = arrayModelList.get(position);

            if (pi.getFlag() == 1) {
                holder.line_main.setBackgroundColor(ContextCompat.getColor(YourActivity.this, R.color.colorPrimaryDark));
            } else if (pi.getFlag() == 2) {
                holder.line_main.setBackgroundColor(ContextCompat.getColor(YourActivity.this, R.color.colorPrimary));
            } else {
                holder.line_main.setBackgroundColor(ContextCompat.getColor(YourActivity.this, R.color.colorLightPrimary2));
            }
            return v;
        }

In your model Class generate getter and setter method for flag,

private int flag;//1 = boxed,2 = unboxed

public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142