我刚开始学习android,
点击button
(MainActivity)
,第二项活动
(SecondActivity)
加载具有cardview图像,点击另一个活动
activity_swami
打开,必须将网址(json
格式)中的数据加载到textview
(CARDVIEW
)。但它显示空白活动。
本地静态数据显示。
我在
recyclerview
代码
activity_recycle
我的文件是 swami.java
package com.example.vinay.scroll;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class swami extends AppCompatActivity {
// private static final String URL_DATA="http://192.168.31.152/MyApi/index.php";
private static final String URL_DATA="https://vinayshirashyad.000webhostapp.com/index.php";
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<ActivitySwami> activitySwamiList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycle);
recyclerView = (RecyclerView) findViewById(R.id.recycle);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setHasFixedSize(true);
activitySwamiList = new ArrayList<>();
loadRecyclerViewData();
}
private void loadRecyclerViewData(){
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading Data");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
URL_DATA,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("swami");
for(int i=0;i<array.length();i++) {
JSONObject o = array.getJSONObject(i);
ActivitySwami activitySwami = new ActivitySwami(
o.getString("name"), o.getString("description"))
}
adapter = new swamiAdapter(activitySwamiList,getApplicationContext());
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(),error.getMessage(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
适配器类swamiAdapter.java
package com.example.vinay.scroll;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
/**
* Created by vinay on 3/16/2018.
*/
public class swamiAdapter extends RecyclerView.Adapter<swamiAdapter.ViewHolder> {
private List<ActivitySwami> activitySwamiList;
private Context context;
public swamiAdapter(List<ActivitySwami> activitySwamiList, Context context) {
this.activitySwamiList = activitySwamiList;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.activity_swami,parent,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ActivitySwami activitySwami = activitySwamiList.get(position);
// holder.id.setText(activitySwami.getId());
holder.name.setText(activitySwami.getName());
holder.desc.setText(activitySwami.getDesc());
}
@Override
public int getItemCount() {
return activitySwamiList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
//public TextView id;
public TextView name;
public TextView desc;
public ViewHolder(View itemView) {
super(itemView);
// id = itemView.findViewById(R.id.id);
name = itemView.findViewById(R.id.name);
desc = itemView.findViewById(R.id.desc);
}
}
}
activity_recycle.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
tools:context="com.example.vinay.scroll.swami">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
activity_swami.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.vinay.scroll.swami"
android:orientation="vertical">
<android.support.v7.widget.CardView
app:elevation="25dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_gravity="center"
android:layout_margin="5dp"
app:cardCornerRadius="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/cardview_default_elevation">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="name"/>
<TextView
android:id="@+id/desc"
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="description"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
答案 0 :(得分:2)
您忘记在 activitySwamiList
ArrayList
尝试以下代码
for(int i=0;i<array.length();i++) {
JSONObject o = array.getJSONObject(i);
ActivitySwami activitySwami = new ActivitySwami(
o.getString("name"), o.getString("description"));
activitySwamiList.add(activitySwami)
}
答案 1 :(得分:1)
将数据添加到Arraylist
activitySwamiList.add(activitySwami)