Skip to main content
Aviso eliminado Atraer atención por ComunidadBot
Recompensa finalizada sin una respuesta ganadora por ComunidadBot
Aviso añadido Atraer atención por El Cóndor
Recompensa iniciada por un valor de 50 de reputación por El Cóndor
Estoy tratando de poner un ejemplo mas claro y breve
Origen Enlace
El Cóndor
  • 909
  • 4
  • 17
  • 34

Tengo 2 layouts que se cargan al mismo tiempo, en el 1er layout activity_main.xml tengo un listView y un textView, en el 2do layout single_post.xml tengo 4 textView que son los que se muestran en la listView del activity_main.xml

Quiero manipular el color del texto programaticamente de single_post.xml, intento acceder a el mediante LayoutInflater pero al parecer no funciona, con el código que tengo a continuación puedo hacer que cambie el color del texto del textView de activity_main.xml pero no logro hacer nada con el textView de single_post.xml

 public class MainActivity extends AppCompatActivity {

    JSONParser jsonParser = new JSONParser();
    ArrayList<HashMap<String, String>> employeeList;

    private ProgressDialog pDialog;
    private static final String url_tareas = "http://localhost/test/get_employee.php";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_EMPLOYEES = "employees";
    private static final String TAG_NAME = "name";
    public static final String TAG_DESIGNATION = "designation";
    JSONArray employees = null;
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("INICIA ","onCreate 1 ");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        employeeList = new ArrayList<HashMap<String, String>>();

        new MainActivity.LoadAllProducts().execute();
        list = (ListView) findViewById(R.id.listView);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        Log.d("TERMINA  ","onCreate 2 ");
    }

    class LoadAllProducts extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            Log.d("INICIA ","onPreExecute 3 ");
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
            Log.d("TERMINA ","onPreExecute 3 ");
        }

        protected String doInBackground(String... args) {
            Log.d("INICIA ","doInBackground 4 ");
            List params = new ArrayList();
            JSONObject json = jsonParser.makeHttpRequest(url_tareas, "GET", params);

            Log.d("All Employees: ", json.toString());

            try {
                int success =sDesignation= json.getInt(TAG_SUCCESS);"manager";

                if (success == 1) {
                   LayoutInflater employeesinflater = json.getJSONArray(TAG_EMPLOYEESLayoutInflater);

                    for (int i = 0; i < employees.lengthgetSystemService(); i++) {
                        JSONObject c = employeesContext.getJSONObject(iLAYOUT_INFLATER_SERVICE);
 
                        StringView nameview = cinflater.getStringinflate(TAG_NAME);
                        String designation = cR.getString(TAG_DESIGNATION);
                        Loglayout.d(""single_post,"designation1= "+designation);

                        HashMap map = new HashMap(null);

                       TextView map.putestatus_SINGLE_POST=(TAG_NAME, nameTextView);
                        mapview.putfindViewById(TAG_DESIGNATION, designation);

                        employeeListR.add(map);
                        Logid.d("INICIA ","designation2= "+designationtxt_designation_SINGLE_POST);
 
                        TextView designationN = estatus_ACTIVITY_MAIN=(TextView)findViewById(R.id.txt_designationtxt_designation_ACTIVITY_MAIN);

                        if(designationsDesignation.equals("manager")){
                            LogToast.dmakeText("ENTRA AL IF ","designation3= "+designation);
                            //CAMBIA EL COLOR DEL TEXTO designationN
                        }LogMainActivity.d("CONTINUA"this,"designation= "+designation);
                    }
                }
            } catch (JSONException e) {
              "YES "+sDesignation, eToast.printStackTrace(LENGTH_LONG);
                Log.eshow("","Error= "+e);
            }
            Logestatus_ACTIVITY_MAIN.dsetTextColor("TERMINA ","doInBackground 4 "Color.RED);
            return null;
        }

        protected void onPostExecute(String file_url) {
            Logestatus_SINGLE_POST.dsetTextColor("INICIA ","onPostExecute 5 ");

            pDialogColor.dismiss(RED);
            runOnUiThread(new Runnable() {
                public void run() }else{
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapterToast.makeText(
                            MainActivity.this,
                            employeeList,
                            R.layout.single_post,
                            new String[] {
                                    TAG_NAME,
                                    TAG_DESIGNATION,
                           "NO }"+sDesignation,
                            new int[] {
                                    RToast.idLENGTH_LONG).txt_name,show();
                                    Restatus_ACTIVITY_MAIN.idsetTextColor(Color.txt_designation,
                            }GREEN);
                    listestatus_SINGLE_POST.setAdaptersetTextColor(adapterColor.GREEN);
                }
            });
            Log.d("TERMINA ","onPostExecute 6 ");
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.user.testbd.MainActivity">
 

    <ListView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="37dp" />

    <TextView
        android:text="manager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/listView"
        android:layout_alignParentStart="true"
        android:layout_marginStart="91dp"
        android:layout_marginTop="75dp"
        android:id="@+id/txt_designation2"txt_designation_ACTIVITY_MAIN"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txt_designation2"
        android:layout_alignStart="@+id/txt_designation2"
        android:layout_marginTop="32dp"
        android:id="@+id/button" />
</RelativeLayout>

introducir la descripción de la imagen aquí

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="1dp">activity_main3">

    <TextView
        android:text="Name:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="71dp"
        android:id="@+id/textView2"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="Designation:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_alignStart="@+id/textView2"
        android:layout_marginTop="46dp"
        android:id="@+id/textView3"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="designation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView3"
        android:layout_alignStart="@+id/txt_name"
        android:id="@+id/txt_designation"txt_designation_SINGLE_POST"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="86dp"
        android:id="@+id/txt_name"
        android:textSize="18sp"
        android:textColor="@android:color/black"
        android:layout_above="@+id/textView3"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

txt_designation no lo tengo en el layot activity_main osea esta en el layout single_postintroducir la descripción de la imagen aquí

La idea es obtener el valor de un textView que se carga en otro layout pero solo puedo obtener el valor de los textView que se cargan en el layout que se indique en el método setContentView();

Esto me muestra el Logcat:

    03-02 10:25:49.879 30713-30736/com.example.user.testbd D/All Employees:: {"success":1,"employees":[{"designation":"manager","name":"rick"}]}
                                                                                    
                                                                                    [ 03-02 10:25:49.879 30713:0x7810 D/         ]
                                                                                    designation1= manager
    03-02 10:25:49.879 30713-30736/com.example.user.testbd D/INICIA: designation2= manager
    03-02 10:25:49.880 30713-30736/com.example.user.testbd D/ENTRA AL IF: designation3= manager
    03-02 10:25:49.880 30713-30736/com.example.user.testbd D/SALE DEL IF: designation= manager
    03-02 10:25:49.880 30713-30736/com.example.user.testbd D/TERMINA: doInBackground 4 
    03-02 10:25:49.891 30713-30713/com.example.user.testbd D/INICIA: onPostExecute 5 

Pero si en la parte de código del MainActivity que tengo comentada cambio el color del texto e intento hacer esto:

TextView designationN = (TextView)findViewById(R.id.txt_designation2);

    if(designation.equals("manager")){
                                Log.d("ENTRA AL IF ","designation3= "+designation);
                                //CAMBIA EL COLOR DEL TEXTO designationN
                                designationN.setTextColor(Color.RED);
                            }

introducir la descripción de la imagen aquí

Lo hago de esta forma porque en esa parte del código tengo el nuevo valor de txt_designation

public class MainActivity extends AppCompatActivity {

    JSONParser jsonParser = new JSONParser();
    ArrayList<HashMap<String, String>> employeeList;

    private ProgressDialog pDialog;
    private static final String url_tareas = "http://localhost/test/get_employee.php";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_EMPLOYEES = "employees";
    private static final String TAG_NAME = "name";
    public static final String TAG_DESIGNATION = "designation";
    JSONArray employees = null;
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("INICIA ","onCreate 1 ");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        employeeList = new ArrayList<HashMap<String, String>>();

        new MainActivity.LoadAllProducts().execute();
        list = (ListView) findViewById(R.id.listView);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        Log.d("TERMINA  ","onCreate 2 ");
    }

    class LoadAllProducts extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            Log.d("INICIA ","onPreExecute 3 ");
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
            Log.d("TERMINA ","onPreExecute 3 ");
        }

        protected String doInBackground(String... args) {
            Log.d("INICIA ","doInBackground 4 ");
            List params = new ArrayList();
            JSONObject json = jsonParser.makeHttpRequest(url_tareas, "GET", params);

            Log.d("All Employees: ", json.toString());

            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    employees = json.getJSONArray(TAG_EMPLOYEES);

                    for (int i = 0; i < employees.length(); i++) {
                        JSONObject c = employees.getJSONObject(i);
 
                        String name = c.getString(TAG_NAME);
                        String designation = c.getString(TAG_DESIGNATION);
                        Log.d("","designation1= "+designation);

                        HashMap map = new HashMap();

                        map.put(TAG_NAME, name);
                        map.put(TAG_DESIGNATION, designation);

                        employeeList.add(map);
                        Log.d("INICIA ","designation2= "+designation);
 
                        TextView designationN = (TextView)findViewById(R.id.txt_designation);

                        if(designation.equals("manager")){
                            Log.d("ENTRA AL IF ","designation3= "+designation);
                            //CAMBIA EL COLOR DEL TEXTO designationN
                        }Log.d("CONTINUA","designation= "+designation);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.e("","Error= "+e);
            }
            Log.d("TERMINA ","doInBackground 4 ");
            return null;
        }

        protected void onPostExecute(String file_url) {
            Log.d("INICIA ","onPostExecute 5 ");

            pDialog.dismiss();
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            MainActivity.this,
                            employeeList,
                            R.layout.single_post,
                            new String[] {
                                    TAG_NAME,
                                    TAG_DESIGNATION,
                            },
                            new int[] {
                                    R.id.txt_name,
                                    R.id.txt_designation,
                            });
                    list.setAdapter(adapter);
                }
            });
            Log.d("TERMINA ","onPostExecute 6 ");
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.user.testbd.MainActivity">
 

    <ListView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="37dp" />

    <TextView
        android:text="manager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/listView"
        android:layout_alignParentStart="true"
        android:layout_marginStart="91dp"
        android:layout_marginTop="75dp"
        android:id="@+id/txt_designation2"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txt_designation2"
        android:layout_alignStart="@+id/txt_designation2"
        android:layout_marginTop="32dp"
        android:id="@+id/button" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="1dp">

    <TextView
        android:text="Name:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="71dp"
        android:id="@+id/textView2"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="Designation:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_alignStart="@+id/textView2"
        android:layout_marginTop="46dp"
        android:id="@+id/textView3"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="designation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView3"
        android:layout_alignStart="@+id/txt_name"
        android:id="@+id/txt_designation"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="86dp"
        android:id="@+id/txt_name"
        android:textSize="18sp"
        android:textColor="@android:color/black"
        android:layout_above="@+id/textView3"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

