0

I've one listView with multiple choice and checkbox. I get the values from listview executing a query in sqlite3. When i click a button, I need to insert the selected items in another table but i don't know how can i do it. Before doing insert i'm trying to know if it works showing one console log (Log.v). in this code there is not insert statement. Any suggestions? Thanks in advance and sorry about my english,

Alex.

I paste the code:

public class productos extends Activity  {
    SQLiteDatabase db;
Spinner prodSpinner;
ArrayAdapter<String> prodAdapter; 

Spinner catSpinner;
ArrayAdapter<String> catAdapter;
Cursor catCursor;

Cursor prodCursor;
String Value2;
String valor2;
SparseBooleanArray sp;   

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

    Bundle extras = getIntent().getExtras();
    //final String Value = extras.getSerializable("Lista").toString();
    //final String Value2 = extras.getSerializable("Super").toString();
    Button CatText2 = (Button) findViewById(R.id.textCategoria);
    CatText2.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        Log.v("valor4:", "AAAAA");
        recCatSpinner();
        } 
  });
    Button btSelecciona = (Button) findViewById(R.id.btSelecciona);
    btSelecciona.setOnClickListener(new OnClickListener() {
    public void onClick (View arg0) {
         Toast.makeText(getBaseContext(),"AAAA",Toast.LENGTH_SHORT).show();
    }
    });  

    Button btComanda = (Button) findViewById(R.id.btComanda);
    btComanda.setOnClickListener(new OnClickListener() {
    public void onClick (View arg0) {
        EscriuComanda();
    }

    private void EscriuComanda() {
            final ListView prodSpinner = (ListView) findViewById(R.id.spProductes);
            int count = 0;
            //
            sp = new SparseBooleanArray();
            //SparseBooleanArray sp=prodSpinner.getCheckedItemPositions();
            sp.clear();
            sp = prodSpinner.getCheckedItemPositions();
             for(int i = 0; i < sp.size(); i++)
             {
                        if ( sp.valueAt(i)==true)
                        {
                            Log.v("400", "SI: " + valor2); 
                        }
                else
                {
                     Log.v("500", "No: " + valor2);
                }
             }
        }   
    });  
//Toast.makeText(getBaseContext(),Value2,Toast.LENGTH_SHORT).show();
    recCatSpinner();            
}
public class UsuariosSQLiteHelper extends SQLiteOpenHelper {
    public UsuariosSQLiteHelper(Context contexto, String nombre,
                               CursorFactory factory, int version) {
        super(contexto, nombre, factory, version);
    }
    public void onCreate(SQLiteDatabase db) {
        Log.v("OnClickV", "1");
     }
    public void onUpgrade(SQLiteDatabase db, int versionAnterior, int versionNueva) {

        Log.v("OnClickV", "1");
     }
    }
public Cursor recuperaCategoria()
    {
final UsuariosSQLiteHelper usdbh =new UsuariosSQLiteHelper(this, "DBLlistaCompra", null, 1);
final SQLiteDatabase db = usdbh.getWritableDatabase();
String tableName = "Categorias";
String[] columns = {"_id","Nombre"};


 return db.query(tableName, columns, null, null, null, null, null);
    }

public Cursor recuperaProductos()
{
    final UsuariosSQLiteHelper usdbh =new UsuariosSQLiteHelper(this, "DBLlistaCompra", null, 1);
    final SQLiteDatabase db = usdbh.getWritableDatabase();
    String tableName = "ArtSuperV";
    String[] columns = {"_id","NombreA"};
    String where = "NombreC='" + valor2 + "'";

    return db.query(tableName, columns, where, null, null, null, null);
}
public void recCatSpinner() {
        final ListView prodSpinner = (ListView) findViewById(R.id.spProductes);

        catCursor = recuperaCategoria();
        catCursor.moveToPosition(1);
        catAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);  //.simple_list_item_multiple_choice);// .simple_spinner_item); 
        catAdapter.setDropDownViewResource (android.R.layout.simple_list_item_multiple_choice); //.simple_spinner_dropdown_item);
        prodSpinner.setAdapter(catAdapter);
        if (catCursor.moveToFirst()) { 
          do { 
             catAdapter.add(catCursor.getString(1)); 
         } 
          while (catCursor.moveToNext()); 
          if (db != null) { 
          Toast.makeText(getBaseContext(),catCursor.getString(1),Toast.LENGTH_SHORT).show();
          db.close(); 
          } 
        } 
        startManagingCursor(catCursor);
        catCursor.close();
        prodSpinner.setOnItemClickListener(
        new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent,
                View view2, int pos, long id) {
                valor2 = parent.getItemAtPosition(pos).toString();
                Toast.makeText(getBaseContext(),valor2,Toast.LENGTH_SHORT).show();
                Log.v("valor2:", valor2);
                recProdSpinner(); 
                }
        });
}
        public void recProdSpinner() {
            final ListView prodSpinner = (ListView) findViewById(R.id.spProductes);

            prodCursor = recuperaProductos();
            prodCursor.moveToPosition(1);
            prodAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice);  //.simple_list_item_multiple_choice);// .simple_spinner_item); 
            prodSpinner.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
            prodAdapter.setDropDownViewResource (android.R.layout.simple_list_item_multiple_choice); //.simple_spinner_dropdown_item);
            prodSpinner.setAdapter(prodAdapter);
            if (prodCursor.moveToFirst()) { 
              do { 
                 prodAdapter.add(prodCursor.getString(1));
                 } 
              while (prodCursor.moveToNext()); 
              if (db != null) { 
              Toast.makeText(getBaseContext(),prodCursor.getString(1),Toast.LENGTH_SHORT).show();
              db.close(); 
              } 
            } 
            startManagingCursor(prodCursor);
            prodCursor.close();
            prodSpinner.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent,
                    View view2, int pos, long id) {
                    valor2 = parent.getItemAtPosition(pos).toString();
                    Toast.makeText(getBaseContext(),valor2,Toast.LENGTH_SHORT).show();
                    Log.v("valor2:", valor2);

                }
            });

        }

}

1 Answers1

1

Cant figure out some of the code flow due to language issue.
But scheme should be:
1. Set the adapters for both the lists (even if the lists are empty on launch, set the adapter with the empty but initialised array lists and make the array list as the global variables of the class so that they can be accessed from anywhere in the activity.)
2. Now select the items from the list1 and get their index in the first list.
3. Add those items in the second array list.
4. Call the "notifyDataSetChanged()" for the adapter of the second list view.

Link to know about proper use of "notifyDataSetChanged()" are
notifyDataSetChanged example

Community
  • 1
  • 1
akkilis
  • 1,904
  • 14
  • 21