ListView with fragment button

0

I have a problem ... I load the list but the button event does not work.

fragment_pedido_consultado.xml

<FrameLayout 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"
tools:context="com.example.hernan.apptesis.infoUser.pedidoConsultado">

<!-- TODO: Update blank fragment layout -->

<ListView
    android:id="@+id/ListServices"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Now: listview_solicitudes.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:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/textView24"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="18dp"
    android:layout_marginTop="20dp"
    android:text="Estado:"
    android:textStyle="bold" />

<TextView
    android:id="@+id/estado"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/textView24"
    android:layout_marginStart="20dp"
    android:layout_toEndOf="@+id/textView24"
    android:text="TextView" />

<TextView
    android:id="@+id/textView26"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/estado"
    android:layout_marginStart="45dp"
    android:layout_toEndOf="@+id/estado"
    android:text="Ticket:"
    android:textStyle="bold" />

<TextView
    android:id="@+id/ticket"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/textView26"
    android:layout_marginStart="25dp"
    android:layout_toEndOf="@+id/textView26"
    android:text="TextView" />

<TextView
    android:id="@+id/textView29"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/textView24"
    android:layout_below="@+id/textView24"
    android:layout_marginTop="21dp"
    android:text="Dispositivo:"
    android:textStyle="bold" />

<TextView
    android:id="@+id/tipoDispositivos"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/textView29"
    android:layout_marginStart="21dp"
    android:layout_toEndOf="@+id/textView29"
    android:text="TextView" />

<TextView
    android:id="@+id/Soporte:"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/textView29"
    android:layout_below="@+id/textView29"
    android:layout_marginTop="10dp"
    android:text="Soporte:"
    android:textStyle="bold" />

<TextView
    android:id="@+id/tipoSoportes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/tipoDispositivos"
    android:layout_alignTop="@+id/Soporte:"
    android:text="TextView" />

<TextView
    android:id="@+id/textView33"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/Soporte:"
    android:layout_below="@+id/Soporte:"
    android:layout_marginTop="11dp"
    android:text="Fecha:"
    android:textStyle="bold" />

<TextView
    android:id="@+id/fecha"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/tipoSoportes"
    android:layout_alignTop="@+id/textView33"
    android:text="TextView" />

<Button
    android:id="@+id/removeSolicitud"
    style="@android:style/Widget.Material.Button.Borderless.Small"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/textView33"
    android:layout_below="@+id/textView33"
    android:layout_marginTop="7dp"
    android:drawableLeft="@drawable/removemini"
    android:text="Cancelar"
    android:textAppearance="@style/TextAppearance.AppCompat.Small"
    tools:targetApi="lollipop" />

Lastly asked for.java

public class pedidoConsultado extends Fragment {

private ListView listado;
ArrayList<HashMap<String, String>> contactList;
Button remove;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_pedido_consultado, container, false);

    contactList = new ArrayList<>();

    listado = (ListView) view.findViewById(R.id.ListServices);

    ProgressDialog dialog = new ProgressDialog(getActivity());
    new cargarServicios(dialog).execute();

    return view;
}//fin onCreateView

public class cargarServicios extends AsyncTask<Void,Void,String>{

    ProgressDialog dialog;

    public cargarServicios(ProgressDialog dialog) {
        super();
        this.dialog = dialog;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog.setTitle("Procesando...");
        dialog.setMessage("Un momento...");
        dialog.show();
    }

    @Override
    protected String doInBackground(Void... params) {
        String respuestaPost;

        String ID = obtenerIdentificador();

        httpHandler handler = new httpHandler();

        respuestaPost = handler.cargarServicio(ID);

        return respuestaPost;

    }//fin doInBackground

    @Override
    protected void onPostExecute(String response) {
        super.onPostExecute(response);

        String[] data = obtDatosJSON(response);
        CargaLista(data);

        dialog.dismiss();

    }
}//fin class cargarServicios

public void CargaLista(String[] services ){

    try{

        AdapterServicios adapter = new AdapterServicios(getActivity(), services);

        listado.setAdapter(adapter);

    } catch (Exception e){
        Log.i("Error en adapter: ",e.getMessage());
    }


}//fin CargarLista

public String[] obtDatosJSON (String response){

    String [] listaServicios = null;

    try{

        JSONArray jsonArray = new JSONArray(response);

        for (int i=0; i<= jsonArray.length(); i++){

            String estado           = jsonArray.getJSONObject(i).getString("estado");
            String ticket           = jsonArray.getJSONObject(i).getString("ticket");
            String tipoSoportes     = jsonArray.getJSONObject(i).getString("tipoSoportes");
            String tipoDispositivos = jsonArray.getJSONObject(i).getString("tipoDispositivos");
            String fecha            = jsonArray.getJSONObject(i).getString("fecha");

            listaServicios = new String[]{ticket,estado,tipoSoportes,tipoDispositivos,fecha};

        }//fin for

    } catch (Exception e){
        e.printStackTrace();
    }

    return listaServicios;
}