txt_designation no lo tengo en el layot activity_main osea esta en el layout single_post

La idea es obtener el valor de un textView que se carga en otro layout pero solo puedo obtener el valor de los textView que se cargan en el layout que se indique en el método setContentView();

Esto me muestra el Logcat:

    03-02 10:25:49.879 30713-30736/com.example.user.testbd D/All Employees:: {"success":1,"employees":[{"designation":"manager","name":"rick"}]}
                                                                                    
                                                                                    [ 03-02 10:25:49.879 30713:0x7810 D/         ]
                                                                                    designation1= manager
    03-02 10:25:49.879 30713-30736/com.example.user.testbd D/INICIA: designation2= manager
    03-02 10:25:49.880 30713-30736/com.example.user.testbd D/ENTRA AL IF: designation3= manager
    03-02 10:25:49.880 30713-30736/com.example.user.testbd D/SALE DEL IF: designation= manager
    03-02 10:25:49.880 30713-30736/com.example.user.testbd D/TERMINA: doInBackground 4 
    03-02 10:25:49.891 30713-30713/com.example.user.testbd D/INICIA: onPostExecute 5 

Pero si en la parte de código del MainActivity que tengo comentada cambio el color del texto e intento hacer esto:

TextView designationN = (TextView)findViewById(R.id.txt_designation2);

    if(designation.equals("manager")){
                                Log.d("ENTRA AL IF ","designation3= "+designation);
                                //CAMBIA EL COLOR DEL TEXTO designationN
                                designationN.setTextColor(Color.RED);
                            }

