0

Following is the code of my Java file:

package samples.employeedirectory;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class EmployeeDetails extends ListActivity {

    protected TextView employeeNameText;
    protected TextView titleText;
    protected List<EmployeeAction> actions;
    protected EmployeeActionAdapter adapter;
    protected int employeeId;
    protected int managerId;
    protected TextView availabilityText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.employee_details);

        employeeId = getIntent().getIntExtra("EMPLOYEE_ID", 0);
        SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT emp._id, emp.firstName, emp.lastName, emp.experience, emp.techskills, emp.currentpro, emp.title, emp.officePhone, emp.cellPhone, emp.email, emp.availability, emp.modelskills, emp.managerId, mgr.firstName managerFirstName, mgr.lastName managerLastName FROM employee emp LEFT OUTER JOIN employee mgr ON emp.managerId = mgr._id WHERE emp._id = ?", 
                new String[]{""+employeeId});

        if (cursor.getCount() == 1)
        {
            cursor.moveToFirst();

            employeeNameText = (TextView) findViewById(R.id.employeeName);
            employeeNameText.setText(cursor.getString(cursor.getColumnIndex("firstName")) + " " + cursor.getString(cursor.getColumnIndex("lastName")));

            titleText = (TextView) findViewById(R.id.title);
            titleText.setText(cursor.getString(cursor.getColumnIndex("title")));




            actions = new ArrayList<EmployeeAction>();



            String officePhone = cursor.getString(cursor.getColumnIndex("officePhone"));
            if (officePhone != null) {
                actions.add(new EmployeeAction("Call office", officePhone, EmployeeAction.ACTION_CALL));
            }

            String cellPhone = cursor.getString(cursor.getColumnIndex("cellPhone"));
            if (cellPhone != null) {
                actions.add(new EmployeeAction("Call mobile", cellPhone, EmployeeAction.ACTION_CALL));
                actions.add(new EmployeeAction("SMS", cellPhone, EmployeeAction.ACTION_SMS));
            }

            String email = cursor.getString(cursor.getColumnIndex("email"));
            if (email != null) {
                actions.add(new EmployeeAction("Email", email, EmployeeAction.ACTION_EMAIL));
            }

            String available = cursor.getString(cursor.getColumnIndex("availability"));
            if (available != null) {
                actions.add(new EmployeeAction("Availability", available, -1));
            }

            String experienc = cursor.getString(cursor.getColumnIndex("experience"));
            if (experienc != null) {
                actions.add(new EmployeeAction("Experience(in years)", experienc, -1));
            }

            String modskills = cursor.getString(cursor.getColumnIndex("modelskills"));
            if (modskills != null) {
                actions.add(new EmployeeAction("Modelling skills",modskills, -1));
            }

            String techskills = cursor.getString(cursor.getColumnIndex("techskills"));
            if (techskills != null) {
                actions.add(new EmployeeAction("Technical skills",techskills, -1));
            }
            String currpro = cursor.getString(cursor.getColumnIndex("currentpro"));
            if (currpro != null) {
                actions.add(new EmployeeAction("Current project",currpro, -1));
            }

            managerId = cursor.getInt(cursor.getColumnIndex("managerId"));
            if (managerId>0) {
                actions.add(new EmployeeAction("View manager", cursor.getString(cursor.getColumnIndex("managerFirstName")) + " " + cursor.getString(cursor.getColumnIndex("managerLastName")), EmployeeAction.ACTION_VIEW));
            }

            cursor = db.rawQuery("SELECT count(*) FROM employee WHERE managerId = ?", 
                    new String[]{""+employeeId});
            cursor.moveToFirst();
            int count = cursor.getInt(0);
            if (count>0) {
                actions.add(new EmployeeAction("View direct reports", "(" + count + ")", EmployeeAction.ACTION_REPORTS));
            }

            adapter = new EmployeeActionAdapter();
            setListAdapter(adapter);
        }

    }

    public void onListItemClick(ListView parent, View view, int position, long id) {

        EmployeeAction action = actions.get(position);

        Intent intent;
        switch (action.getType()) {

            case EmployeeAction.ACTION_CALL:  
                Uri callUri = Uri.parse("tel:" + action.getData());  
                intent = new Intent(Intent.ACTION_CALL, callUri); 
                startActivity(intent);
                break;

            case EmployeeAction.ACTION_EMAIL:  
                intent = new Intent(Intent.ACTION_SEND);
                intent.setType("plain/text");
                intent.putExtra(Intent.EXTRA_EMAIL, new String[]{action.getData()});
                startActivity(intent);
                break;

            case EmployeeAction.ACTION_SMS:  
                Uri smsUri = Uri.parse("sms:" + action.getData());  
                intent = new Intent(Intent.ACTION_VIEW, smsUri); 
                startActivity(intent);
                break;

            case EmployeeAction.ACTION_REPORTS:  
                intent = new Intent(this, DirectReports.class);
                intent.putExtra("EMPLOYEE_ID", employeeId);
                startActivity(intent);
                break;

            case EmployeeAction.ACTION_VIEW:  
                intent = new Intent(this, EmployeeDetails.class);
                intent.putExtra("EMPLOYEE_ID", managerId);
                startActivity(intent);
                break;
        }
    }    

    class EmployeeActionAdapter extends ArrayAdapter<EmployeeAction> {

        EmployeeActionAdapter() {
            super(EmployeeDetails.this, R.layout.action_list_item, actions);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            EmployeeAction action = actions.get(position);
            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(R.layout.action_list_item, parent, false);
            TextView label = (TextView) view.findViewById(R.id.label);
            label.setText(action.getLabel());
            TextView data = (TextView) view.findViewById(R.id.data);
            data.setText(action.getData());
            return view;
        }

    }

}

