我是android编程的新手。我有一个数据库类,其中有名称、公司、位置列。在主活动中,我有 AutoCompleteTextBox 和一个按钮。例如,如果我在搜索框中输入 SUGAR 并单击搜索按钮,它应该导航到数据库,在下一页上它应该显示为 Name=Sugar、Company=Annapurna、Location=Panjagutta。我完成了我的 DBHelper 类和主要活动。但我无法完成 Second.java 类,即我需要显示数据库中的值...你能帮帮我吗?我附上代码。
DBHelper 类如下
public class ProductDataBase extends SQLiteOpenHelper{
static String db_name="MY_DB";
static String TABLENAME="Ipay_ReFro_Retailer_Tablet_Details";
SQLiteDatabase sdb;
public ProductDataBase(Context context) {
super(context, db_name, null, 100);
// TODO Auto-generated constructor stub
sdb=getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table if not exists '"+TABLENAME+"'(Name text,Company text,Location text)");
}
public void Insertion(){
sdb.execSQL("insert or replace into '"+TABLENAME+"' values ('sugar','annapurna','PANJAGUTTA')");
sdb.execSQL("insert or replace into '"+TABLENAME+"' values ('salt','annapurna','ECIL')");
sdb.execSQL("insert or replace into '"+TABLENAME+"' values ('milk','heritage','BANJARA HILLS')");
}
public ArrayList<String> getItemDetails(String itemName){
ArrayList<String> itemdetails=new ArrayList<String>();
Cursor c=sdb.rawQuery("select * from '"+TABLENAME+"' where Name='"+itemName+"'", null);
if(c!=null){
while(c.moveToNext()){
String iName=c.getString(c.getColumnIndex("Name"));
String iCompany=c.getString(c.getColumnIndex("Company"));
String iLoc=c.getString(c.getColumnIndex("Location"));
itemdetails.add(iName);
itemdetails.add(iCompany);
itemdetails.add(iLoc);
}
}
return itemdetails;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
主要活动如下
public class TejaShopActivity extends Activity {String[] items= {"sugar","salt","milk"};
ProductDataBase pdb;
String itemName;
ArrayList<String> a=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final AutoCompleteTextView sp=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
Button btn=(Button)findViewById(R.id.button1);
pdb=new ProductDataBase(this);
pdb.Insertion();
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,items);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
//int id=(int) parent.getItemIdAtPosition(pos);
itemName=parent.getItemAtPosition(pos).toString();
Toast.makeText(TejaShopActivity.this,""+itemName, 10).show();
a=pdb.getItemDetails(itemName);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String iname=a.get(0);
String icompany=a.get(1);
String iloc=a.get(2);
//when you are navigating to Second.class get the values from the bundle
Intent i=new Intent(TejaShopActivity.this,Second.class);
i.putExtra(iname, "itemName");
i.putExtra(icompany, "Company");
i.putExtra(iloc, "Loc");
startActivity(i);
}
});
}
/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;*/
}
我的 Second.java 如下
public class Second extends Activity{
TextView tv1,tv2,tv3;
ProductDataBase pdb;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
tv1=(TextView)findViewById(R.id.textView1);
tv2=(TextView)findViewById(R.id.textView2);
tv3=(TextView)findViewById(R.id.textView3);
pdb=new ProductDataBase(this);
Bundle b=getIntent().getExtras();
String x=b.getString("itemName");
String y=b.getString("Company");
String z=b.getString("Loc");
tv1.setText(x);
tv2.setText(y);
tv3.setText(z);
}
}
请你们中的任何人解决这个问题。我在这里被击中了。不知道该怎么办???