introducir la descripción de la imagen aquí

Lo hago de esta forma porque en esa parte del código tengo el nuevo valor de txt_designation

Tengo 2 layouts que se cargan al mismo tiempo, en el 1er layout activity_main.xml tengo un listView y un textView, en el 2do layout single_post.xml tengo 4 textView que son los que se muestran en la listView del activity_main.xml

Quiero manipular el color del texto programaticamente de single_post.xml, intento acceder a el mediante LayoutInflater pero al parecer no funciona, con el código que tengo a continuación puedo hacer que cambie el color del texto del textView de activity_main.xml pero no logro hacer nada con el textView de single_post.xml

 public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String sDesignation= "manager";

        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.single_post, null);

        TextView estatus_SINGLE_POST=(TextView)view.findViewById(R.id.txt_designation_SINGLE_POST);
        TextView estatus_ACTIVITY_MAIN=(TextView)findViewById(R.id.txt_designation_ACTIVITY_MAIN);

            if(sDesignation.equals("manager")){
                   Toast.makeText(MainActivity.this, "YES "+sDesignation, Toast.LENGTH_LONG).show();
                   estatus_ACTIVITY_MAIN.setTextColor(Color.RED);
                   estatus_SINGLE_POST.setTextColor(Color.RED);
            }else{
                Toast.makeText(MainActivity.this, "NO "+sDesignation, Toast.LENGTH_LONG).show();
                estatus_ACTIVITY_MAIN.setTextColor(Color.GREEN);
                estatus_SINGLE_POST.setTextColor(Color.GREEN);
            }
          }
        }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    tools:context="com.example.user.testbd.MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="37dp" />

    <TextView
        android:text="manager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/listView"
        android:layout_alignParentStart="true"
        android:layout_marginStart="91dp"
        android:layout_marginTop="75dp"
        android:id="@+id/txt_designation_ACTIVITY_MAIN"
        android:textSize="18sp"
        android:textColor="@android:color/black" />    
