Android - Obtener eventos de un objeto dentro de un layout anidado

 
Vista:
sin imagen de perfil

Obtener eventos de un objeto dentro de un layout anidado

Publicado por Francisco (1 intervención) el 03/03/2016 23:30:31
Primero que todo, quiero agradecer a quienes entren y traten de ayudarme,

Sin tener conocimientos de java, estoy creando una aplicación en android studio, y todo lo que he hecho a sido a través de tutoriales, pero ahora quede pegado con un tema.

Estoy haciendo un app para realizar una encuesta, Tengo un fragment asociado a un layout este contiene un numero pregunta, la pregunta y un listview para cargar las opciones de respuesta.


activity_ejecucion_encuesta.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
android:paddingLeft="60dp"
android:paddingTop="60dp"
android:paddingRight="60dp"
android:paddingBottom="30dp">


<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="N° "
android:id="@+id/textView3" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="id_pregunta"
android:id="@+id/tv_id_pregunta" />
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Pregunta: "
android:id="@+id/textView5"
android:textColor="#000000" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="pregunta"
android:id="@+id/tv_descripcion_preg"
android:textColor="#000000" />

</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lv_opciones" />
</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Siguiente >"
android:id="@+id/btSiguiente" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="&lt; Anterior"
android:id="@+id/btAnterior" />

</LinearLayout>

</LinearLayout>

el listview es cargado por otro layout ya sea respuesta simple o multiple (check o radiobuton).

y quiero poder obtener el valor o eventos cuando haga click en estos objetos. acontiniacion pego la clase asociada al layou principal y los layout con las opciones.

package minsal.pts.adened;

import android.app.Fragment;
import android.app.FragmentManager;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class EjecucionEncuesta extends Fragment {

SQLControladorEncuestas dbconeccion;


ListView listaOpc;

Cursor cursorPre;
View rootViewSimp;
View rootViewMult;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

final View rootView = inflater.inflate(R.layout.activity_ejecucion_encuesta, container, false);


super.onCreate(savedInstanceState);



dbconeccion = new SQLControladorEncuestas(rootView.getContext());
listaOpc = (ListView) rootView.findViewById(R.id.lv_opciones);

final TextView tvIdPregunta = (TextView) rootView.findViewById(R.id.tv_id_pregunta);
final TextView tvPregunta = (TextView) rootView.findViewById(R.id.tv_descripcion_preg);


try{
cursorPre = dbconeccion.traePreguntasXEncuesta(1);

if (cursorPre.moveToFirst())
{

tvIdPregunta.setText(cursorPre.getString(0));
tvPregunta.setText(cursorPre.getString(1));


Cursor cursorOpc = dbconeccion.traeOpciones(cursorPre.getInt(0));

String[] fromOpc = new String[] {"_id", "descripcion"};


int[] toOpcSimp
= new int[] {
R.id.tv_id,
R.id.rdbOpcion
};
int[] toOpcMult = new int[] {
R.id.tv_id,
R.id.chkOpcion
};

Toast.makeText(rootView.getContext(),cursorPre.getString(3),Toast.LENGTH_LONG).show();


if (cursorPre.getInt(3) == 2) {
SimpleCursorAdapter adapterOpc = new SimpleCursorAdapter(rootView.getContext(), R.layout.formato_lista_opciones_simple, cursorOpc, fromOpc, toOpcSimp);
adapterOpc.notifyDataSetChanged();
listaOpc.setAdapter(adapterOpc);
}else{
SimpleCursorAdapter adapterOpc = new SimpleCursorAdapter(rootView.getContext(), R.layout.formato_lista_opciones_multiple, cursorOpc, fromOpc, toOpcMult);
adapterOpc.notifyDataSetChanged();
listaOpc.setAdapter(adapterOpc);
}


}
}catch (Exception e){
Toast.makeText(rootView.getContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}

Button btSiguiente = (Button) rootView.findViewById(R.id.btSiguiente);

btSiguiente.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cursorPre.moveToNext())
{

tvIdPregunta.setText(cursorPre.getString(0));
tvPregunta.setText(cursorPre.getString(1));

//Toast.makeText(rootView.getContext(),cursorPre.getString(0),Toast.LENGTH_LONG).show();

Cursor cursorOpc = dbconeccion.traeOpciones(cursorPre.getInt(0));

String[] fromOpc = new String[] {"_id", "descripcion"};

int[] toOpcSimp
= new int[] {
R.id.tv_id,
R.id.rdbOpcion
};
int[] toOpcMult = new int[] {
R.id.tv_id,
R.id.chkOpcion
};
//Toast.makeText(rootView.getContext(),cursorPre.getString(3),Toast.LENGTH_LONG).show();

if (cursorPre.getInt(3) == 2) {
// SimpleCursorAdapter adapterOpc = new SimpleCursorAdapter(rootView.getContext(), R.layout.formato_lista_opciones_simple, cursorOpc, fromOpc, toOpcSimp);
//adapterOpc.notifyDataSetChanged();
//listaOpc.setAdapter(adapterOpc);

Fragment fragment = null;
fragment = new OpcionesSimples();

if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.lv_opciones, fragment).commit();

} else {
Log.e("FMP", "MainActivity - Error cuando se creo el fragment");
}

}else{
SimpleCursorAdapter adapterOpc = new SimpleCursorAdapter(rootView.getContext(), R.layout.formato_lista_opciones_multiple, cursorOpc, fromOpc, toOpcMult);
adapterOpc.notifyDataSetChanged();
listaOpc.setAdapter(adapterOpc);
}
}
}
});

