Reputation: 11
I'm new to android programming. I have a database class in which I have Name, Company, Location columns. In the Main Activity I have AutoCompleteTextBox and a Button. For example, If I enter SUGAR in the search box and click the search button, it should navigate to the database and on the next page it should display as Name=Sugar, Company=Annapurna, Location=Panjagutta. I'm done with my DBHelper class and Main Activity. But I'm unable to finish with the Second.java class i.e where I need to display the values from the Database... Can you please help me out. I'm attaching the code.
The DBHelper class is as follows
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
}
}
The Main Activity is as follows
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 is as follows
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);
}
}
Kindly can anyone of u solve this problem. I'm struck up here. dunno what to do???
Upvotes: 1
Views: 4406
Reputation: 21
in TejaShopActivity, your issue is here:
i.putExtra(iname, "itemName");
i.putExtra(icompany, "Company");
i.putExtra(iloc, "Loc");
your arguments are reversed. Should be:
i.putExtra("itemName", iname);
i.putExtra("Company", icompany);
i.putExtra("Loc", iloc);
Upvotes: 1