我是Android编程的新手。我有一个数据库类,其中我有Name,Company,Location列。在主活动中,我有AutoCompleteTextBox和一个Button。例如,如果我在搜索框中输入SUGAR并单击搜索按钮,它应该导航到数据库,在下一页上它应显示为Name = Sugar,Company = Annapurna,Location = Panjagutta。我已经完成了我的DBHelper类和Main Activity。但我无法完成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;*/
}
My 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);
}
}
请你解决这个问题。我很震惊不知道该怎么办???
答案 0 :(得分:1)
在TejaShopActivity中,您的问题就在这里:
i.putExtra(iname, "itemName");
i.putExtra(icompany, "Company");
i.putExtra(iloc, "Loc");
你的论点是相反的。应该是:
i.putExtra("itemName", iname);
i.putExtra("Company", icompany);
i.putExtra("Loc", iloc);