从listView多项选择中插入选中的值

时间:2012-01-27 12:22:04

标签: android

我有一个带有多项选择和复选框的listView。 我从listview执行sqlite3中的查询获取值。 当我单击一个按钮时,我需要在另一个表中插入所选项目,但我不知道我该怎么做。 在插入之前,我试图知道它是否正常显示一个控制台日志(Log.v)。在这段代码中没有insert语句。 有什么建议?在此先感谢并抱歉我的英语,

亚历。

我粘贴代码:

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 个答案:

答案 0 :(得分:1)

由于语言问题,无法找出一些代码流。
但方案应该是:1。为两个列表设置适配器(即使列表在启动时为空,使用空的但初始化的数组列表设置适配器,并将数组列表作为类的全局变量,以便可以从活动的任何位置访问它们。)
2。现在从list1中选择项目并在第一个列表中获取它们的索引。在第二个数组列表中添加这些项。
4。为第二个列表视图的适配器调用“notifyDataSetChanged()”。

了解正确使用“notifyDataSetChanged()”的链接是notifyDataSetChanged example