gpt4 book ai didi

android - 我需要从数据库中获取数据并在另一个 Activity 中显示它们

转载 作者:行者123 更新时间:2023-11-30 03:35:51 25 4
gpt4 key购买 nike

我是 android 编程的新手。我有一个数据库类,其中有名称、公司、位置列。在主要 Activity 中,我有 AutoCompleteTextBox 和一个按钮。例如,如果我在搜索框中输入 SUGAR 并单击搜索按钮,它应该导航到数据库,在下一页上它应该显示为 Name=Sugar, Company=Annapurna, Location=Panjagutta。我完成了我的 DBHelper 类和主 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
}



}

主要 Activity 如下

    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);

}

}

请问你们中的任何人都可以解决这个问题。我在这里被打动了。不知道怎么办???

最佳答案

在 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);

关于android - 我需要从数据库中获取数据并在另一个 Activity 中显示它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16654035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com