private String obtenerIdentificador(){

    SharedPreferences settings = this.getActivity().getSharedPreferences("testApp",0);

    String ID = settings.getString("id","0");

    return ID;

}//obtenerIdentificador

}//fin pedidoConsultado

Now AdapterServices.java

public class AdapterServicios extends BaseAdapter {

private Activity activity;
private String[] items;
private static LayoutInflater inflater= null;

public AdapterServicios(Activity activity, String[] items){
    this.activity = activity;
    this.items = items;
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}//fin constructor

@Override
public int getCount() {

    return items.length;
}

@Override
public Object getItem(int position) {

    return position;
}

@Override
public long getItemId(int position) {

    return position;
}

public class Holder {
    TextView vTicket;
    TextView vEstado;
    TextView vTipoSoportes;
    TextView vTipoDispositivos;
    TextView vFecha;
    Button vRemove;
}

@Override
public View getView(final int position, final View convertView, ViewGroup parent) {

    Holder holder = new Holder();

    View fila;

    fila = inflater.inflate(R.layout.listview_solicitudes, null);

    holder.vTicket = (TextView) fila.findViewById(R.id.ticket);
    holder.vEstado = (TextView) fila.findViewById(R.id.estado);
    holder.vTipoSoportes = (TextView) fila.findViewById(R.id.tipoSoportes);
    holder.vTipoDispositivos = (TextView) fila.findViewById(R.id.tipoDispositivos);
    holder.vFecha = (TextView) fila.findViewById(R.id.fecha);
    holder.vRemove = (Button) fila.findViewById(R.id.removeSolicitud);

    holder.vTicket.setText(items[0]);
    holder.vEstado.setText(items[1]);
    holder.vTipoSoportes.setText(items[2]);
    holder.vTipoDispositivos.setText(items[3]);
    holder.vFecha.setText(items[4]);

    holder.vRemove.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(activity,"Presiono",Toast.LENGTH_SHORT).show();
        }
    });
    fila.setTag(holder);
    return fila;

}//fin getView

}//fin class AdapterServicios
    
asked by Her Asd 28.05.2017 в 05:48
source

1 answer

0

Assuming that this button is in each listview item, it seems to me that you are going to have to write your own adapter instead of using SimpleAdapter.

On the adapter, doing the override of getView() , where the item in the list is inflated, you look for the button of the item with view.findViewById() and assign the event handler with setOnClickListener(new View.OnClickListener(){...})

By doing so, each button that is inflated in its corresponding item is left with its own click handler.

EDIT 1

Here is an example simple getView () where you see the setTag (holder). You can also see how it reuses views already instantiated using the convertview.

EDIT 2

A simple example of what we were seeing. Notice that the click on each button tells you which button you touched 1, 2, or 3.

Main Activity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;

    public class MainActivity extends AppCompatActivity {

        ListView listview;
        MyAdapter listAdapter;
        ArrayList<MiData> data;


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

            data = new ArrayList<>();
            data.add(new MiData("AAAA"));
            data.add(new MiData("BBBB"));
            data.add(new MiData("CCCC"));
            data.add(new MiData("DDDD"));

            listview = (ListView) findViewById(R.id.listView);
            listAdapter = new MyAdapter(this, data);
            listview.setAdapter(listAdapter);
        }

    }

MyAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;


public class MyAdapter extends BaseAdapter {

    ArrayList<MiData> data;
    Context context;

    public MyAdapter(Context ctx, ArrayList<MiData> data){
        this.data = data;
        this.context = ctx;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View convertview, ViewGroup viewGroup) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_item, viewGroup, false);

        ViewHolder vh = new ViewHolder();
        vh.tv = (TextView) view.findViewById(R.id.tvText);
        vh.tv.setText(data.get(i).dato);
        vh.btn = (Button) view.findViewById(R.id.btnClickMe);
        vh.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "Click en boton: " + i, Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }

    private class ViewHolder{
        TextView tv;
        Button btn;
    }
}

MiData.java

public class MiData {
    String dato;

    public MiData(String dato){
        this.dato = dato;
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.johnurrutia.soes_73787.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>

</android.support.constraint.ConstraintLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tvText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ejemplo de text"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>
    <Button
        android:id="@+id/btnClickMe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tvText"
        android:layout_alignParentTop="true"
        android:text="Cliqueame!"/>
</RelativeLayout>

EDIT 3

If you also need to be able to click on the item outside the button, in main activiy you add:

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this, "Click en item: " + i , Toast.LENGTH_SHORT).show();
            }
        });

and we must put the button of the item focusable:false in the xml so that the listview receives the clicks outside the button.

    
answered by 28.05.2017 / 06:17
source