我是回收站视图小部件的新手,我想从我的api网址中获取数据并将数据列出到回收站视图中,我具有以下类:recyclerAdapter
,recyclerView
,{{1 }},MainActivity
。
数据已成功从api加载,但未在recycler视图中设置从api接收到的json字符串,并且recycler视图显示为白色和空。任何人都可以在我的代码中找到问题吗?
MainActivity.java代码是:
row
recyclerView.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);
}
}
}
public class recyclerView {
private String hazine,tarikh,tozihat,madarkharj,harSahm;
public recyclerView(String hazine,String tarikh,String tozihat,String madarkharj,String harSahm ) {
this.hazine = hazine;
this.harSahm = harSahm;
this.madarkharj = madarkharj;
this.tarikh = tarikh;
this.tozihat = tozihat;
}
public String getHazine(){return hazine;}
public String getTarikh(){return tarikh;}
public String getTozihat(){return tozihat;}
public String getMadarkharj(){return madarkharj;}
public String getHarSahm(){return harSahm;}
回收站适配器类是这样的:
public class recyclerAdapter extends RecyclerView.Adapter<recyclerAdapter.ViewHolder>{
public static final String KEY_NAME = "name";
public static final String KEY_LASTNAME = "lastname";
public static final String KEY_IMAGE = "image";
public static final String KEY_HAZINE = "hazine";
public static final String KEY_TARIKH = "date";
public static final String KEY_MADARKHARJ = "member_id";
private List<recyclerView> cashlist;
private Context context;
//在这个类中,我们将为ViewHolder提供另一个类
public recyclerAdapter(List<recyclerView> cashlist, Context context){
this.cashlist = cashlist;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.row,viewGroup,false);
return new recyclerAdapter.ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
final recyclerView recyclerviews = cashlist.get(position);
viewHolder.hazine.setText(recyclerviews.getHazine());
viewHolder.harSahm.setText(recyclerviews.getHarSahm());
viewHolder.tarikh.setText(recyclerviews.getTarikh());
viewHolder.tozihat.setText(recyclerviews.getTozihat());
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "TEST is Toasted", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return cashlist.size();
}
}
这是row.xml:
public class ViewHolder extends RecyclerView.ViewHolder{
TextView hazine,harSahm,tozihat,tarikh;
public ViewHolder(@NonNull View itemView) {
super(itemView);
hazine = (TextView) itemView.findViewById(R.id.mablaq_hazine);
harSahm = (TextView) itemView.findViewById(R.id.bedehkariha_dong);
tarikh = (TextView) itemView.findViewById(R.id.tarikh_hazine);
tozihat = (TextView) itemView.findViewById(R.id.babate_hazine);
}
}
答案 0 :(得分:2)
我首先解析问题。更改
JSONArray array = jsonObject.getJSONArray("bedehkariHa");
收件人:
JSONArray array = jsonObject.getJSONArray("bedehkari_ha");
然后检查
答案 1 :(得分:0)
根据JSON的结果,键 bedehkariHa 不存在,可能会导致解析错误,即JSONException。所以代替这个:
JSONArray array = jsonObject.getJSONArray("bedehkariHa");
使用它来获取与 bedehkari_ha 相关的JSONArray:
JSONArray array = jsonObject.getJSONArray("bedehkari_ha");
答案 2 :(得分:0)
我首先要看一下您的一般方法。 您似乎正在实例化Volley请求...我敢打赌这不是您的应用程序中唯一的请求吗?
创建一个“ NetworkService” 单例,其中包含应用程序上下文并将其称为服务层...而不是为您需要的每个类调用新的Volley实例!
这是我为我的应用程序写的一个...我省略了很多方法,而给了你一个GetRequest方法。您可以在服务中添加参数的URLEncoding。
您可以简单地在活动/片段中调用它:
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);
}