</RelativeLayout>

introducir la descripción de la imagen aquí

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main3">

    <TextView
        android:text="Name:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="71dp"
        android:id="@+id/textView2"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="Designation:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_alignStart="@+id/textView2"
        android:layout_marginTop="46dp"
        android:id="@+id/textView3"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="designation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView3"
        android:layout_alignStart="@+id/txt_name"
        android:id="@+id/txt_designation_SINGLE_POST"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="86dp"
        android:id="@+id/txt_name"
        android:textSize="18sp"
        android:textColor="@android:color/black"
        android:layout_above="@+id/textView3"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

introducir la descripción de la imagen aquí

La idea es obtener el valor de un textView que se carga en otro layout pero solo puedo obtener el valor de los textView que se cargan en el layout que se indique en el método setContentView();

se eliminaron 2 caracteres en el cuerpo
Origen Enlace
El Cóndor
  • 909
  • 4
  • 17
  • 34
public class MainActivity extends AppCompatActivity {

    JSONParser jsonParser = new JSONParser();
    ArrayList<HashMap<String, String>> employeeList;

    private ProgressDialog pDialog;
    private static final String url_tareas = "http://10.34.84.97localhost/test/get_employee.php";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_EMPLOYEES = "employees";
    private static final String TAG_NAME = "name";
    public static final String TAG_DESIGNATION = "designation";
    JSONArray employees = null;
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("INICIA ","onCreate 1 ");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        employeeList = new ArrayList<HashMap<String, String>>();

        new MainActivity.LoadAllProducts().execute();
        list = (ListView) findViewById(R.id.listView);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        Log.d("TERMINA  ","onCreate 2 ");
    }

    class LoadAllProducts extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            Log.d("INICIA ","onPreExecute 3 ");
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
            Log.d("TERMINA ","onPreExecute 3 ");
        }

        protected String doInBackground(String... args) {
            Log.d("INICIA ","doInBackground 4 ");
            List params = new ArrayList();
            JSONObject json = jsonParser.makeHttpRequest(url_tareas, "GET", params);

            Log.d("All Employees: ", json.toString());

            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    employees = json.getJSONArray(TAG_EMPLOYEES);

                    for (int i = 0; i < employees.length(); i++) {
                        JSONObject c = employees.getJSONObject(i);

                        String name = c.getString(TAG_NAME);
                        String designation = c.getString(TAG_DESIGNATION);
                        Log.d("","designation1= "+designation);

                        HashMap map = new HashMap();

                        map.put(TAG_NAME, name);
                        map.put(TAG_DESIGNATION, designation);

                        employeeList.add(map);
                        Log.d("INICIA ","designation2= "+designation);

                        TextView designationN = (TextView)findViewById(R.id.txt_designation);

                        if(designation.equals("manager")){
                            Log.d("ENTRA AL IF ","designation3= "+designation);
                            //CAMBIA EL COLOR DEL TEXTO designationN
                        }Log.d("CONTINUA","designation= "+designation);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.e("","Error= "+e);
            }
            Log.d("TERMINA ","doInBackground 4 ");
            return null;
        }

        protected void onPostExecute(String file_url) {
            Log.d("INICIA ","onPostExecute 5 ");

            pDialog.dismiss();
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            MainActivity.this,
                            employeeList,
                            R.layout.single_post,
                            new String[] {
                                    TAG_NAME,
                                    TAG_DESIGNATION,
                            },
                            new int[] {
                                    R.id.txt_name,
                                    R.id.txt_designation,
                            });
                    list.setAdapter(adapter);
                }
            });
            Log.d("TERMINA ","onPostExecute 6 ");
}
}
}
public class MainActivity extends AppCompatActivity {

