Monday, March 10, 2014

Spinner read data from the database to achieve two interactive


android development, when you use Spinner read data from the database and implement two interact in Map data = (Map) shopData.get (position);
Appears shopData cannot be resolved.
My code is
public class BeginActivity extends ListActivity {
private SQLiteDatabase database;
Spinner spinnershop, spinnershelf;
Button begin_scan;
TextView selectshop, selectshelf;
private Integer shopId, shelfId;
private ArrayAdapter shop_adapter;
private ArrayAdapter shelf_adapter;
private String strshop, stshelf;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main_scan);
spinnershop = (Spinner) findViewById (R.id.spinnershop);
spinnershelf = (Spinner) findViewById (R.id.spinnershelf);
begin_scan = (Button) findViewById (R.id.begin_scan);
selectshop = (TextView) findViewById (R.id.TextView03);
selectshelf = (TextView) findViewById (R.id.TextView04);
this.setTitle ("inventory of the main interface");
Map data = ShopDB.getShop () ;/ / get database data
/ / Find
store shelves under the number corresponding number from the store shelves tableList shopList = data.get (1);
List shopData = data.get (0);

ArrayAdapter shop_adapter = new ArrayAdapter (BeginActivity.this, android.R.layout.simple_spinner_item, shopList);
shop_adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item); / / style

spinnershop.setAdapter (shop_adapter);
spinnershop.setOnItemSelectedListener (itemSelectedListener);
}
/ / Select the store shelves linkage corresponding number
private OnItemSelectedListener itemSelectedListener = new OnItemSelectedListener () {
@ Override
public void onItemSelected (AdapterView parent, View view,
int position, long id) {
Spinner spShop = (Spinner) parent;
String selShop = (String) spShop.getItemAtPosition (position);
Map data = (Map) shopData.get (position) ;/ / according to the selected stores to get the id and then get the corresponding number
shelvesint pid = data.get (selShop) ;/ / get id
storesList shelfList = ShopDB.getCityByPid (pid);
shelf_adapter = new ArrayAdapter (BeginActivity.this, android.R.layout.simple_spinner_item, shelfList);
shelf_adapter
. SetDropDownViewResource (android.R.layout.simple_spinner_dropdown_item);
spinnershelf.setAdapter (shelf_adapter);
spinnershelf.setOnItemSelectedListener (citySelectedListener);

}
@ Override
public void onNothingSelected (AdapterView parent) {
/ / TODO Auto-generated method stub
}
};

String selectedShop = null;
private OnItemSelectedListener citySelectedListener = new OnItemSelectedListener () {
@ Override
public void onItemSelected (AdapterView parent, View view,
int position, long id) {
Spinner spShelf = (Spinner) parent;
selectedShop = (String) spShelf.getItemAtPosition (position);
}

@ Override
public void onNothingSelected (AdapterView parent) {
/ / TODO Auto-generated method stub
}
};


}<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
shopData defined in the onCreate method, the scope is defined on only from the local to the onCreate end. So you are not in the onItemSelected references.
The method defined shopData's put it outside.
Reply:
cited a floor tantahe reply:
shopData defined in the onCreate method, the scope is defined on only from the local to the onCreate end. So you are not in the onItemSelected references.
The method defined shopData's put it outside.

Thanks for the answer, as you approach the problem solved, thank you very much.

No comments:

Post a Comment