android:回收器视图适配器
我不熟悉recyclerView小部件,我想从api url获取数据并将数据列在我的recyclerView中,我有以下类:android:回收器视图适配器,android,android-recyclerview,adapter,Android,Android Recyclerview,Adapter,我不熟悉recyclerView小部件,我想从api url获取数据并将数据列在我的recyclerView中,我有以下类:recyclerAdapter,recyclerView,MainActivity,row 数据已成功从api加载,但未在recycler视图中设置从api接收的json字符串,并且recycler视图显示为白色和空。任何人都可以在我的代码中找到问题? MainActivity.java代码是: public class MainActivity extends AppCo
recyclerAdapter
,recyclerView
,MainActivity
,row
数据已成功从api加载,但未在recycler视图中设置从api接收的json字符串,并且recycler视图显示为白色和空。任何人都可以在我的代码中找到问题?
MainActivity.java代码是:
public class MainActivity extends AppCompatActivity {
final String member_id = "2";
final String get_cash_url = "http://famila1.ir/khabgah/get_khabgah_cash.php?member_id=" + member_id;
private RecyclerView rc;
private RecyclerView.Adapter adapter;
private List<recyclerView> cashList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rc = findViewById(R.id.rc);
rc.setHasFixedSize(true);
rc.setLayoutManager(new LinearLayoutManager(this));
cashList = new ArrayList<>();
loadUrlData();
}
private void loadUrlData() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
get_cash_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response);
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("bedehkariHa");
for (int i = 0; i < array.length(); i++) {
JSONObject jo = array.getJSONObject(i);
recyclerView temp = new recyclerView(jo.getString("hazine"), jo.getString("tarikh"), jo.getString("tozihat"), jo.getString("member_id"), "kir");
cashList.add(temp);
}
adapter = new recyclerAdapter(cashList, getApplicationContext());
rc.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
progressDialog.setMessage("somthing was wrong during runnig app...");
progressDialog.show();
System.out.println("error: "+error);
}
}
);
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
}
}
这是row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:background="@android:color/white" >
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:padding="5dp"
android:background="@android:color/holo_blue_bright" android:layout_marginBottom="5dp"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="3dp"
android:background="@android:color/holo_blue_dark">
<TextView
android:text="سهم شما:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/tarikh_hazine2"
android:textSize="18sp" android:textColor="@android:color/holo_red_dark"
android:textStyle="bold"
/>
<TextView
android:text="قالب پرداخت"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/bedehkariha_dong"
android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"/>
<TextView
android:text="مبلغ کل: "
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/mablaq_hazine2"
android:textSize="18sp" android:textColor="@android:color/holo_red_dark"
android:textStyle="bold"/>
<TextView
android:text="مبلغ کل"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/mablaq_hazine" android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="3dp"
android:background="@android:color/holo_blue_dark">
<TextView
android:text="تاریخ:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/tarikh_hazine22"
android:textSize="18sp" android:textColor="@android:color/holo_red_dark"
android:textStyle="bold"
/>
<TextView
android:text="تاریخ"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/tarikh_hazine" android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"
android:inputType="textMultiLine"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="3dp"
android:background="@android:color/holo_blue_dark">
<TextView
android:text="توضیحات: "
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/babate_hazine2"
android:textSize="18sp" android:textColor="@android:color/holo_red_dark"
android:textStyle="bold"
/>
<TextView
android:text="هزینه بابت......"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/babate_hazine" android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"
android:inputType="textMultiLine"
android:gravity="right"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_marginTop="2dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_weight="1" android:padding="2dp">
<TextView
android:text="پرداخت از: "
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/pardakhtaz" android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"
android:fadingEdge="none"
android:background="@android:color/white" android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/pardakhtaz1" android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#FF75AD06" android:padding="2dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/pardakhtaz2" android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="@android:color/holo_green_dark" android:padding="2dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/pardakhtaz3" android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#FF75AD06" android:padding="2dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/pardakhtaz4" android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="@android:color/holo_green_dark" android:padding="2dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/pardakhtaz5" android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#75ad06" android:padding="2dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/pardakhtaz6" android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="@android:color/holo_green_dark" android:padding="2dp"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_weight="1" android:padding="2dp">
<TextView
android:text="هزینه شریکی با:"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/moshtarak_ba"
android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"
android:fadingEdge="none"
android:background="@android:color/white" android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/moshtarak_ba1"
android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#FFF10D0D" android:padding="2dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/moshtarak_ba2"
android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="@android:color/holo_red_dark" android:padding="2dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/moshtarak_ba3"
android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#FFF10D0D" android:padding="2dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/moshtarak_ba4"
android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="@android:color/holo_red_dark" android:padding="2dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/moshtarak_ba5"
android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#FFF10D0D" android:padding="2dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/moshtarak_ba6"
android:layout_weight="1"
android:textSize="18sp" android:textColor="@android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="@android:color/holo_red_dark" android:padding="2dp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
我首先遇到解析问题。更改
JSONArray array = jsonObject.getJSONArray("bedehkariHa");
致:
然后首先检查我遇到的解析问题。更改
JSONArray array = jsonObject.getJSONArray("bedehkariHa");
致:
然后根据JSON的结果检查,键bedehkariHa不存在,这可能导致解析错误,即JSONException。因此,与此相反:
JSONArray array = jsonObject.getJSONArray("bedehkariHa");
使用此命令获取与bedehkari_ha关联的JSONArray:
根据JSON的结果,密钥bedehkariHa不存在,这可能导致解析错误,即JSONException。因此,与此相反:
JSONArray array = jsonObject.getJSONArray("bedehkariHa");
使用此命令获取与bedehkari_ha关联的JSONArray:
我先看看你的一般做法。 你似乎正在实例化一个截击请求…我打赌这不是你应用程序中唯一的一个 制作一个包含应用程序上下文的“NetworkService”singleton,并将其作为服务层调用……而不是为您需要的每个类调用一个新的Volley实例 这里是我为我的应用程序写的一个…我省略了很多方法,留给你们一个GetRequest方法。您可以在服务中添加参数的URL编码 您只需在活动/片段中调用:
HashMap<String,String> urlParams = new HashMap<>();
urlParams.put("param1","value1");
VolleyRequestService.getInstance(this).getJSONObject(urlParams, true, new GetJSONObjectRequestable() {
@Override
public void onSuccessfulAPIResponse(JSONObject response) {
}
@Override
public void onFailedAPIResponse(VolleyRequestService.ErrorType errorType, String description) {
}
},"https://myendpoint.com/endpoint.php");
我先看看你的一般做法。 你似乎正在实例化一个截击请求…我打赌这不是你应用程序中唯一的一个 制作一个包含应用程序上下文的“NetworkService”singleton,并将其作为服务层调用……而不是为您需要的每个类调用一个新的Volley实例 这里是我为我的应用程序写的一个…我省略了很多方法,留给你们一个GetRequest方法。您可以在服务中添加参数的URL编码 您只需在活动/片段中调用:
HashMap<String,String> urlParams = new HashMap<>();
urlParams.put("param1","value1");
VolleyRequestService.getInstance(this).getJSONObject(urlParams, true, new GetJSONObjectRequestable() {
@Override
public void onSuccessfulAPIResponse(JSONObject response) {
}
@Override
public void onFailedAPIResponse(VolleyRequestService.ErrorType errorType, String description) {
}
},"https://myendpoint.com/endpoint.php");
它是否显示错误?异常?是否显示错误?例外?
JSONArray array = jsonObject.getJSONArray("bedehkariHa");
JSONArray array = jsonObject.getJSONArray("bedehkari_ha");
HashMap<String,String> urlParams = new HashMap<>();
urlParams.put("param1","value1");
VolleyRequestService.getInstance(this).getJSONObject(urlParams, true, new GetJSONObjectRequestable() {
@Override
public void onSuccessfulAPIResponse(JSONObject response) {
}
@Override
public void onFailedAPIResponse(VolleyRequestService.ErrorType errorType, String description) {
}
},"https://myendpoint.com/endpoint.php");
package devdroidjonno.com.tests.Networking;
import android.content.Context;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import java.util.HashMap;
import java.util.Map;
public class VolleyRequestService {
private static final String DEBUG = "VolleyRequestService";
private static final String authCode = "87fe987vwefb987eb9w87wv";
private static VolleyRequestService ourInstance = null;
private static RequestQueue requestQueue;
private VolleyRequestService(Context applicationContext) {
this.requestQueue = Volley.newRequestQueue(applicationContext);
Log.d(DEBUG, "Init Volley Service " + this.hashCode());
}
public static VolleyRequestService getInstance(Context context) {
if (ourInstance == null) {
ourInstance = new VolleyRequestService(context.getApplicationContext());
}
return ourInstance;
}
// A GET request to an endpoint that returns a JSONObject.
public void getJSONObject(HashMap<String, String> urlParams, final boolean requiresAuthHeader, final GetJSONObjectRequestable callback, String endpointURL) {
endpointURL += "?";
for (Map.Entry mapEntry : urlParams.entrySet()) {
endpointURL += mapEntry.getKey() + "=" + mapEntry.getValue() + "&";
}
Log.d(DEBUG, endpointURL);
requestQueue.add(new JsonObjectRequest(Request.Method.GET, endpointURL, null, response -> {
Log.d(DEBUG, response.toString());
try {
if (response.has("error")) {
callback.onFailedAPIResponse(ErrorType.APIERROR, response.getString("error"));
} else {
callback.onSuccessfulAPIResponse(response);
}
} catch (Exception e) {
callback.onFailedAPIResponse(ErrorType.EXCEPTION, e.getLocalizedMessage());
e.printStackTrace();
}
}, error -> callback.onFailedAPIResponse(ErrorType.VOLLEYERROR, error.getMessage())) {
@Override
public Map<String, String> getHeaders() {
HashMap<String, String> headers = new HashMap<>();
if (requiresAuthHeader) headers.put("Authorization", authCode);
return headers;
}
});
}
public enum ErrorType {
NOERROR, APIERROR, VOLLEYERROR, AUTHERROR, EXCEPTION
}
}
package devdroidjonno.com.tests.Networking;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
public interface GetJSONObjectRequestable {
void onSuccessfulAPIResponse(JSONObject response) throws JSONException, IOException;
void onFailedAPIResponse(VolleyRequestService.ErrorType errorType, String description);
}