我有问题。我从PHP脚本下载JSON:
mgo
此脚本下载JSON:
"gopkg.in/mgo.v2"
我使用GSON将其解析为此类:
[{
"Nazwa": "Rower",
"Cena": "300"
}, {
"Nazwa": "Rower mtb",
"Cena": "350"
}]
我将 postList 添加到RecyclerViewAdapter:
public class MainActivity extends AppCompatActivity
{
public List<Post> postList;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view)
{
EditText editText = findViewById(R.id.editText);
final String name = editText.getText().toString();
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "https://shoppingcentreeshop.000webhostapp.com/androidAppScripts/szukajProduk tu.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
Log.v(TAG, response);
Gson gson = new Gson();
Type type = new TypeToken<List<Post>>(){}.getType();
postList = gson.fromJson(response, type);
int size = postList.size();
Log.v(TAG, "SIZE = "+size);
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(MainActivity.this, postList);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
recyclerView.setAdapter(recyclerViewAdapter);
}
}, new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Log.e(TAG, error.toString());
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError
{
Map<String, String> params = new HashMap<String, String> ();
params.put("name_product", name);
return params;
}
};
requestQueue.add(stringRequest);
}
}
为什么在我的回收站视图中,我只有 postList 中的第一个索引中的文本。
我知道,有很多代码,但是我不知道代码在哪里。
我第一次使用JSON将数据从服务器获取到RecyclerView 所以请忍受我
答案 0 :(得分:0)
我重新启动了Android Studio,现在可以使用了! 我不知道为什么!