使用RecyclerView和ListItem在Android Studio中创建表格

huangapple go评论94阅读模式
英文:

Creating table in android studio using recyclerview and ListItem

问题

I understand that you're facing an issue with your Android app where you're trying to display data from an API in a RecyclerView, but the data isn't showing up, and you're encountering a JSON parsing error. The error message you provided indicates that the response from your PHP script is not being parsed correctly as JSON. The issue seems to be with the PHP script itself, as it is echoing HTML tags before the JSON response.

To resolve this issue, you should modify your PHP script to only echo the JSON response without any HTML or other content. Here's the modified PHP script:

<?php 
function curl($url){
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $output = curl_exec($ch); 
    curl_close($ch);      
    return $output;
}

$send = curl("http://hrd.tvip.co.id/rest_server/api/absensi/index?shift_day=2020-08-24&shift_day_2=2020-08-27&badgenumber=0100028400");

// Output the JSON data directly without any other content
echo $send;
?>

By making this change, your PHP script will only output the JSON response from the API, and your Android app should be able to parse it correctly and display the data in your RecyclerView.

Remember to also check if your API request URL is correct and if the API is returning the expected JSON data. If the URL or API response is incorrect, it could also lead to the issue you're facing.

英文:

I want to create a table in android using ListItem and RecylerView. I already create it using API but it doesn't showing any data. here is my code

kehadiran.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.kehadiran&quot;&gt;

    &lt;RelativeLayout
        android:id=&quot;@+id/llViewTitle&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_centerHorizontal=&quot;true&quot;&gt;

        &lt;TextView
            android:id=&quot;@+id/kehadiran&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:textSize=&quot;32dp&quot;
            android:textColor=&quot;@color/colorPrimaryDark&quot;
            android:layout_centerHorizontal=&quot;true&quot;
            android:layout_marginTop=&quot;32dp&quot;
            android:layout_centerInParent=&quot;true&quot;
            android:text = &quot;Kehadiran&quot;
            /&gt;

    &lt;/RelativeLayout&gt;

    &lt;HorizontalScrollView
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@+id/llViewTitle&quot;
        android:layout_marginStart=&quot;8dp&quot;
        android:layout_marginLeft=&quot;8dp&quot;
        android:layout_marginTop=&quot;8dp&quot;
        android:layout_marginEnd=&quot;8dp&quot;
        android:layout_marginRight=&quot;8dp&quot;&gt;

        &lt;androidx.recyclerview.widget.RecyclerView
            android:id=&quot;@+id/recyclerViewDeliveryProductList&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:nestedScrollingEnabled=&quot;false&quot;
            tools:listitem=&quot;@layout/table_list_item&quot; /&gt;

    &lt;/HorizontalScrollView&gt;

&lt;/RelativeLayout&gt;