Following is the code of the corresponding xml file:

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

<LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#505050"
    android:padding="8px">
    <TextView
        android:id="@+id/employeeName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:textSize="50px"/>
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"/>

</LinearLayout>

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

I need to have a different background color for some fields, like availability or tehnical skills. I need your help on how to change it?

user2473631
  • 33
  • 1
  • 7
  • you can just use `android:background="#FFFFFF"` (set your desired color here) attribute in your xml element. – SSS Jul 11 '14 at 07:59
  • I cannot, see the xml code. Doing this will change the background of all the fields in the listview. I want to change the color of specific fields only. – user2473631 Jul 11 '14 at 08:05
  • Have you tried something like this `yourTextview.setBackgroundResource(R.color.yourcolor)` ? – SSS Jul 11 '14 at 08:17
  • I know how to change the background color of items I have in Textview. I want to change the background color of availability field, which is there in the listview. Thanks – user2473631 Jul 11 '14 at 08:23

2 Answers2

0

Put a global flag (Or check the values before display) and do the change accordingly -- there must be a value depending on which your visibility changes? So before you set a background to this view just check that field.

Say that fiels is int status;

if(status == 1){

View.setBackgroundresource(R.drawable.image1);
}else{
View.setBackgroundresource(R.drawable.image2);
}

Here is a sample cursor adapter class that demos the fetching of records and displaying them in a list depending on values different strings are displayed:

private class CurAdapter extends CursorAdapter{


        boolean imageExists;


        public CurAdapter(Context context, Cursor c, int flags) {
            super(context, c, flags);

        }


        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            TextView tv = (TextView) view.findViewById(R.id.textView1);
            TextView tv1 = (TextView) view.findViewById(R.id.textView2);
            ImageView iv = (ImageView) view.findViewById(R.id.imageView1);


            //  String isAdhoc = (cursor.getString(cursor.getColumnIndexOrThrow("isAdhoc")));
            String name = (cursor.getString(cursor.getColumnIndexOrThrow("Name")));
            String image = cursor.getString(cursor.getColumnIndexOrThrow("imageUri"));
            String manuplate = dateConvert(cursor.getString(cursor.getColumnIndexOrThrow("Date")));


            //  if(isAdhoc.equals("1")){

            tv.setText(name);


            if(manuplate.contains("0001")){
                manuplate = manuplate.replace("0001", "");
            }
            tv1.setText((manuplate));
            tv.setTypeface(tf, Typeface.BOLD);
            tv1.setTypeface(tf1); 

            //  System.out.println("Image Uri is:"+image);


            if(image.contains("jpg") || image.contains("png")){
                File f = new File(image);
                Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
                iv.setImageBitmap(bmp); 




            }else{
                Uri IMAGE_URI = null;
                IMAGE_URI = Uri.parse(image);
                IMAGE_URI= Uri.withAppendedPath(IMAGE_URI, Contacts.Photo.CONTENT_DIRECTORY);
                try{
                    iv.setImageURI(IMAGE_URI);
                    iv.setScaleType(ScaleType.CENTER_CROP); 


                }catch (Exception e){
                    imageExists = false; 

                }
            }
            if (!imageExists){

                iv.setBackgroundResource(R.drawable.default_img);
                iv.setScaleType(ScaleType.CENTER_CROP); 
            }
        }
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            View view = LayoutInflater.from(context).inflate(R.layout.contacts_list, null);

            return view;

        }

    }
Skynet
  • 7,820
  • 5
  • 44
  • 80
  • Can you be a bit more specific? If I need to change the background of availability field, what changes should I do in my xml file? – user2473631 Jul 11 '14 at 07:58
  • I have shown the java code as well. So, if you could please guide me on what exactly to change in the code, I would be very grateful. I want to set the background to say, blue color. I am very new to android. Thanks! – user2473631 Jul 11 '14 at 08:03
  • Use something like this: employeeNameText.setBackgroundColor(getResources().getColor(R.color.blue)); – mlethys Jul 11 '14 at 08:11
  • By the way, when I use setBackgroundResource, it get the error "Cannot make a static reference to the non-static method setBackgroundResource(int) from the type View" – user2473631 Jul 11 '14 at 08:13
  • @mlethys I know how to change the background color of items I have in Textview. I want to change the background color of availability field. Thanks – user2473631 Jul 11 '14 at 08:22
  • Check [this](http://stackoverflow.com/questions/4969171/cannot-make-static-reference-to-non-static-method) for the above error you get. – Skynet Jul 11 '14 at 08:22
  • I can't run through all that code, it would be time consuming. The essential part here is to understand the logic of how to do it. That logic I have provided you in the answer :) If your availability == -1 then its not available ~~> Set one resource, if availability != -1 ~~> its available - set a different resource. – Skynet Jul 11 '14 at 08:23
  • ALso it is suggestible that you use a Cursor Adapter -- will make life much easier. – Skynet Jul 11 '14 at 08:33
  • The availability is a string, booked or free. What should I do for a string? – user2473631 Jul 11 '14 at 08:35
  • if(availability.equalsIgnoreCase("booked"){}else{} – Skynet Jul 11 '14 at 08:40
0

If you need to change background of different item inside your listview maybe you can create several "list_item" layout, and depending on your data you will inflate the according layout.

axl coder
  • 739
  • 3
  • 19