    JSONParser jsonParser = new JSONParser();
    ArrayList<HashMap<String, String>> employeeList;

    private ProgressDialog pDialog;
    private static final String url_tareas = "http://10.34.84.97/test/get_employee.php";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_EMPLOYEES = "employees";
    private static final String TAG_NAME = "name";
    public static final String TAG_DESIGNATION = "designation";
    JSONArray employees = null;
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("INICIA ","onCreate 1 ");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        employeeList = new ArrayList<HashMap<String, String>>();

        new MainActivity.LoadAllProducts().execute();
        list = (ListView) findViewById(R.id.listView);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        Log.d("TERMINA  ","onCreate 2 ");
    }

    class LoadAllProducts extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            Log.d("INICIA ","onPreExecute 3 ");
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
            Log.d("TERMINA ","onPreExecute 3 ");
        }

        protected String doInBackground(String... args) {
            Log.d("INICIA ","doInBackground 4 ");
            List params = new ArrayList();
            JSONObject json = jsonParser.makeHttpRequest(url_tareas, "GET", params);

            Log.d("All Employees: ", json.toString());

            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    employees = json.getJSONArray(TAG_EMPLOYEES);

                    for (int i = 0; i < employees.length(); i++) {
                        JSONObject c = employees.getJSONObject(i);

                        String name = c.getString(TAG_NAME);
                        String designation = c.getString(TAG_DESIGNATION);
                        Log.d("","designation1= "+designation);

                        HashMap map = new HashMap();

                        map.put(TAG_NAME, name);
                        map.put(TAG_DESIGNATION, designation);

                        employeeList.add(map);
                        Log.d("INICIA ","designation2= "+designation);

                        TextView designationN = (TextView)findViewById(R.id.txt_designation);

                        if(designation.equals("manager")){
                            Log.d("ENTRA AL IF ","designation3= "+designation);
                            //CAMBIA EL COLOR DEL TEXTO designationN
                        }Log.d("CONTINUA","designation= "+designation);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.e("","Error= "+e);
            }
            Log.d("TERMINA ","doInBackground 4 ");
            return null;
        }

        protected void onPostExecute(String file_url) {
            Log.d("INICIA ","onPostExecute 5 ");

            pDialog.dismiss();
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            MainActivity.this,
                            employeeList,
                            R.layout.single_post,
                            new String[] {
                                    TAG_NAME,
                                    TAG_DESIGNATION,
                            },
                            new int[] {
                                    R.id.txt_name,
                                    R.id.txt_designation,
                            });
                    list.setAdapter(adapter);
                }
            });
            Log.d("TERMINA ","onPostExecute 6 ");
}
}
}
public class MainActivity extends AppCompatActivity {

    JSONParser jsonParser = new JSONParser();
    ArrayList<HashMap<String, String>> employeeList;

    private ProgressDialog pDialog;
    private static final String url_tareas = "http://localhost/test/get_employee.php";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_EMPLOYEES = "employees";
    private static final String TAG_NAME = "name";
    public static final String TAG_DESIGNATION = "designation";
    JSONArray employees = null;
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("INICIA ","onCreate 1 ");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        employeeList = new ArrayList<HashMap<String, String>>();

        new MainActivity.LoadAllProducts().execute();
        list = (ListView) findViewById(R.id.listView);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        Log.d("TERMINA  ","onCreate 2 ");
    }

    class LoadAllProducts extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            Log.d("INICIA ","onPreExecute 3 ");
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
            Log.d("TERMINA ","onPreExecute 3 ");
        }

        protected String doInBackground(String... args) {
            Log.d("INICIA ","doInBackground 4 ");
            List params = new ArrayList();
            JSONObject json = jsonParser.makeHttpRequest(url_tareas, "GET", params);

            Log.d("All Employees: ", json.toString());

            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    employees = json.getJSONArray(TAG_EMPLOYEES);

                    for (int i = 0; i < employees.length(); i++) {
                        JSONObject c = employees.getJSONObject(i);

                        String name = c.getString(TAG_NAME);
                        String designation = c.getString(TAG_DESIGNATION);
                        Log.d("","designation1= "+designation);

                        HashMap map = new HashMap();

                        map.put(TAG_NAME, name);
                        map.put(TAG_DESIGNATION, designation);

                        employeeList.add(map);
                        Log.d("INICIA ","designation2= "+designation);

                        TextView designationN = (TextView)findViewById(R.id.txt_designation);

                        if(designation.equals("manager")){
                            Log.d("ENTRA AL IF ","designation3= "+designation);
                            //CAMBIA EL COLOR DEL TEXTO designationN
                        }Log.d("CONTINUA","designation= "+designation);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.e("","Error= "+e);
            }
            Log.d("TERMINA ","doInBackground 4 ");
            return null;
        }

        protected void onPostExecute(String file_url) {
            Log.d("INICIA ","onPostExecute 5 ");

            pDialog.dismiss();
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            MainActivity.this,
                            employeeList,
                            R.layout.single_post,
                            new String[] {
                                    TAG_NAME,
                                    TAG_DESIGNATION,
                            },
                            new int[] {
                                    R.id.txt_name,
                                    R.id.txt_designation,
                            });
                    list.setAdapter(adapter);
                }
            });
            Log.d("TERMINA ","onPostExecute 6 ");
}
}
}
se añadieron 10153 caracteres en el cuerpo
Origen Enlace
El Cóndor
  • 909
  • 4
  • 17
  • 34

