所以我看到有关数据没有显示在recyclerview上的问题,但是我的工作与之无关。我使用PHP从Mysql获取数据,php获取日期和整数作为Json。但是当我打电话给recyclerview时,数据没有显示出来。 PHP脚本,我测试了脚本,它确实将数据作为Json
获取recyclerView活动
public class recycle_view_sales_report extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private String string_url = "http://10.0.3.2/sales_report.php";
private salesAdapter adapter;
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycle_view_sales_report);
this.setTitle("Customer table");
recyclerView = (RecyclerView)findViewById(R.id.recyclerv);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
getinformation();
}
private void getinformation(){
StringRequest stringRequest = new StringRequest(Request.Method.POST,string_url,
new Response.Listener<String>(){
@Override
public void onResponse(String response) {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
List<salesData> list = Arrays.asList(gson.fromJson(response,salesData[].class));
adapter = new salesAdapter(list,context);
recyclerView.setAdapter(adapter);
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
}
});
MySingleton.getInstance(this).addToRequestQue(stringRequest);
}
}
适配器类
public class salesAdapter extends RecyclerView.Adapter<salesAdapter.MyViewholder> {
private List<salesData> list = new ArrayList<>();
Context context;
salesAdapter(List<salesData> list, Context ctx){
this.list = list;
context = ctx;
}
@Override
public salesAdapter.MyViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sales_report_layout,parent,false);
return new salesAdapter.MyViewholder(view,context,list);
}
@Override
public void onBindViewHolder(salesAdapter.MyViewholder holder, int position) {
holder.datetx.setText(String.valueOf(list.get(position).getDate()));
holder.salestx.setText(String.valueOf(list.get(position).getSales()));
}
@Override
public int getItemCount() {
return list.size();
}
public static class MyViewholder extends RecyclerView.ViewHolder {
TextView datetx,salestx;
List<salesData> datas = new ArrayList<salesData>();
Context context;
public MyViewholder(View itemView, Context ctx, List<salesData> datas){
super(itemView);
this.datas = datas;
context = ctx;
datetx = (TextView)itemView.findViewById(R.id.tx_date);
salestx = (TextView)itemView.findViewById(R.id.tx_sales);
}}
}
数据类
public class salesData {
String date;
int sales;
public salesData(String date, int sales) {
this.date = date;
this.sales = sales;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public int getSales() {
return sales;
}
public void setSales(int sales) {
this.sales = sales;
}
}
XML RECYCLERVIEW
`<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="120dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="120dp"
android:id = "@+id/tx_id"
android:layout_alignParentLeft="true"
android:text="Date:"
android:gravity="center"
android:textAppearance="?android:textAppearanceLarge"
/>
<TextView
android:id="@+id/tx_date"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/tx_id"
android:layout_toRightOf="@+id/tx_id"
android:gravity="center"
android:text="tx_date"
android:textAppearance="?android:textAppearanceLarge"/>
<TextView
android:id="@+id/sales"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/tx_date"
android:layout_toRightOf="@+id/tx_date"
android:gravity="center"
android:text="Sales:"
android:textAppearance="?android:textAppearanceLarge"/>
<TextView
android:id="@+id/tx_sales"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/sales"
android:layout_toRightOf="@+id/sales"
android:gravity="center"
android:text="tx_sales"
android:textAppearance="?android:textAppearanceLarge"/>
`
XML布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res /android"
android:layout_width="match_parent" android:layout_height="120dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="120dp"
android:id = "@+id/tx_id"
android:layout_alignParentLeft="true"
android:text="Date:"
android:gravity="center"
android:textAppearance="?android:textAppearanceLarge"
/>
<TextView
android:id="@+id/tx_date"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/tx_id"
android:layout_toRightOf="@+id/tx_id"
android:gravity="center"
android:text="tx_date"
android:textAppearance="?android:textAppearanceLarge"/>
<TextView
android:id="@+id/sales"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/tx_date"
android:layout_toRightOf="@+id/tx_date"
android:gravity="center"
android:text="Sales:"
android:textAppearance="?android:textAppearanceLarge"/>
<TextView
android:id="@+id/tx_sales"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/sales"
android:layout_toRightOf="@+id/sales"
android:gravity="center"
android:text="tx_sales"
android:textAppearance="?android:textAppearanceLarge"/>
</RelativeLayout>
php脚本
<?PHP
require "conn.php";
$sql_qry = " SELECT calendar.datefield AS DATE, SUM(orders.price) AS total_sales FROM orders
RIGHT JOIN calendar ON (DATE(orders.date_time) = calendar.datefield) GROUP BY DATE;";
$result = mysqli_query($conn, $sql_qry);
$response = array();
while($row = mysqli_fetch_array($result)){
array_push($response,array("DATE"=>$row[0],"total_sales"=>$row[1]));
}
echo json_encode($response);
?>
答案 0 :(得分:0)
你可以尝试
我的Json看起来像
{“saleNodes”:[{“id”:“1”,“saledate”:“2017-10-10”}]}
我制作json的Php代码就像..
if ($result->num_rows > 0) {
$userDetails = array();
while ($row = $result->fetch_assoc()) {
// $response["id"] = $row["id"];
$userData[] = array(
"id" => $row["id"],
"saledate" => $row["date"],
);
}
} else {
echo "0 results";
}
$result1['saleNodes'] = $userData;
$json = json_encode($result1);
echo $json;
在android应用程序中可以获取数据
private void getData() {
String url="http://1.11.201.179/Airport/tour_places.php";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//pDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray loginNodes = jsonObject.getJSONArray("saleNodes");
Log.e("****ddd",loginNodes.toString());
for (int i = 0; i < loginNodes.length(); i++) {
JSONObject jo = loginNodes.getJSONObject(i);
String name = jo.getString("saledate");
SalesData d = new SalesData();
d.setDate(name);
salesDatasList.add(d);
}
showCardList();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//pDialog.dismiss();
Log.e("****ddd",error.toString());
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
//Parmeter
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
适配器看起来像
//Being adapter
void showCardList(){
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(Your activity.this));
// create an Object for Adapter
mAdapter = new CardViewDataAdapter(salesDatasList);
// set the adapter object to the Recyclerview
recyclerView.setAdapter(mAdapter);
}
class CardViewDataAdapter extends RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> {
public CardViewDataAdapter(ArrayList<SalesData> os_versions) {
dataSet = os_versions;
}
@Override
public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
// create a new view
View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.row_sale, null);
itemLayoutView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
CardViewDataAdapter.ViewHolder viewHolder = new CardViewDataAdapter.ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(CardViewDataAdapter.ViewHolder viewHolder, int i) {
SalesData fp = dataSet.get(i);
viewHolder.tv_date.setText(fp.getDate());
viewHolder.menu = fp;
}
@Override
public int getItemCount() {
return dataSet.size();
}
public void updateList(List<SalesData> temp) {
dataSet = (ArrayList<SalesData>) temp;
notifyDataSetChanged();
}
// inner class to hold a reference to each item of RecyclerView
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView tv_date;
public SalesData menu;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
tv_date = (TextView) itemLayoutView .findViewById(R.id.tv_date);
}
}
}
//end Adapter
在Activity中声明变量
ArrayList<SalesData> salesDatasList = new ArrayList<>();
private CardViewDataAdapter mAdapter;
private ArrayList<SalesData> dataSet;
private RecyclerView recyclerView;
您的mainactity.xml
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:scrollbars="vertical" />
row_sale.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="5dp"
card_view:cardBackgroundColor="#FFFFFF"
card_view:cardUseCompatPadding="true" >
<TextView
android:id="@+id/tv_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:textAllCaps="true"
android:text="date"/>
</android.support.v7.widget.CardView>
您的SaleDate类
public class SalesData {
private String date;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
不要忘记添加gadle.xml
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.mcxiaoke.volley:library:1.0.19'
上面的代码正在为我工作..很高兴...提前感谢..