[英]Populate Android spinner from PHP MySql
I have populate spinner from PHP MySql.While i select 1st item then populate another spinner from item click.But the problem is in second spinner populate the data doubly.Whenever i select the item of 1st Spinner.Can someone help me for only once add the data in Second spinner. 我已经从PHP MySql中填充了微调器。虽然我选择了第一个微调器,然后从项目单击中填充了另一个微调器。但是问题是第二个微调器会双重填充数据。每当我选择第一个微调器的项目时,有人只能帮我添加一次第二个微调器中的数据。
Here is my Screen Shot 这是我的屏幕截图
Here is my Activity Code 这是我的活动代码
spinnerCategory.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(parent.getContext(), "On Item Select : \n" + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
strSelected_Category=parent.getItemAtPosition(pos).toString();
System.out.println("strSelected_Category = " + strSelected_Category);
spinnerSubCategory.setEnabled(true);
ServiceHandler jsonParser = new ServiceHandler();
String json = jsonParser.makeServiceCall(URL_SUB_CATEGORIES, ServiceHandler.GET);
Log.e("Response: ", " > " + json);
if (json != null)
{
try {
JSONArray array=new JSONArray(json);
JSONArray sortedArray = new JSONArray();
for(int i=0;i<array.length();i++)
{
JSONObject obj=array.getJSONObject(i);
if(obj.getString("cat_name").equalsIgnoreCase(strSelected_Category))
{
sortedArray.put(obj);
Log.e("sortedArray"," = " +sortedArray);
}
}
if (sortedArray != null)
{
for (int i = 0; i < sortedArray.length(); i++)
{
JSONObject catObj = (JSONObject) sortedArray.get(i);
Category cat = new Category(catObj.getInt("subcat_id"),catObj.getString("subcat_name"));
sub_categoriesList.add(cat);
Log.e("cat "," = " + cat);
}
populateSubCategory();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
Log.e("JSON Data", "Didn't receive any data from server!");
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
/**
* Adding Category spinner data
* */
private void populateSpinner() {
List<String> lables = new ArrayList<String>();
for (int i = 0; i < categoriesList.size(); i++)
{
lables.add(categoriesList.get(i).getName());
}
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, lables);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCategory.setAdapter(spinnerAdapter);
}
private void populateSubCategory() {
List<String> lables = new ArrayList<String>();
for (int i = 0; i < sub_categoriesList.size(); i++)
{
lables.add(sub_categoriesList.get(i).getName());
}
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, lables);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerSubCategory.setAdapter(spinnerAdapter);
}
spinnerCategory.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(parent.getContext(), "On Item Select : \n" + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
strSelected_Category=parent.getItemAtPosition(pos).toString();
System.out.println("strSelected_Category = " + strSelected_Category);
spinnerSubCategory.setEnabled(true);
ServiceHandler jsonParser = new ServiceHandler();
String json = jsonParser.makeServiceCall(URL_SUB_CATEGORIES, ServiceHandler.GET);
Log.e("Response: ", " > " + json);
if (json != null)
{
try {
JSONArray array=new JSONArray(json);
JSONArray sortedArray = new JSONArray();
for(int i=0;i<array.length();i++)
{
JSONObject obj=array.getJSONObject(i);
if(obj.getString("cat_name").equalsIgnoreCase(strSelected_Category))
{
sortedArray.put(obj);
Log.e("sortedArray"," = " +sortedArray);
}
}
sub_categoriesList.clear();
if (sortedArray != null)
{
for (int i = 0; i < sortedArray.length(); i++)
{
JSONObject catObj = (JSONObject) sortedArray.get(i);
Category cat = new Category(catObj.getInt("subcat_id"),catObj.getString("subcat_name"));
sub_categoriesList.add(cat);
Log.e("cat "," = " + cat);
}
populateSubCategory();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
Log.e("JSON Data", "Didn't receive any data from server!");
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
/**
* Adding Category spinner data
* */
private void populateSpinner() {
List<String> lables = new ArrayList<String>();
for (int i = 0; i < categoriesList.size(); i++)
{
lables.add(categoriesList.get(i).getName());
}
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, lables);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCategory.setAdapter(spinnerAdapter);
}
private void populateSubCategory() {
List<String> lables = new ArrayList<String>();
for (int i = 0; i < sub_categoriesList.size(); i++)
{
lables.add(sub_categoriesList.get(i).getName());
}
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, lables);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerSubCategory.setAdapter(spinnerAdapter);
} }
TRY THIS , HAPPY CODING :) 尝试此,愉快的编码:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.