UPDATE

En el metodo onCreate() tengo lo siguiente:MainActivity.java

public class MainActivity extends AppCompatActivity {

    JSONParser jsonParser = new JSONParser();
    ArrayList<HashMap<String, String>> employeeList;

    private ProgressDialog pDialog;
    private static final String url_tareas = "http://10.34.84.97/test/get_employee.php";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_EMPLOYEES = "employees";
    private static final String TAG_NAME = "name";
    public static final String TAG_DESIGNATION = "designation";
    JSONArray employees = null;
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("INICIA ","onCreate 1 ");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        designation=employeeList = new ArrayList<HashMap<String, String>>(TextView);

        new MainActivity.LoadAllProducts().execute();
        list = (ListView) findViewById(R.id.txt_designationlistView);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        Log.d("TERMINA  ","onCreate 2 ");
    }

    class LoadAllProducts extends AsyncTask<String, String, TAG_DESIGNATIONString> {

        @Override
        protected void onPreExecute() {
            Log.d("INICIA ","onPreExecute 3 ");
            super.onPreExecute();
            pDialog = "manager";new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
            Log.d("TERMINA ","onPreExecute 3 ");
        }

        protected String doInBackground(String... args) {
            Log.d("INICIA ","doInBackground 4 ");
            List params = new ArrayList();
            JSONObject json = jsonParser.makeHttpRequest(url_tareas, "GET", params);

            Log.d("All Employees: ", json.toString());

            try {
                int success = json.getInt(TAG_SUCCESS);

                if (TAG_DESIGNATIONsuccess == 1) {
                    employees = json.equalsgetJSONArray("manager"TAG_EMPLOYEES);

                    for (int i = 0; i < employees.length(); i++) {
                Toast        JSONObject c = employees.makeTextgetJSONObject(MainActivityi);

                        String name = c.thisgetString(TAG_NAME);
                        String designation = c.getString(TAG_DESIGNATION);
                        Log.d("","designation1= "YES""+designation);

                        HashMap map = new HashMap();

                        map.put(TAG_NAME, Toastname);
                        map.LENGTH_LONGput(TAG_DESIGNATION, designation);

                        employeeList.showadd(map);
                        Log.d("INICIA ","designation2= "+designation);

                        TextView designationN = (TextView)findViewById(R.id.txt_designation); 

                        if(designation.setTextColorequals(Color"manager")){
                            Log.REDd("ENTRA AL IF ","designation3= "+designation);
                            //CAMBIA EL COLOR DEL TEXTO designationN
                        }elseLog.d("CONTINUA","designation= "+designation);
                    }
                }
            } catch (JSONException e) {
                designatione.setTextColorprintStackTrace(Color);
                Log.GREENe("","Error= "+e);
            }
            Log.d("TERMINA ","doInBackground 4 ");
            return null;
        }

        protected void onPostExecute(String file_url) {
            Log.d("INICIA ","onPostExecute 5 ");

            pDialog.dismiss();
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            MainActivity.this,
                            employeeList,
                            R.layout.single_post,
                            new String[] {
                                    TAG_NAME,
                                    TAG_DESIGNATION,
                            },
                            new int[] {
                                    R.id.txt_name,
                                    R.id.txt_designation,
                            });
                    list.setAdapter(adapter);
                }
            });
            Log.d("TERMINA ","onPostExecute 6 ");
}
}
}

En el activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.user.testbd.MainActivity">


    <ListView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="37dp" />

    <TextView
        android:text="manager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/listView"
        android:layout_alignParentStart="true"
        android:layout_marginStart="91dp"
        android:layout_marginTop="75dp"
        android:id="@+id/txt_designation2"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txt_designation2"
        android:layout_alignStart="@+id/txt_designation2"
        android:layout_marginTop="32dp"
        android:id="@+id/button" />
