使用Volley/RecyclerView点击按钮,从JSON中提取新信息



我有一个android应用程序,它目前使用PHP从SQL数据库中提取信息,并将其转换为JSON字符串

我的代码成功地提取了应用程序启动的信息,并按预期用我的数据库内容填充RecyclerView。

我的想法是有两个按钮,可以在同一个RecyclerView中的两组信息之间切换。

我的问题

因此,我创建了一个按钮,在button Click上,我希望它更改JSON输出所在的URL(example.com/api.php(,并使用一个带有单独JSON输出的新URL[example.com/api2.php(

本质上是切换RecyclerView的数据源,然后我需要在运行的应用程序中重新加载新结果。

我的问题

如何更改的值

private static final String URL_PRODUCTS = "http://example.com/Api.php";

TO

private static final String URL_PRODUCTS = "http://example.com/Api2.php";

然后

不知怎的用新信息刷新我的RecyclerView。

我的尝试

simpleButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
simpleButton1.setVisibility(View.GONE);
menuButton.setText("Menu");
URL_PRODUCTS = "http://example.com/Api.php"
#### I HAVE NO IDEA HOW TO REFRESH THE RECYCLERVIEW#######

我的一些代码(为了清晰起见,省略了一些位(

public class MainActivity extends AppCompatActivity {
Button simpleButton1;
boolean menuUp;
//this is the JSON Data URL
private static final String URL_PRODUCTS = "http://example.com/Api.php";
//a list to store all the products
List<Product> productList;
//the recyclerview
RecyclerView recyclerView;

获取&转换数据

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
simpleButton1 = (Button) findViewById(R.id.simpleButton1);//get id of button 1
menuButton = (Button) findViewById(R.id.menuButton);//get id of button 2

//getting the recyclerview from xml
recyclerView = findViewById(R.id.recylcerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//initializing the productlist
productList = new ArrayList<>();
//this method will fetch and parse json
//to display it in recyclerview
loadProducts();
}

更多

/*
* Creating a String Request
* The request type is GET defined by first parameter
* The URL is defined in the second parameter
* Then we have a Response Listener and a Error Listener
* In response listener we will get the JSON response as a String
* */
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//converting the string to json array object
JSONArray array = new JSONArray(response);
//traversing through all the object
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject product = array.getJSONObject(i);
//adding the product to product list
productList.add(new Product(
product.getInt("id"),
product.getString("title"),
product.getString("shortdesc"),
product.getDouble ("rating"),
product.getDouble("price"),
product.getString("image")
));
}
//creating adapter object and setting it to recyclerview
ProductsAdapter adapter = new ProductsAdapter(MainActivity.this, productList);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
MORE
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);

您可以定义2个url:

private static final String URL_PRODUCTS = "http://example.com/Api.php";
private static final String URL_PRODUCTS_2 = "http://example.com/Api2.php";

然后设置RecyclerView:

productList = new ArrayList<>();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new ProductsAdapter(MainActivity.this, productList);
recyclerView.setAdapter(adapter);
loadProducts(URL_PRODUCTS);//add param for this method then you have to add it to your volley request

在你的凌空抽射反应中改变2行

ProductsAdapter adapter = new ProductsAdapter(MainActivity.this, productList);
recyclerView.setAdapter(adapter);

至:

adapter.notifyDataSetChanged();

现在,当点击按钮时,您将更改url

simpleButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
productList.clear();
loadProducts(URL_PRODUCTS_2);

最新更新