Update fragment with Listview

Asked

Viewed 112 times

2

I intend to "refresh" a fragment (fragment30.java) that displays the records of an Sqlite table after the deletion of one of these records .

The records shall be submitted using a Listview associated with a Adapter (Myadapter.java) and I think the difficulty is that the deletion routine is in this file ...

I’ve tried everything with total failure ...

Myadapter.java

public class MyAdapter extends ArrayAdapter<programas> {

private DataBase dataBase;
private SQLiteDatabase conn;

// TABELA programas

private programas[] mInfos;

public MyAdapter(Context context, programas[] infos) {
    super(context, 0 ,infos);
    mInfos = infos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_list,null);


    // ligação das TextView a este componente

    TextView titulo = (TextView) convertView.findViewById(R.id.titulo);
    TextView descricao = (TextView) convertView.findViewById(R.id.descricao);
    ViewPager viewPager = (ViewPager) convertView.findViewById(R.id.viewpager);

    final programas in = getItem(position);


    // preenchimento das TextView

    titulo.setText(in.getTitulo());
    descricao.setText(in.getCanal()+"/"+in.getDia()+":"+in.getHora());

    titulo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            AlertDialog.Builder AlertaRes = new AlertDialog.Builder(getContext());
            AlertaRes.setTitle("CONFIRMA ELIMINAÇÃO ?");
            AlertaRes.setMessage(in.getTitulo());
            AlertaRes.setIcon(android.R.drawable.ic_delete);

            AlertaRes.setPositiveButton("SIM", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dataBase = new DataBase(getContext());
                    conn = dataBase.getWritableDatabase();

                    String whereClause = "titulo='"+ in.getTitulo() +"'";

                    conn.delete("programas",whereClause,null);
                    conn.close();



                    // CÓDIGO PARA FAZER O REFRESH 

                }
            });

            AlertaRes.setNegativeButton("NÃO", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    //Toast.makeText(getActivity().getBaseContext(),"Registo Não Gravado",Toast.LENGTH_LONG).show();

                }
            });

            AlertaRes.show();



        }

    });


    return convertView;
}

}

Fragment30.java.

public class Fragment30 extends Fragment {

private DataBase dataBase;
private SQLiteDatabase conn;
private Context mContext;


@Override
public View onCreateView (LayoutInflater li, ViewGroup vg, Bundle b) {

    mContext = getActivity();

    View rootView = li.inflate(R.layout.fragment30, vg, false);

    ListView list = (ListView) rootView.findViewById(R.id.lv_items);


    dataBase = new DataBase(getActivity());
    conn = dataBase.getReadableDatabase();

    String[] colunas = new String[]{"titulo", "tipo", "canal","dia","hora"};

    Cursor cursor = conn.query("programas", colunas, null, null, null, null, "titulo ASC");


    if (cursor.getCount() > 0) {

        int conta = 0;

        programas[] values = new programas[cursor.getCount()];

        cursor.moveToFirst();

        do {

            conta++;
            values[conta-1] = new programas(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3),cursor.getString(4));

        } while (cursor.moveToNext());

        MyAdapter adapter2 = new MyAdapter(mContext,values);
        list.setAdapter(adapter2);

    }else{

        programas[] values = new programas[1];
        values[0] = new programas("Sem registos"," "," "," "," ");

        MyAdapter adapter2 = new MyAdapter(mContext,values);

        list.setAdapter(adapter2);
    }

    return rootView;
}

}

java programs.

public class programas {

private String titulo;
private String tipo;
private String canal;
private String dia;
private String hora;
private long id;

public programas (String titulo, String tipo,String canal,String dia,String hora){

    this.titulo=titulo;
    this.tipo=tipo;
    this.canal=canal;
    this.dia=dia;
    this.hora=hora;

}


public String getTitulo() {
    return titulo;
}
public void setTitulo(String titulo) {
    this.titulo = titulo;
}
public String getTipo() {
    return tipo;
}
public void setTipo(String tipo) {
    this.tipo = tipo;
}
public String getCanal() {
    return canal;
}
public void setCanal(String canal) {
    this.canal = canal;
}
public String getDia() {
    return dia;
}
public void setDia(String dia) {
    this.dia = dia;
}

public String getHora() {
    return hora;
}
public void setHora(String hora) {
    this.hora = hora;
}

}

1 answer

2

After any changes to your Adapter data, you should call the method notifyDatasetChanged, which will update your Listview according to the changes. If this is not enough to update the data, you can also run the query again and replace the adapter (ListView.setAdapter(...)).

However, I suggest you rethink your implementation using a CursorAdapter, which is nothing more than an Adapter that reads the data directly from the cursor - and not from an array like you are doing. Index this Codepath tutorial (in English) on the subject.

Also consider using the standard ViewHolder to optimize the performance of your list. Index this article on how to do it.

  • Yes, the solution may be the "notifyDatasetChanged" method, but even so there is the difficulty of activating it within the Adapter itself : the elimination routine is within this class ... How much to run the query again as the Activity (fragment) will be aware of the deletion ? With regard to the suggestions Cursoradapter and Viewholder, I thank you very much and I will study the subject

  • Try MyAdapter.this.notifyDatasetChanged(). If you have any doubts about understanding the use of this, see this answer.

  • In case the Activity/fragment has "knowledge" (I believe the best term would be "to be notified") on the updating of the data would be necessary to use a callback. Recommend this answer to study on the subject.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.