</RelativeLayout>

En el single_post.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="1dp">

    <TextView
        android:text="Name:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="71dp"
        android:id="@+id/textView2"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="Designation:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_alignStart="@+id/textView2"
        android:layout_marginTop="46dp"
        android:id="@+id/textView3"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="designation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView3"
        android:layout_alignStart="@+id/txt_name"
        android:id="@+id/txt_designation"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="86dp"
        android:id="@+id/txt_name"
        android:textSize="18sp"
        android:textColor="@android:color/black"
        android:layout_above="@+id/textView3"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

txt_designation no lo tengo en el layot activity_main y por ello me genera el error: java.lang.NullPointerException como puedo hacer referencia al textView txt_designation que se encuentraosea esta en un Layout diferente del que envia el layout setContentView()single_post

La idea es obtener el valor de un textView que se carga en otro layout pero solo puedo obtener el valor de los textView que se cargan en el layout que se indique en el método setContentView();

Esto me muestra el Logcat:

    03-02 10:25:49.879 30713-30736/com.example.user.testbd D/All Employees:: {"success":1,"employees":[{"designation":"manager","name":"rick"}]}
                                                                                    
                                                                                    [ 03-02 10:25:49.879 30713:0x7810 D/         ]
                                                                                    designation1= manager
    03-02 10:25:49.879 30713-30736/com.example.user.testbd D/INICIA: designation2= manager
    03-02 10:25:49.880 30713-30736/com.example.user.testbd D/ENTRA AL IF: designation3= manager
    03-02 10:25:49.880 30713-30736/com.example.user.testbd D/SALE DEL IF: designation= manager
    03-02 10:25:49.880 30713-30736/com.example.user.testbd D/TERMINA: doInBackground 4 
    03-02 10:25:49.891 30713-30713/com.example.user.testbd D/INICIA: onPostExecute 5 

Pero si en la parte de código del MainActivity que tengo comentada cambio el color del texto e intento hacer esto:

TextView designationN = (TextView)findViewById(R.id.txt_designation2);

    if(designation.equals("manager")){
                                Log.d("ENTRA AL IF ","designation3= "+designation);
                                //CAMBIA EL COLOR DEL TEXTO designationN
                                designationN.setTextColor(Color.RED);
                            }

introducir la descripción de la imagen aquí

Lo hago de esta forma porque en esa parte del código tengo el nuevo valor de txt_designation

En el metodo onCreate() tengo lo siguiente:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        designation=(TextView)findViewById(R.id.txt_designation);
        String TAG_DESIGNATION = "manager";

            if(TAG_DESIGNATION.equals("manager")){
                Toast.makeText(MainActivity.this, "YES", Toast.LENGTH_LONG).show();
                designation.setTextColor(Color.RED);
            }else{
                designation.setTextColor(Color.GREEN);
            }
       }

txt_designation no lo tengo en el layot activity_main y por ello me genera el error: java.lang.NullPointerException como puedo hacer referencia al textView txt_designation que se encuentra en un Layout diferente del que envia el setContentView()

La idea es obtener el valor de un textView que se carga en otro layout pero solo puedo obtener el valor de los textView que se cargan en el layout que se indique en el método setContentView();

UPDATE

En el MainActivity.java

public class MainActivity extends AppCompatActivity {

    JSONParser jsonParser = new JSONParser();
    ArrayList<HashMap<String, String>> employeeList;

    private ProgressDialog pDialog;
    private static final String url_tareas = "http://10.34.84.97/test/get_employee.php";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_EMPLOYEES = "employees";
    private static final String TAG_NAME = "name";
    public static final String TAG_DESIGNATION = "designation";
    JSONArray employees = null;
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("INICIA ","onCreate 1 ");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        employeeList = new ArrayList<HashMap<String, String>>();

