gpt4 book ai didi

android - 从android中的mysql数据库加载多个微调器

转载 作者:行者123 更新时间:2023-11-29 23:25:58 24 4
gpt4 key购买 nike

在我的android应用程序中,我有6个旋转器,我需要根据类别明智地使用mysql数据库中存在的数据加载它们。并且我使用json来检索数据并将其发送回android,但在这里我没有得到如何将数据分配给微调器。请帮我解决这个问题。

public void getItems(int l)
{
//Toast.makeText(getApplicationContext(), ""+l, 5000).show();
AsyncHttpClient client = new AsyncHttpClient();
RequestParams autofill_params = new RequestParams();
autofill_params.put("sending_category_id_JSON",autofill_composeJSONfromSQLite(l));
//Toast.makeText(getApplicationContext(), "Sending JSON ==> "+autofill_params,5000).show();
client.post("http://www.XXXX.com/mobile_cycle/spares_auto_fill.php", autofill_params, new AsyncHttpResponseHandler()

{
@Override
public void onSuccess(String response)

{
//Toast.makeText(getApplicationContext(), "Responce is ==>"+response, 5000).show();
//spinner_updater();
Gson gson = new GsonBuilder().create();
try
{
JSONArray arr = new JSONArray(response);
//Toast.makeText(getApplicationContext(), "Length ==> "+arr.length(), 5000).show();
//public String temp_array[];
final String[] temp_array = new String[5];
for (int i = 0; i < arr.length(); i++)
{
JSONObject obj = (JSONObject) arr.get(i);
//auto_first_name = obj.get("name").toString();
//Toast.makeText(getApplicationContext(), "Length ==> "+arr.length(), 5000).show();
String item_name = obj.get("name").toString();
temp_array[i] = item_name;
//spinner_updater(item_name);
}

spinner_updater(temp_array);

}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

//fillData(autofill_network_flag);
}

@Override
public void onFailure(int statusCode, Throwable error,String content)

{

if (statusCode == 404)

{
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
}

else if (statusCode == 500)

{
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
}

else

{
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]",
Toast.LENGTH_LONG).show();
}
}

});
}

public void spinner_updater(String[] item_name)

{

Toast.makeText(getApplicationContext(), "Inside Updater Item Length ==> "+item_name.length, 5000).show();

for(int length = 0; length < item_name.length; length++)

{
String empty_temp = " ";

String temp_item = item_name[length];

if(temp_item.equals(empty_temp))

{
//Toast.makeText(getApplicationContext(), "Item ==> "+item_name[length], 5000).show();
}

else

{
Toast.makeText(getApplicationContext(), "Item ==> "+item_name[length], 5000).show();
}
}

ArrayAdapter<String> adp2 = new ArrayAdapter<String>
(this, android.R.layout.simple_dropdown_item_1line, str1);
sp2.setAdapter(adp2);

}

最佳答案

首先创建与微调器一样多的列表

         List<String> list1, list2, list;

list1 = new ArrayList<String>();
list2 = new ArrayList<String>();
list3 = new ArrayList<String>();


spinner1 = (Spinner) findViewById(R.id.spinner1 );
spinner2 = (Spinner) findViewById(R.id.spinner2 );
spinner3 = (Spinner) findViewById(R.id.spinner3 );

现在将数据从数据库或 json 添加到每个列表

                SQLiteDatabase db = dbh.getReadableDatabase();

Cursor cursor = db.rawQuery("select * from frnds", null);
adding first row items in first list
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
.moveToNext()) {
// do what you need with the cursor here
list1.add(cursor.getString(1));
}

在第二个列表中添加第二行项目

 for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
.moveToNext()) {
// do what you need with the cursor here
list2.add(cursor.getString(2));
}

在第三个列表中添加第三行项目

for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
.moveToNext()) {
// do what you need with the cursor here
list3.add(cursor.getString(3));
}
cursor.close();

将列表设置为微调器

   ArrayAdapter<String> spinner1Adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,
list1);
spinner1Adapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(spinner1Adapter);

ArrayAdapter<String> spinner2Adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,
list2);
spinner2Adapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(spinner2Adapter);

ArrayAdapter<String> spinner3Adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,
list3);
spinner3Adapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


spinner3.setAdapter(spinner3Adapter);

关于android - 从android中的mysql数据库加载多个微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26966199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com