table_list_item.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:orientation=&quot;horizontal&quot;&gt;

    &lt;TextView
        android:id=&quot;@+id/txttglabsen&quot;
        android:layout_width=&quot;150dp&quot;
        android:layout_height=&quot;match_parent&quot;
        android:layout_gravity=&quot;top&quot;
        android:background=&quot;@drawable/table_content_cell_bg&quot;
        android:textSize=&quot;18sp&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/txtf1&quot;
        android:layout_width=&quot;180dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_gravity=&quot;top&quot;
        android:background=&quot;@drawable/table_content_cell_bg&quot;
        android:textSize=&quot;18sp&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/txtdepoawal&quot;
        android:layout_width=&quot;124dp&quot;
        android:layout_height=&quot;match_parent&quot;
        android:layout_gravity=&quot;top&quot;
        android:background=&quot;@drawable/table_content_cell_bg&quot;
        android:textSize=&quot;18sp&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/txtshiftmasuk&quot;
        android:layout_width=&quot;180dp&quot;
        android:layout_height=&quot;match_parent&quot;
        android:layout_gravity=&quot;top&quot;
        android:background=&quot;@drawable/table_content_cell_bg&quot;
        android:textSize=&quot;18sp&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/txtF4&quot;
        android:layout_width=&quot;180dp&quot;
        android:layout_height=&quot;match_parent&quot;
        android:layout_gravity=&quot;top&quot;
        android:background=&quot;@drawable/table_content_cell_bg&quot;
        android:textSize=&quot;18sp&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/txtDepoAkhir&quot;
        android:layout_width=&quot;128dp&quot;
        android:layout_height=&quot;match_parent&quot;
        android:layout_gravity=&quot;top&quot;
        android:background=&quot;@drawable/table_content_cell_bg&quot;
        android:textSize=&quot;18sp&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/txtKeluar&quot;
        android:layout_width=&quot;180dp&quot;
        android:layout_height=&quot;match_parent&quot;
        android:layout_gravity=&quot;top&quot;
        android:background=&quot;@drawable/table_content_cell_bg&quot;
        android:textSize=&quot;18sp&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/txtketerangan&quot;
        android:layout_width=&quot;128dp&quot;
        android:layout_height=&quot;match_parent&quot;
        android:layout_gravity=&quot;top&quot;
        android:background=&quot;@drawable/table_content_cell_bg&quot;
        android:textSize=&quot;18sp&quot; /&gt;

&lt;/LinearLayout&gt;

TableViewAdapter.java

package com.example.eis2.Item;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.eis2.R;
import java.util.List;

public class TableViewAdapter extends RecyclerView.Adapter {

    private List kehadiranlist;
    public TableViewAdapter(List kehadiranlist, Context applicationContext) {
        this.kehadiranlist = kehadiranlist;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.
                from(parent.getContext()).
                inflate(R.layout.table_list_item, parent, false);

        return new RowViewHolder(itemView);
    }

    @SuppressLint(&quot;SetTextI18n&quot;)
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        RowViewHolder rowViewHolder = (RowViewHolder) holder;

        int rowPos = rowViewHolder.getAdapterPosition();

