저는 Android 프로그래밍을 처음 사용합니다. 이름, 회사, 위치 열이있는 데이터베이스 클래스가 있습니다. 주요 활동에는 AutoCompleteTextBox와 버튼이 있습니다. 예를 들어, 검색 상자에 SUGAR을 입력하고 검색 버튼을 클릭하면 데이터베이스로 이동해야하며 다음 페이지에는 Name = Sugar, Company = Annapurna, Location = Panjagutta로 표시되어야합니다. DBHelper 클래스와 Main Activity로 끝났습니다. 하지만 Second.java 클래스로 끝낼 수 없습니다. 즉 데이터베이스에서 값을 표시해야하는 경우 ... 나를 도와주세요. 나는 코드를 첨부하고있다.데이터베이스에서 데이터를 가져 와서 다른 액티비티에 표시해야합니다.
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);
}
}
친절 U의 사람이 해결할 수있는 다음과 같은
DBHelper 클래스입니다 이 문제. 나는 여기에서 쳤다. 몰라 무엇을 할 ??? TejaShopActivity에서
매개 변수 변경 : i.putExtra ("itemName", iname); – Capitan
나는 그것을 사용해 보았지만 사용하지는 않았다. 나는 'salt'또는 데이터베이스에 언급 된 autcompletetextview의 항목을 입력 할 수 있습니다. 문제는 버튼을 클릭 한 후 해당 페이지에서 탐색 중이지만 출력이 표시되지 않는다는 것입니다. 출력이 공백으로 변합니다. – goutham07511