gpt4 book ai didi

php - Recyclerview MySql int 显示 0

转载 作者:行者123 更新时间:2023-11-29 18:11:18 24 4
gpt4 key购买 nike

所以我看到了有关数据未在 recyclerview 上显示的问题,但不适用于我的。我使用 PHP 从 Mysql 获取数据,PHP 将日期和整数作为 Json 获取。但是当我调用 recyclerview 时,数据没有显示。PHP 脚本,我已经测试了该脚本,它确实以 Json 形式获取数据

recycler查看 Activity

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 回收器 View

  `<?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);



?>

最佳答案

你可以试试

  1. 我的 Json 看起来像

    {"saleNodes":[{"id":"1","saledate":"2017-10-10"}]}

  2. 我用于制作 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;
  3. 在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);
    }
  4. 适配器看起来像

       //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
  5. 在 Activity 中声明变量

      ArrayList<SalesData> salesDatasList = new ArrayList<>();
    private CardViewDataAdapter mAdapter;
    private ArrayList<SalesData> dataSet;
    private RecyclerView recyclerView;
  6. 您的 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" />
  7. 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>
  8. 您的 SaleDate 类

    public class SalesData {

    private String date;

    public String getDate() {
    return date;

    }

      public void setDate(String date) {
    this.date = date;
    }

    }

  9. 不要忘记添加 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'
  10. 上面的代码对我有用..享受它...提前致谢..

关于php - Recyclerview MySql int 显示 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47384252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com