        if (rowPos == 0) {

            rowViewHolder.txttglabsen.setBackgroundResource(R.drawable.table_header_cell_bg);
            rowViewHolder.txtf1.setBackgroundResource(R.drawable.table_header_cell_bg);
            rowViewHolder.txtdepoawal.setBackgroundResource(R.drawable.table_header_cell_bg);
            rowViewHolder.txtshiftmasuk.setBackgroundResource(R.drawable.table_header_cell_bg);
            rowViewHolder.txtF4.setBackgroundResource(R.drawable.table_header_cell_bg);
            rowViewHolder.txtDepoAkhir.setBackgroundResource(R.drawable.table_header_cell_bg);
            rowViewHolder.txtKeluar.setBackgroundResource(R.drawable.table_header_cell_bg);
            rowViewHolder.txtketerangan.setBackgroundResource(R.drawable.table_header_cell_bg);

            rowViewHolder.txttglabsen.setText(&quot;Tanggal Absen&quot;);
            rowViewHolder.txtf1.setText(&quot;F1&quot;);
            rowViewHolder.txtdepoawal.setText(&quot;Depo Awal&quot;);
            rowViewHolder.txtshiftmasuk.setText(&quot;Shift Masuk&quot;);
            rowViewHolder.txtF4.setText(&quot;F4&quot;);
            rowViewHolder.txtDepoAkhir.setText(&quot;Depo Akhir&quot;);
            rowViewHolder.txtKeluar.setText(&quot;Shift Keluar&quot;);
            rowViewHolder.txtketerangan.setText(&quot;keterangan&quot;);
        } else {
            keteranganmodel modal = (keteranganmodel) kehadiranlist.get(rowPos - 1);

            rowViewHolder.txttglabsen.setBackgroundResource(R.drawable.table_content_cell_bg);
            rowViewHolder.txtf1.setBackgroundResource(R.drawable.table_content_cell_bg);
            rowViewHolder.txtdepoawal.setBackgroundResource(R.drawable.table_content_cell_bg);
            rowViewHolder.txtshiftmasuk.setBackgroundResource(R.drawable.table_content_cell_bg);
            rowViewHolder.txtF4.setBackgroundResource(R.drawable.table_content_cell_bg);
            rowViewHolder.txtDepoAkhir.setBackgroundResource(R.drawable.table_content_cell_bg);
            rowViewHolder.txtDepoAkhir.setBackgroundResource(R.drawable.table_content_cell_bg);
            rowViewHolder.txtketerangan.setBackgroundResource(R.drawable.table_content_cell_bg);

            rowViewHolder.txttglabsen.setText(modal.getShift_day() + &quot;&quot;);
            rowViewHolder.txtf1.setText(modal.getF1());
            rowViewHolder.txtdepoawal.setText(modal.getDepo_f1() + &quot;&quot;);
            rowViewHolder.txtshiftmasuk.setText(modal.getWaktu_masuk() + &quot;&quot;);
            rowViewHolder.txtF4.setText(modal.getF4() + &quot;&quot;);
            rowViewHolder.txtDepoAkhir.setText(modal.getDepo_f4());
            rowViewHolder.txtKeluar.setText(modal.getWaktu_keluar() + &quot;&quot;);
            rowViewHolder.txtketerangan.setText(modal.getKet_absensi() + &quot;&quot;);
        }
    }

    @Override
    public int getItemCount() {
        return kehadiranlist == null ? 0 : kehadiranlist.size();
    }

    public static class RowViewHolder extends RecyclerView.ViewHolder {
        TextView txttglabsen;
        TextView txtf1;
        TextView txtdepoawal;
        TextView txtshiftmasuk;
        TextView txtF4;
        TextView txtDepoAkhir;
        TextView txtKeluar;
        TextView txtketerangan;

        RowViewHolder(View itemView) {
            super(itemView);
            txttglabsen = itemView.findViewById(R.id.txttglabsen);
            txtf1 = itemView.findViewById(R.id.txtf1);
            txtdepoawal = itemView.findViewById(R.id.txtdepoawal);
            txtshiftmasuk = itemView.findViewById(R.id.txtshiftmasuk);
            txtF4 = itemView.findViewById(R.id.txtF4);
            txtDepoAkhir = itemView.findViewById(R.id.txtDepoAkhir);
            txtKeluar = itemView.findViewById(R.id.txtKeluar);
            txtketerangan = itemView.findViewById(R.id.txtketerangan);
        }
    }
}

kehadiran.java

package com.example.eis2;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.eis2.Item.TableViewAdapter;
import com.example.eis2.Item.keteranganmodel;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.example.eis2.Item.LoginItem.ABSENSI_URL;

public class kehadiran extends AppCompatActivity {
    private List&lt;keteranganmodel&gt; absensi;
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kehadiran);

        RecyclerView recyclerView = findViewById(R.id.recyclerViewDeliveryProductList);

        TableViewAdapter adapter = new TableViewAdapter(getKehadiranlist(), getApplicationContext());

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);

        recyclerView.setAdapter(adapter);
        absensi = new ArrayList&lt;&gt;();
    }

    private List getKehadiranlist() {
        StringRequest stringRequest = new StringRequest(Request.Method.GET, ABSENSI_URL,
                new Response.Listener&lt;String&gt;() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject obj = new JSONObject(response);
                            JSONArray keteranganArray = obj.getJSONArray(&quot;data&quot;);

                            for (int i = 0; i &lt; keteranganArray.length(); i++) {

                                JSONObject keteranganobject = keteranganArray.getJSONObject(i);

                                keteranganmodel keterangan = new keteranganmodel(
                                        keteranganobject.getString(&quot;shift_day&quot;),
                                        keteranganobject.getString(&quot;f1&quot;),
                                        keteranganobject.getString(&quot;depo_f1&quot;),
                                        keteranganobject.getString(&quot;waktu_masuk&quot;),
                                        keteranganobject.getString(&quot;f4&quot;),
                                        keteranganobject.getString(&quot;depo_f4&quot;),
                                        keteranganobject.getString(&quot;waktu_keluar&quot;),
                                        keteranganobject.getString(&quot;ket_absensi&quot;));

                                absensi.add(keterangan);
                                System.out.println(&quot;data:&quot; + keterangan);
                            }
                            TableViewAdapter adapter = new TableViewAdapter(absensi, getApplicationContext());

                            list.setAdapter((ListAdapter) adapter);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
        return absensi;
    }
}

