我正在尝试将json数据解析到我的android java代码中,我能够从json中获取数据,但是我无法从中获取特定字段。
json api的结构如下:
[
{
"S/L NO": "1",
"Item Name": "Cheese Corn",
"Category": "Appetizers"
},
{
"S/L NO": "2",
"Item Name": "Pasta",
"Category": "Full Meal"
}
]
我想得到“项目名称”并加载到我的滚动微调器。以下是我的代码:
public class Provide_food extends AppCompatActivity {
Spinner spinner;
String url = "https://sheet.best/api/sheets/00f2c956-1c25-44ab-85f5-2e3bcfc580a0";
List<Item_Model> list = new ArrayList<>();
List<String> list1 = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_provide_food);
spinner = (Spinner) findViewById(R.id.select_food);
List itemlist = new ArrayList<>();
RequestQueue queue = Volley.newRequestQueue(this);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.e("Response", response.toString());
CustomAdapter adapter = new CustomAdapter(Provide_food.this, R.layout.item, itemlist);
spinner.setAdapter(adapter);
// do something with the response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
});
// Add request to queue
queue.add(jsonArrayRequest);
}
}
我还开发了一个customadapter,用于将项目名称加载到可滚动微调器中。代码如下:
package com.example.helping_hands_individual;
import android.content.Context;
import android.transition.Slide;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class CustomAdapter extends ArrayAdapter {
LayoutInflater inflater;
Context context;
List<Item_Model> list;
public CustomAdapter(Context applicationContext,int resource, List<Item_Model> list){
super(applicationContext,resource);
this.context = applicationContext;
this.list = list;
inflater = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflater.inflate(R.layout.item,null);
TextView itemname = (TextView)view.findViewById(R.id.item_names);
itemname.setText(list.get(i).itemname);
return view;
}
}
下面是item.xml的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/item_names"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:textSize="18dp"
android:text="Demo"/>
</LinearLayout>
有人能帮我吗?
3条答案
按热度按时间j2qf4p5b1#
遍历jsonarray并使用
getString()
方法从对象获取字段值。dfty9e192#
尝试此解决方案:
第一步。为你的React做一个模型。
第二步。把你的答案放到模型课上
m1m5dgzv3#