所以我需要为我的微调器设置一个adpter,但是信息是在ArrayList(HashMap(String,String))上设置的,我见过的例子只是存储在一个Arraylist中我该如何改变它。
我有: public class NewProductActivity extends Activity{
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
Spinner adapter ;
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private static String url_all_estabs = "http://10.0.2.2/webprojecto4/index_pesagem.php";
// JSON Node names
private static final String TAG_PRODUCTS = "estab";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "design";
// products JSONArray
JSONArray products = null;
/** WebServices */
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputdt_ini;
EditText inputdt_fim;
EditText inputDesc;
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewProductActivity.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_estabs, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} 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
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Locate the spinner in activity_main.xml
Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);
// Spinner adapter
mySpinner
.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,
worldlist));
// Spinner on item click listener
mySpinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
// TODO Auto-generated method stub
// Locate the textviews in activity_main.xml
TextView txtrank = (TextView) findViewById(R.id.rank);
// Set the text followed by the position
txtpopulation.setText(world.get(position).getPopulation());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
这就是我所拥有的,我需要改变它 ArrayList(HashMap(String,String))
// Spinner adapter
mySpinner
.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,
worldlist));
}
答案 0 :(得分:0)
// first way to do it
List<String> worldList; // as global variable
// in doInBackground(String... args) {} function instead of this line productsList.add(map);
// use this one :
protected String doInBackground(String... args) {
// your code ....
worldList = new ArrayList<String>(map.values()); // return List object
}
// in onPostExecute(String file_url) function
// then use it in your ArrayAdapter now you are happy
protected void onPostExecute(String file_url) {
// your code ...
mySpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,
worldlist));
}
//Value of worldList could be String or Integer what ever you want
//done
// second way to do it
// however you can convert HashMap to array like so
String[] worldArray; // as global variable
// in doInBackground(String... args) {} function instead of this line productsList.add(map);
// use this one :
protected String doInBackground(String... args) {
// your code ....
worldArray = map.values().toArray(new String[map.size()]); // return array
}
// in onPostExecute(String file_url) function
// then use it in your ArrayAdapter now you are happy
protected void onPostExecute(String file_url) {
// your code ...
mySpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,
worldArray ));
}
//done
我告诉你如何做到这一点coz ArrayAdapter构造函数可以采取数组或列表,
我希望这有用。