我在将我的产品从我的SQL Light数据库中导入Spinner时遇到问题。我也使用CustomAdapter来做到这一点。当我点击旋转器时没有任何反应。
我的课程:
_CustomAdapter
class _CustomAdapter extends ArrayAdapter<MyProduct>{
private Activity context;
ArrayList<MyProduct> product;
public _CustomAdapter(Activity context, int resource, int bezID, ArrayList<MyProduct>
product) {
super(context, resource, bezID, product);
this.context = context;
this.product = product;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyProduct current = product.get(position);
TextView label = new TextView(context);
label.setTextSize(30);
label.setText(current.getBeschreibung());
return label;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View row = inflater.inflate(R.layout._spinneritem, parent, false);
MyProduct current = product.get(position);
ImageView profile = (ImageView) row.findViewById(R.id.imgProd);
profile.setBackgroundResource(current.getImage());
TextView name = (TextView) row.findViewById(R.id.bezProd);
name.setText(current.getBeschreibung());
return row;
}
}
ActivityClass
public class SortimentActivity extends Activity {
EditText preis;
EditText kommentar;
Button okB;
ProductDao pd;
Spinner spinProducts;
MyProduct selected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sortiment);
preis=(EditText) findViewById(R.id.preis);
kommentar=(EditText) findViewById(R.id.kommentar);
okB=(Button)findViewById(R.id.bOK);
this.deleteDatabase("vegi.db");
SQLiteDatabase db = new OpenHelper(this).getWritableDatabase();
ArrayList<MyProduct> products= new ArrayList<MyProduct>();
pd = new ProductDao(db);
try{
products=(ArrayList<MyProduct>)pd.getAll();
} catch(Exception e){
}
spinProducts= (Spinner) findViewById(R.id.products);
_CustomAdapter adapter = new _CustomAdapter(this,R.layout._spinneritem, R.id.bezProd, products);
spinProducts.setAdapter(adapter);
spinProducts.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
selected =(MyProduct) spinProducts.getSelectedItem();
String a = String.valueOf(selected.getPreis());
preis.setText(a);
kommentar.setText(selected.getBeschreibung());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
preis.setText("");
kommentar.setText("");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sortiment, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void okClick(View v){
double pr= Double.parseDouble(preis.getText().toString());
String kom=kommentar.getText().toString();
selected.setPreis(pr);
selected.setBeschreibung(kom);
pd.update(selected);
}
}
MyProduct类
public class MyProduct {
private long id;
private String lebensmittel;
private double preis;
private String beschreibung;
private int image;
public MyProduct()
{
this.id=0;
this.lebensmittel = "Birne";
this.preis=0;
this.beschreibung= "teuer";
this.image = 1;
}
public MyProduct(long id, String lebensmittel, double preis, String beschreibung, int image)
{
this.id=id;
this.lebensmittel = lebensmittel;
this.preis=preis;
this.beschreibung= beschreibung;
this.image = image;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getLebensmittel() {
return lebensmittel;
}
public void setLebensmittel(String lebensmittel) {
this.lebensmittel = lebensmittel;
}
public double getPreis() {
return preis;
}
public void setPreis(double preis) {
this.preis = preis;
}
public String getBeschreibung() {
return beschreibung;
}
public void setBeschreibung(String beschreibung) {
this.beschreibung = beschreibung;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}
public class ProductDao implements Dao<MyProduct>{
private SQLiteDatabase db;
private SQLiteStatement insertStatement;
private String DB_Table = "products";
private String INSERT = "insert into " + DB_Table
+ "(idi,product,price,comment,imgid)" + "values (?,?,?,?,?)";
private String SEL_ROW = "select idi,product,price,comment from "
+ DB_Table;
private String CREATE_TABLE = "create table " + DB_Table
+ "( idi long NOT NULL, " + " product String NOT NULL, "
+ " price double, " + " comment String, " + " imgid int)";
private String CREATE_INDEX = "create index if not exists " + DB_Table
+ "_ind " + "ON " + DB_Table + " (idi)";
public ProductDao(SQLiteDatabase db) {
this.db = db;
try {
insertStatement = db.compileStatement(INSERT);
Log.d("ANDRO VEGI", "Create INSERT product");
} catch (SQLException e) {
Log.d("ANDRO VEGI", "NO Database, NO INSERT product");
}
}
@Override
public long save(MyProduct product) {
insertStatement.clearBindings();
insertStatement.bindLong(1, product.getId());
// TODO !!!!
return insertStatement.executeInsert();
}
@Override
public void update(MyProduct type) {
}
@Override
public void delete(MyProduct type) {
}
@Override
public MyProduct get(long id) {
Cursor cs = db.rawQuery(SEL_ROW + " where idi=?",
new String[] { String.valueOf(id) });
if (cs.moveToFirst())
return buildProductFromCursor(cs);
else
return null;
}
@Override
public List<MyProduct> getAll() {
List <MyProduct>list= new ArrayList<MyProduct>();
// Select All Query
Cursor cursor = db.rawQuery("select idi,product,price,comment,imgid from products", null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
MyProduct product = new MyProduct();
product.setId(Integer.parseInt(cursor.getString(0)));
product.setLebensmittel(cursor.getString(1));
product.setPreis(Double.parseDouble(cursor.getString(2)));
product.setBeschreibung(cursor.getString(3));
product.setImage(Integer.parseInt(cursor.getString(4)));
// Adding contact to list
list.add(product);
} while (cursor.moveToNext());
cursor.close();
}
return list;
}
private MyProduct buildProductFromCursor(Cursor cs) {
MyProduct product = null;
if (cs == null)
Log.e("Internal Error", "no cursor");
else {
Log.v("ANDRO VEGI", toString(cs));
product = new MyProduct(cs.getLong(0), cs.getString(1),
cs.getDouble(2), cs.getString(3), cs.getInt(4));
}
return product;
}
public void createTable() {
db.execSQL(CREATE_TABLE);
db.execSQL(CREATE_INDEX);
try {
insertStatement = db.compileStatement(INSERT);
Log.d("ANDRO VEGI", "Create INSERT product");
} catch (SQLException e) {
Log.d("ANDRO VEGI", "NO Database, NO INSERT product");
}
}
public void fillTable() {
long ok = 0;
if (ok > -1)
ok = save(new MyProduct(1, "Äpfel", 0,
"neue Lieferung vom Huberbauer", R.drawable.apfel_70x70));
if (ok > -1)
ok = save(new MyProduct(2, "Birnen", 0,
"Achtung sehr süss aber überreif", R.drawable.birne_70x70));
if (ok > -1)
ok = save(new MyProduct(3, "Karotten", 0, "wie immer",
R.drawable.karotte_70x70));
if (ok > -1)
Log.d("ANDRO VEGI", "OK INSERT products ");
else
Log.d("ANDRO VEGI", "INSERT products failed");
}
String toString(Cursor cs) {
String sh = "";
try {
sh += cs.getLong(0) + "," + cs.getLong(0) + "," + cs.getString(1)
+ "," + cs.getDouble(2) + "," + cs.getString(3) + ","
+ cs.getInt(4);
} catch (Exception e) {
sh += "***,";
}
return sh;
}
}