keteranganmodel.java

package com.example.eis2.Item;

public class keteranganmodel {
    private String shift_day;
    private String f1;
    private String depo_f1;
    private String waktu_masuk;
    private String f4;
    private String depo_f4;
    private String waktu_keluar;
    private String ket_absensi;

    public keteranganmodel(String shift_day, String f1, String depo_f1, String waktu_masuk, String f4, String depo_f4, String waktu_keluar, String ket_absensi) {
        this.shift_day = shift_day;
        this.f1 = f1;
        this.depo_f1 = depo_f1;
        this.waktu_masuk = waktu_masuk;
        this.f4 = f4;
        this.depo_f4 = depo_f4;
        this.waktu_keluar = waktu_keluar;
        this.ket_absensi = ket_absensi;
    }

    public String getShift_day() {
        return shift_day;
    }
    public void setShift_day(String shift_day) { this.shift_day = shift_day; }

    public String getF1() {
        return f1;
    }
    public void setF1(String f1) {
        this.f1 = f1;
    }

    public String getDepo_f1() {
        return depo_f1;
    }
    public void setDepo_f1(String depo_f1) {
        this.depo_f1 = depo_f1;
    }

    public String getWaktu_masuk() {return waktu_masuk;}
    public void setWaktu_masuk(String waktu_masuk) {this.waktu_masuk = waktu_masuk; }

    public String getF4() {
        return f4;
    }
    public void setF4(String f4) {
        this.f4 = f4;
    }

    public String getDepo_f4() {
        return depo_f4;
    }
    public void setDepo_f4(String depo_f4) {
        this.depo_f4 = depo_f4;
    }

    public String getWaktu_keluar() {
        return waktu_keluar;
    }
    public void setKet_absensi(String waktu_keluar) {
        this.waktu_keluar = waktu_keluar;
    }

    public String getKet_absensi() {
        return ket_absensi;
    }
    public void setWaktu_keluar(String ket_absensi) {
        this.ket_absensi = ket_absensi;
    }
}

and here is it's API

&lt;?php 
function curl($url){
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $output = curl_exec($ch); 
    curl_close($ch);      
    return $output;
}
$send = curl(&quot;http://hrd.tvip.co.id/rest_server/api/absensi/index?shift_day=2020-08-24&amp;shift_day_2=2020-08-27&amp;badgenumber=0100028400&quot;);

// mengubah JSON menjadi array
$data = json_decode($send, TRUE);

echo &quot;&lt;pre&gt;&quot;;
print_r($data);
echo &quot;&lt;/pre&gt;&quot;;
?&gt;

I tried every time but it always blank or no showing data

the main problem is

2020-08-31 10:18:16.245 11130-11130/com.example.eis2 W/System.err: org.json.JSONException: Value &lt;pre&gt;Array of type java.lang.String cannot be converted to JSONObject

答案1

得分: 2

根据您的 PHP 代码,您正在使用 echo &lt;pre&gt;,在您的 API 输出中也是如此, 请从您的代码中删除这两个 pre echo 语句,仅在 PHP 级别上使用它们来调试您的输出。

因此,在您的 PHP 代码中将会是这样的:

$data = json_decode($send, TRUE);
echo $data;
英文:

As per your php code you are doing echo &lt;pre&gt;, the same is also coming in output of your api, kindly remove both pre echo statements from your code, use them for debugging your output at php level only.

So in your php code will be like

$data = json_decode($send, TRUE);
echo $data;

huangapple
  • 本文由 发表于 2020年8月31日 11:08:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63664256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定