Button btAnterior = (Button)rootView.findViewById(R.id.btAnterior);
btAnterior.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cursorPre.moveToPrevious())
{

tvIdPregunta.setText(cursorPre.getString(0));
tvPregunta.setText(cursorPre.getString(1));

//Toast.makeText(rootView.getContext(),cursorPre.getString(0),Toast.LENGTH_LONG).show();

Cursor cursorOpc = dbconeccion.traeOpciones(cursorPre.getInt(0));

String[] fromOpc = new String[] {"_id", "descripcion"};

int[] toOpcSimp
= new int[] {
R.id.tv_id,
R.id.rdbOpcion
};
int[] toOpcMult = new int[] {
R.id.tv_id,
R.id.chkOpcion
};
//Toast.makeText(rootView.getContext(),cursorPre.getString(3),Toast.LENGTH_LONG).show();

if (cursorPre.getInt(3) == 2) {
SimpleCursorAdapter adapterOpc = new SimpleCursorAdapter(rootView.getContext(), R.layout.formato_lista_opciones_simple, cursorOpc, fromOpc, toOpcSimp);
adapterOpc.notifyDataSetChanged();
listaOpc.setAdapter(adapterOpc);



}else{
SimpleCursorAdapter adapterOpc = new SimpleCursorAdapter(rootView.getContext(), R.layout.formato_lista_opciones_multiple, cursorOpc, fromOpc, toOpcMult);
adapterOpc.notifyDataSetChanged();
listaOpc.setAdapter(adapterOpc);
}
}
}
});
try{
RadioButton rbOpcion = (RadioButton) rootViewSimp.findViewById(R.id.rdbOpcion);
rbOpcion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(),v.toString(),Toast.LENGTH_LONG).show();
}
});

CheckBox chkOpcion = (CheckBox) rootViewMult.findViewById(R.id.chkOpcion);
chkOpcion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(),v.toString(),Toast.LENGTH_LONG).show();
}
});

}catch (Exception e){
Toast.makeText(rootView.getContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}

return rootView;
}


}

formato_lista_opciones_simple.xml

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


<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:paddingLeft="30dp">

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="opcion"
android:id="@+id/rdbOpcion"
android:checked="false"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="tv_id"
android:id="@+id/tv_id"
android:visibility="invisible" />

</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">

</LinearLayout>

</LinearLayout>


Necesito ayuda urgente saludos.

PD: perdon por el desorden del post, pero me estoy quedando sin bateria en el note, y ando sin cargador,
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder