使用 Volley / RecyclerView 在 Button 单击时从 JSON 中提取新信息
Pulling new information from JSON on Button click using Volley / RecyclerView
我有一个 android 应用程序,它当前使用 PHP 从 SQL 数据库中提取信息,然后将其转换为 JSON 字符串
我的代码成功提取了有关应用程序启动的信息,并按预期使用我的数据库内容填充了 RecyclerView。
我的想法是在同一个 RecyclerView 中有 2 个按钮可以在 2 组信息之间切换。
我的问题
所以我创建了一个按钮,单击按钮我希望它更改 JSON 输出所在的 URL (示例。com/api .php) 并使用新的 URL 和单独的 JSON 输出 (example.com/api2.php)
基本上切换 RecyclerView 的数据源,然后我需要在 运行 应用程序中重新加载新结果。
我的问题
如何改变
的值
private static final String URL_PRODUCTS = "http://example.com/Api.php";
到
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);
我有一个 android 应用程序,它当前使用 PHP 从 SQL 数据库中提取信息,然后将其转换为 JSON 字符串
我的代码成功提取了有关应用程序启动的信息,并按预期使用我的数据库内容填充了 RecyclerView。
我的想法是在同一个 RecyclerView 中有 2 个按钮可以在 2 组信息之间切换。
我的问题
所以我创建了一个按钮,单击按钮我希望它更改 JSON 输出所在的 URL (示例。com/api .php) 并使用新的 URL 和单独的 JSON 输出 (example.com/api2.php)
基本上切换 RecyclerView 的数据源,然后我需要在 运行 应用程序中重新加载新结果。
我的问题
如何改变
的值private static final String URL_PRODUCTS = "http://example.com/Api.php";
到
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);