我想知道在将位置设置为该值/选择之前是否可以检查微调器是否包含值/选择。我正在做的是客户可以选择产品EX:RED,YELLOW,GREEN。当他们进行选择时,它会记录在我的数据库中。当客户加载要编辑的项目时,我需要将微调器位置设置为该选择。我可以毫无问题地做到这一点。问题是,如果他们重新登录并且他们的购物车在一周后重新填充,则可能不再提供选项,因此我需要确保在选择之前它仍然可用。我已经尝试检查ArrayList,这是我认为我需要做的,但我不完全确定。我尝试过,但它崩溃了应用程序。有任何想法吗?提前致谢。以下是我的代码。
加载选定的选项:
ArrayAdapter<String> spinnerOpt1;
ArrayAdapter<String> spinnerOpt2;
ArrayAdapter<String> spinnerOpt3;
class LoadSelectedOptions extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
Log.d("CONFIRM SPIN STAT 1::", Integer.toString(SpinStat1));
if(SpinStat1==1){
Log.d("SPINSTAT1", Integer.toString(SpinStat1));
List<NameValuePair> Options = new ArrayList<NameValuePair>();
Options.add(new BasicNameValuePair(DO_FUNCTION, SEL_OPT_FUNCTION1));
Options.add(new BasicNameValuePair(SEL_ITEM_COUNT, theProduct_Number));
JSONObject json = jParser.makeHttpRequest(master_url, "GET", Options);
Log.d("SPINSTAT1", json.toString());
try{
sel_success1 = json.getInt(SEL_SUCCESS1);
JSONArray selected_1 = json.getJSONArray(OPT_CATAGORIES);
if (sel_success1 == 1) {
for (int i = 0; i < selected_1.length(); i++) {
JSONObject c = selected_1.getJSONObject(i);
sel_opt1 = c.getString(SEL_OPT1);
}
}else {
}
}catch (JSONException e) {
e.printStackTrace();
}
}
if(SpinStat2==1){
List<NameValuePair> Options2 = new ArrayList<NameValuePair>();
Options2.add(new BasicNameValuePair(DO_FUNCTION, SEL_OPT_FUNCTION2));
Options2.add(new BasicNameValuePair(SEL_ITEM_COUNT, theProduct_Number));
JSONObject json2 = jParser.makeHttpRequest(master_url, "GET", Options2);
try{
sel_success2 = json2.getInt(SEL_SUCCESS2);
JSONArray selected_2 = json2.getJSONArray(OPT_CATAGORIES);
if (sel_success2 == 1) {
for (int i = 0; i < selected_2.length(); i++) {
JSONObject c = selected_2.getJSONObject(i);
sel_opt2 = c.getString(SEL_OPT2);
}
}else {
}
}catch (JSONException e) {
e.printStackTrace();
}
}
if(SpinStat3==1){
List<NameValuePair> Options3 = new ArrayList<NameValuePair>();
Options3.add(new BasicNameValuePair(DO_FUNCTION, SEL_OPT_FUNCTION3));
Options3.add(new BasicNameValuePair(SEL_ITEM_COUNT, theProduct_Number));
JSONObject json3 = jParser.makeHttpRequest(master_url, "GET", Options3);
try{
sel_success3 = json3.getInt(SEL_SUCCESS3);
JSONArray selected_3 = json3.getJSONArray(OPT_CATAGORIES);
if (sel_success3 == 1) {
for (int i = 0; i < selected_3.length(); i++) {
JSONObject c = selected_3.getJSONObject(i);
sel_opt3 = c.getString(SEL_OPT3);
}
}else {
}
}catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
runOnUiThread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
//SpinStat is a value determining if there are options. 0==No Options 1==Options Are Available
if(SpinStat1==1){
//spinnerOpt1 is the ArrayAdapter for that specific spinner. It is loaded from a List/ArrayList "options1"
}else if(spinnerOpt1.equals(sel_opt1)){
Spinner1.setSelection(spinnerOpt1.getPosition(sel_opt1));
}
if(SpinStat2==1){
}else if(spinnerOpt2.equals(sel_opt2)){
Spinner2.setSelection(spinnerOpt2.getPosition(sel_opt2));
}
if(SpinStat3==1){
}else if(spinnerOpt3.equals(sel_opt3)){
Spinner3.setSelection(spinnerOpt3.getPosition(sel_opt3));
}
}
});
}
}
的logcat ::
07-16 13:41:49.875: E/AndroidRuntime(20378): FATAL EXCEPTION: main
07-16 13:41:49.875: E/AndroidRuntime(20378): java.lang.NullPointerException
07-16 13:41:49.875: E/AndroidRuntime(20378): at com.example.shopper.EditCart$LoadSelectedOptions$1.run(EditCart.java:689)
07-16 13:41:49.875: E/AndroidRuntime(20378): at android.app.Activity.runOnUiThread(Activity.java:4784)
07-16 13:41:49.875: E/AndroidRuntime(20378): at com.example.shopper.EditCart$LoadSelectedOptions.onPostExecute(EditCart.java:675)
07-16 13:41:49.875: E/AndroidRuntime(20378): at com.example.shopper.EditCart$LoadSelectedOptions.onPostExecute(EditCart.java:1)
07-16 13:41:49.875: E/AndroidRuntime(20378): at android.os.AsyncTask.finish(AsyncTask.java:631)
07-16 13:41:49.875: E/AndroidRuntime(20378): at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-16 13:41:49.875: E/AndroidRuntime(20378): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
07-16 13:41:49.875: E/AndroidRuntime(20378): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 13:41:49.875: E/AndroidRuntime(20378): at android.os.Looper.loop(Looper.java:137)
07-16 13:41:49.875: E/AndroidRuntime(20378): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-16 13:41:49.875: E/AndroidRuntime(20378): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 13:41:49.875: E/AndroidRuntime(20378): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 13:41:49.875: E/AndroidRuntime(20378): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
07-16 13:41:49.875: E/AndroidRuntime(20378): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
07-16 13:41:49.875: E/AndroidRuntime(20378): at dalvik.system.NativeStart.main(Native Method)
加载SPINNER的选项::
class LoadOptions1 extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(DO_FUNCTION, OPT_FUNCTION));
params.add(new BasicNameValuePair("PROD_ID", GET_PID));
// getting JSON string from URL
Log.d("PROD_ID", GET_PID);
Log.d("DO_FUNCTION", OPT_FUNCTION);
Log.d("master_url", master_url);
JSONObject json = jParser.makeHttpRequest(master_url, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Options: ", json.toString());
// Checking for SUCCESS TAG
success2 = json.getInt(OPT_SUCCESS);
Log.d("success2", Integer.toString(success2));
if (success2 == 1) {
// products found
// Getting Array of Products
JSONArray catagories = json.getJSONArray(OPT_CATAGORIES);
// looping through All Products
for (int i = 0; i < catagories.length(); i++) {
JSONObject c = catagories.getJSONObject(i);
// Storing each json item in variable
String category = c.getString(OPT_CATAGORY);
options1.add(category);
}
} else {
// no products found
// Launch Add New product Activity
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
if (!(success2 == 0)) {
SpinStat1 = 1;
Log.d("SET SPIN STAT 1::", Integer.toString(SpinStat1));
spinnerOpt1 = new ArrayAdapter<String>(EditCart.this, android.R.layout.simple_list_item_1, options1);
Spinner1.setAdapter(spinnerOpt1);
// MySpinner.setSelection(spinnerMenu.getPosition("Select A Catagory"));
}else{
textView8.setVisibility(View.GONE);
Spinner1.setVisibility(View.GONE);
SpinStat1 = 0;
}
}
}
答案::检查列表中的值。
if(SpinStat1==1){
}else if(options1.contains(sel_opt1)){
Spinner1.setSelection(spinnerOpt1.getPosition(sel_opt1));
}
答案 0 :(得分:0)
哦,我看到了问题。您的ArrayAdapter
是null
。你永远不会初始化它们
ArrayAdapter<String> spinnerOpt1;
ArrayAdapter<String> spinnerOpt2;
ArrayAdapter<String> spinnerOpt3;
你需要在onPostExecute()
根据我的看法,假设第689行为else if(spinnerOpt1.equals(sel_opt1))
。如果没有,那么你可能还有其他问题,但那些需要初始化
答案 1 :(得分:0)
针对填充适配器的List检查值“RED”。代码张贴在上面。