Item RadioButton

1

If you could guide me how to achieve the following:

I have a ListView and items that show all the data of my database in SQLite, I put a Radio button FIRMA = (RadioButton) findViewById(R.id.firma); and this is repeated in each Item, it is like a form in each item and what I intend is that when you click it, insert X value to the respective column to the data shown in the item, so that when you reopen the app the radio button is already marked, therefore later you would have to condition if the cell it is empty unmarked and if not marked.

I show the idea below in an image

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     FIRMA = (RadioButton) findViewById(R.id.firma);

    //////TRASMO SQLITE QUE CONSULTA Y MUESTRA EN UN LIST VIEW /////
    dbcon = new SQLController(this);
    dbcon.open();
    lv = (ListView) findViewById(R.id.listView);

    Cursor cursor = dbcon.readData();
    String[] from = new String[] { DatabaseHelper.IDs, DatabaseHelper.MSG };
    int[] to = new int[] { R.id.ids, R.id.msg };

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(
            MainActivity.this, R.layout.list_item, cursor, from, to);

    adapter.notifyDataSetChanged();
    lv.setAdapter(adapter);
}
    
asked by JESUS ESPINOSA 21.02.2016 в 23:27
source

2 answers

2
  

save all marked items and once marked it can no longer be   possible to unmark them, I also pretend that when dialing the radio,   send the data by JSON to a B.D in MYSQL

I mentioned that to indicate the selection of several elements in a UI, checkbox , if you were to select only one item you would use radiobuttons .

This would be an example of the adapter used for your purpose:

private class MyCustomAdapter extends ArrayAdapter<Firmas> {

  private ArrayList<Firmas> FirmasList;

  public MyCustomAdapter(Context context, int textViewResourceId, 
    ArrayList<Firmas> FirmasList) {
   super(context, textViewResourceId, FirmasList);
   this.FirmasList = new ArrayList<firmas>();
   this.FirmasList.addAll(FirmasList);
  }

  private class ViewHolder {
   TextView nombre;
   CheckBox chk;
  }

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

   ViewHolder holder = null;
   Log.v("ConvertView", String.valueOf(position));

   if (convertView == null) {
   LayoutInflater vi = (LayoutInflater)getSystemService(
     Context.LAYOUT_INFLATER_SERVICE);
   convertView = vi.inflate(R.layout.Firmas_info, null);

   holder = new ViewHolder();
   holder.nombre = (TextView) convertView.findViewById(R.id.nombre);
   holder.chk = (CheckBox) convertView.findViewById(R.id.checkBox1);
   convertView.setTag(holder);

    holder.chk.setOnClickListener( new View.OnClickListener() {  
     public void onClick(View v) {  
      CheckBox cb = (CheckBox) v ;  
      Firmas Firmas = (Firmas) cb.getTag();  
      Toast.makeText(getApplicationContext(),
       "Clicked on Checkbox: " + cb.getText() +
       " is " + cb.isChecked(), 
       Toast.LENGTH_LONG).show();
      Firmas.setSelected(cb.isChecked());
     }  
    });  
   } 
   else {
    holder = (ViewHolder) convertView.getTag();
   }

   Firmas firmas = FirmasList.get(position);
   holder.nombre.setText(" (" +  Firmas.getNombre() + ")");
   holder.chk.setText(Firmas.getName());
   holder.chk.setChecked(Firmas.isSelected());
   holder.chk.setTag(Firmas);

   return convertView;

  }

 }

and this is the object used in the adapter:

public class Firmas{

 String nombre = null;
 String chkText = null;
 boolean selected = false;

 public Firmas(String nombre, String chkText, boolean selected) {
  super();
  this.nombre = nombre;
  this.chkText  = chkText ;
  this.selected = selected;
 }

 public String getNombre() {
  return nombre;
 }
 public void setNombre(String nombre) {
  this.nombre = nombre;
 }
 public String getChkText() {
  return chkText ;
 }
 public void setchk(String chkText ) {
  this.chkText  = chkText ;
 }

 public boolean isSelected() {
  return selected;
 }
 public void setSelected(boolean selected) {
  this.selected = selected;
 }

}

You have to add a field where the status of your checkbox will be saved for each record, and within

   holder.chk.setOnClickListener( new View.OnClickListener() {  

implement the save to database.

I recommend you see this example which is fully explained and has all the elements to create the example:

Android ListView Checkbox Example (English )

    
answered by 23.02.2016 / 19:59
source
1

Well, the first thing to say is that by definition when you use a RadioButton (or a group of them) is to group items that are excluded from each other (there is only a single selection). I think it would be better for you to use CheckBox

Now specifically with your question, the answer is somewhat complicated. I once did it and it went something like this:

First : In your database as you indicate, you need an indicator that will be modified when you select an item from the list.

Second : At this point I assume you have an xml where you define the components (or form) of each item on the list.

Third : Having the 2 previous elements, you must have an Object (Java class) that has the columns of your table (I see that you are not using an ORM), therefore you will have to design a method that travels your cursor and loads an array of objects of the type you're driving. For example, if I have a Products table in my BDD I must have a Product object in my model and a method like this to move from a cursor to my object model, this method should be applied for each iteration that is done in the cursor

private Producto cursorToProducto(Cursor cursor) {

    Producto p = new Producto();
    p.setId(cursor.getInt(0));
    p.setNombre(cursor.getString(1));
    p.setUnidades(cursor.getInt(2));
    p.setPrecio(cursor.getDouble(3));
    p.setImporteImpuesto(cursor.getDouble(4));
    p.setImpuestoAplicado(cursor.getDouble(5)); 
    p.setCod_impuesto(cursor.getString(6)); 
    p.setMarcadorTachado(cursor.getInt(8));
    return p;
  }

Implementation in the path of Cursor :

while (!cursor.isAfterLast()) {
    Producto p = cursorToProducto(cursor);
    //retorno es una lista tipo List<Producto>
    retorno.add(p);
    cursor.moveToNext();
}

Fourth : Instead of SimpleCursorAdapter I implemented a Adapter custom of the type of object I'm working on

public class ProductArrayAdapter extends ArrayAdapter<Producto> {
    private LayoutInflater inflater;
    private Context context;
    ...
}

This point is extensive and is the most complicated, I recommend reading this post where they explain it very well.

Fifth : Specifically in this method getView of your ArrayAdapter you must include the statements so that when the CheckBox is activated the record in the database is updated, something like this

checkBox.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        CheckBox cb = (CheckBox) v;
        Producto p = (Producto) cb.getTag();
        p.setSelected(cb.isChecked());
        //evaluar el retorno de cb.isChecked() si es cierto
        //ejecutar la rutina de update en BDD
    }
});

Likewise. right there you can execute the instructions for the cases in which the item is already selected. Disable CheckBox for example.

After all this, the ArrayAdapter itself should show you the data in an appropriate way, as long as you correctly execute your update routines based on the CheckBox

events

I hope I help you.

    
answered by 23.02.2016 в 16:15