        new MainActivity.LoadAllProducts().execute();
        list = (ListView) findViewById(R.id.listView);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        Log.d("TERMINA  ","onCreate 2 ");
    }

    class LoadAllProducts extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            Log.d("INICIA ","onPreExecute 3 ");
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
            Log.d("TERMINA ","onPreExecute 3 ");
        }

        protected String doInBackground(String... args) {
            Log.d("INICIA ","doInBackground 4 ");
            List params = new ArrayList();
            JSONObject json = jsonParser.makeHttpRequest(url_tareas, "GET", params);

            Log.d("All Employees: ", json.toString());

            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    employees = json.getJSONArray(TAG_EMPLOYEES);

                    for (int i = 0; i < employees.length(); i++) {
                        JSONObject c = employees.getJSONObject(i);

                        String name = c.getString(TAG_NAME);
                        String designation = c.getString(TAG_DESIGNATION);
                        Log.d("","designation1= "+designation);

                        HashMap map = new HashMap();

                        map.put(TAG_NAME, name);
                        map.put(TAG_DESIGNATION, designation);

                        employeeList.add(map);
                        Log.d("INICIA ","designation2= "+designation);

                        TextView designationN = (TextView)findViewById(R.id.txt_designation); 

                        if(designation.equals("manager")){
                            Log.d("ENTRA AL IF ","designation3= "+designation);
                            //CAMBIA EL COLOR DEL TEXTO designationN
                        }Log.d("CONTINUA","designation= "+designation);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.e("","Error= "+e);
            }
            Log.d("TERMINA ","doInBackground 4 ");
            return null;
        }

        protected void onPostExecute(String file_url) {
            Log.d("INICIA ","onPostExecute 5 ");

            pDialog.dismiss();
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            MainActivity.this,
                            employeeList,
                            R.layout.single_post,
                            new String[] {
                                    TAG_NAME,
                                    TAG_DESIGNATION,
                            },
                            new int[] {
                                    R.id.txt_name,
                                    R.id.txt_designation,
                            });
                    list.setAdapter(adapter);
                }
            });
            Log.d("TERMINA ","onPostExecute 6 ");
}
}
}

En el activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.user.testbd.MainActivity">


    <ListView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="37dp" />

    <TextView
        android:text="manager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/listView"
        android:layout_alignParentStart="true"
        android:layout_marginStart="91dp"
        android:layout_marginTop="75dp"
        android:id="@+id/txt_designation2"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txt_designation2"
        android:layout_alignStart="@+id/txt_designation2"
        android:layout_marginTop="32dp"
        android:id="@+id/button" />
</RelativeLayout>

En el single_post.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="1dp">

    <TextView
        android:text="Name:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="71dp"
        android:id="@+id/textView2"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="Designation:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_alignStart="@+id/textView2"
        android:layout_marginTop="46dp"
        android:id="@+id/textView3"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="designation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView3"
        android:layout_alignStart="@+id/txt_name"
        android:id="@+id/txt_designation"
        android:textSize="18sp"
        android:textColor="@android:color/black" />

    <TextView
        android:text="name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="86dp"
        android:id="@+id/txt_name"
        android:textSize="18sp"
        android:textColor="@android:color/black"
        android:layout_above="@+id/textView3"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

txt_designation no lo tengo en el layot activity_main osea esta en el layout single_post

La idea es obtener el valor de un textView que se carga en otro layout pero solo puedo obtener el valor de los textView que se cargan en el layout que se indique en el método setContentView();

Esto me muestra el Logcat:

    03-02 10:25:49.879 30713-30736/com.example.user.testbd D/All Employees:: {"success":1,"employees":[{"designation":"manager","name":"rick"}]}
                                                                                    
                                                                                    [ 03-02 10:25:49.879 30713:0x7810 D/         ]
                                                                                    designation1= manager
    03-02 10:25:49.879 30713-30736/com.example.user.testbd D/INICIA: designation2= manager
    03-02 10:25:49.880 30713-30736/com.example.user.testbd D/ENTRA AL IF: designation3= manager
    03-02 10:25:49.880 30713-30736/com.example.user.testbd D/SALE DEL IF: designation= manager
    03-02 10:25:49.880 30713-30736/com.example.user.testbd D/TERMINA: doInBackground 4 
    03-02 10:25:49.891 30713-30713/com.example.user.testbd D/INICIA: onPostExecute 5 

Pero si en la parte de código del MainActivity que tengo comentada cambio el color del texto e intento hacer esto:

TextView designationN = (TextView)findViewById(R.id.txt_designation2);

    if(designation.equals("manager")){
                                Log.d("ENTRA AL IF ","designation3= "+designation);
                                //CAMBIA EL COLOR DEL TEXTO designationN
                                designationN.setTextColor(Color.RED);
                            }

introducir la descripción de la imagen aquí

Lo hago de esta forma porque en esa parte del código tengo el nuevo valor de txt_designation

se añadieron 206 caracteres en el cuerpo
Origen Enlace
El Cóndor
  • 909
  • 4
  • 17
  • 34
Loading
Origen Enlace
El Cóndor
  • 909
  • 4
  • 17
  • 34
Loading