同じインターフェイスで 2 つのスピナーを使用しようとしましたが、機能しません。なんで ?スピナーは 1 つだけ表示されます。
1 つはデータベースから情報を取得しandroid:id="@+id/sp11"
、もう 1 つは文字列ファイルから情報を取得しますandroid:id="@+id/typeMessage_spinner"
。配列 @string ファイルから取得するものだけが機能します。android:id="@+id/typeMessage_spinner"
データベースから取得したものを削除すると機能します。インターフェイスにはスピナーが 1 つだけ表示されます。
このように http://im13.gulfup.com/W1fV1.png
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<Spinner
android:id="@+id/sp11"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/chooseCourse"
android:layout_marginTop="20dip"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
/>
<Spinner
android:id="@+id/typeMessage_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/chooseCourse"
android:layout_marginTop="20dip"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
/>
</RelativeLayout>
ここに私のコードがありonCreate()
ます。私の最初のスピナーを見てください、そしてonPostExecute()
私は2番目のスピナーを持っています
public class MainActivity extends Activity {
JSONParser jParser = new JSONParser(); // class
ArrayList<HashMap<String, String>> coursesList;
private static String url_all_course = "http://10.0.2.2/SmsPhp/view_all_course.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_course = "course";
private static final String TAG_CourseID = "CourseID";
private static final String TAG_Name = "Name";
JSONArray courses = null;
// Spinner element
SpinnerAdapter adapter,adapter1;
Spinner spinner ,spinner1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
coursesList = new ArrayList<HashMap<String, String>>();
spinner= (Spinner) findViewById(R.id.typeMessage_spinner);
spinner1= (Spinner) findViewById(R.id.sp11);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( MainActivity .this,
R.array.typeMessage_spinner, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
new LoadAllCourses().execute();
}
class LoadAllCourses extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_course, "GET",
params);
// Check your log cat for JSON response
Log.d("All courses: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// course found
// Getting Array of course
courses = json.getJSONArray(TAG_course);
// looping through All courses
for (int i = 0; i < courses.length(); i++)// course
// JSONArray
{
JSONObject c = courses.getJSONObject(i); // read first
// Storing each json item in variable
String CourseID = c.getString(TAG_CourseID);
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_CourseID, CourseID);
map.put(TAG_Name, Name);
// adding HashList to ArrayList
coursesList.add(map);
}
} else {
}
} 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
adapter1 = new CustomAdapter(MainActivity.this,
android.R.layout.simple_spinner_item,
coursesList);
spinner1.setAdapter(adapter1); // Set the custom adapter to the spinner
// You can create an anonymous listener to handle the event when is selected an spinner item
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener() {
/* @Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
// Here you get the current item (a User object) that is selected by its position
ArrayAdapter<HashMap<String, String>> x = (ArrayAdapter<HashMap<String, String>>) adapter.getItem(position);
// Here you can do the action you want to...
Toast.makeText(MainActivity.this, "ID: "+ x ,
Toast.LENGTH_SHORT).show();
}*/
@Override
public void onNothingSelected(AdapterView<?> adapter) { }
});
}
}
}