Android 我需要从数据库中获取数据,并在另一个活动中显示它们

Android 我需要从数据库中获取数据,并在另一个活动中显示它们,android,database,sqlite,sqliteopenhelper,Android,Database,Sqlite,Sqliteopenhelper,我是android编程新手。我有一个数据库类,其中有Name、Company和Location列。在主活动中,我有AutoCompleteTextBox和一个按钮。例如,如果我在搜索框中输入SUGAR并单击搜索按钮,它应该导航到数据库,在下一页上它应该显示为Name=SUGAR,Company=Annapurna,Location=Panjagutta。我已经完成了DBHelper类和主要活动。但是我无法完成第二个.java类,也就是我需要显示数据库中的值的地方。。。你能帮帮我吗。我正在附上密码

我是android编程新手。我有一个数据库类,其中有Name、Company和Location列。在主活动中,我有AutoCompleteTextBox和一个按钮。例如,如果我在搜索框中输入SUGAR并单击搜索按钮,它应该导航到数据库,在下一页上它应该显示为Name=SUGAR,Company=Annapurna,Location=Panjagutta。我已经完成了DBHelper类和主要活动。但是我无法完成第二个.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;*/
    }
    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);

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

更改参数:i.putExtra(“itemName”,iname);我试过了,但没有用。我可以在autcompletetextview中输入“salt”或数据库中提到的任何项目。问题是,我单击按钮后,它正在从该页面导航,但没有显示输出。输出变成blankChange参数:i.putExtra(“itemName”,iname);我试过了,但没有用。我可以在autcompletetextview中输入“salt”或数据库中提到的任何项目。问题是,我单击按钮后,它正在从该页面导航,但没有显示输出。更改后输出变为空白,我也面临同样的错误。在Logcat中,错误如下“E/AndroidRuntime(1601):java.lang.IndexOutOfBoundsException:无效索引0,大小为0”哪一行引发异常?同样更改后,我面临相同的错误。在Logcat中,错误如下“E/AndroidRuntime(1601):java.lang.IndexOutOfBoundsException:无效索引0,大小为0”哪一行引发异常?