RecyclerView中的CardView不会充满列表中的所有值

时间:2016-07-01 14:16:05

标签: android android-recyclerview android-cardview

我有一个包含cardview的recyclerview。数据来自服务器并填充在列表中。 Cardview适配器不会从列表中膨胀第二个值。活动仅在屏幕上显示一个值: This is the cardview screen

以下是Recycler视图布局文件的代码:

<?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:orientation="vertical"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:scrollbars="vertical"
tools:context="com.appshaala.vorkal.app.ViewWorkerList">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical">

</android.support.v7.widget.RecyclerView>
</RelativeLayout>

以下是worker_card布局文件的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:card_view="http://schemas.android.com/tools"
 android:padding="16dp">

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/WorkerCV"
app:cardBackgroundColor="@color/colorPrimaryDark"
android:elevation="5dp"
card_view:cardCornerRadius="@dimen/card_album_radius">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="6dp"
    >

    <LinearLayout
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"
        android:padding="3dip" >

        <ImageView
            android:id="@+id/pic"
            android:layout_width="96dp"
            android:layout_height="96dp"
            android:src="@drawable/cover"
            android:contentDescription="Worker pic" />

    </LinearLayout>

    <TextView
        android:id="@+id/Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Raja"
        android:textSize="35sp"
        android:foregroundGravity="center"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/Wrating"
        android:layout_alignStart="@+id/Wrating"
        android:textAlignment="center" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="View Profile"
        android:id="@+id/button2"
        android:layout_below="@+id/thumbnail"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_toLeftOf="@+id/Wrating"
        android:layout_toStartOf="@+id/Wrating" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Call"
        android:id="@+id/button3"
        android:layout_alignTop="@+id/button2"
        android:layout_alignRight="@+id/Wrating"
        android:layout_alignEnd="@+id/Wrating" />

    <RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Wrating"
        style="?android:attr/ratingBarStyleIndicator"
        android:foregroundGravity="center"
        android:layout_below="@+id/Name"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:rating="3.5"
        android:numStars="5" />

</RelativeLayout>

</android.support.v7.widget.CardView>

</LinearLayout>

以下是工作程序适配器的代码:

public class WorkerAdapter extends RecyclerView.Adapter<WorkerAdapter.PersonViewHolder> {
private List<Worker> persons;

public static class PersonViewHolder extends RecyclerView.ViewHolder {
    CardView cv;
    TextView personName;
    RatingBar personRating;

    PersonViewHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.WorkerCV);
        personName = (TextView) itemView.findViewById(R.id.Name);
        personRating = (RatingBar) itemView.findViewById(R.id.Wrating);
    }
}

public WorkerAdapter(List<Worker> persons)
{
    this.persons = persons;
    Log.d("Size",""+persons.size());
}

@Override
public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.worker_card, parent, false);
    PersonViewHolder pvh = new PersonViewHolder(v);
    Log.d("here","ya");
    return pvh;
}

@Override
public void onBindViewHolder(PersonViewHolder holder, int position) {
    Log.d("Pos",""+position);
    holder.personName.setText(persons.get(position).getName());
    holder.personRating.setRating(persons.get(position).getRating());
}

@Override
public int getItemCount() {
    if (persons != null) {
        return persons.size();
    }
    return 0;
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

以下是ViewWorkerList的代码,该代码调用工作程序适配器并使回收器视图膨胀:

public class ViewWorkerList extends AppCompatActivity {

//Creating a List of workers
private List<Worker> listWorkers;

//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private WorkerAdapter adapter;
//  Json nodes
private static String KEY_SUCCESS = "success";
private static String KEY_NAME = "name";
private static String KEY_PHONE = "phoneno";

String url = "http://vorkal.com/read_data.php";
ArrayList<HashMap<String, String>> Item_List;
public static final String KEY_SERVICE = "service";

private String service;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Toast.makeText(getApplicationContext(),"2nd page",Toast.LENGTH_LONG);
    Log.d("2pg","2page");
    Intent intent = getIntent();
    service = intent.getStringExtra("service"); //if it's a string you stored.
    setContentView(R.layout.worker_list_main);

    //Initializing our workers list
    listWorkers = new ArrayList<>();

    Map<String,String> params = new HashMap<String, String>();
    params.put("tag", "get_list");
    params.put("service", service);
    StringRequest stringRequest = new StringRequest (Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        Log.d("response",response.toString());
                        JSONObject jsonObject=new JSONObject(response);

                        int success = jsonObject.getInt("success");
                        //int success=response.getInt(KEY_SUCCESS);
                     //   Log.d("response", response.getString("workers"));
                        Log.d("sucess",""+success);
                        String workerArray=jsonObject.getString("workers");
                        JSONArray jar=new JSONArray(workerArray);
                        JSONObject json = jar.getJSONObject(0);
                        Log.d("name",json.getString("name"));
                        parseData(jar);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show();
                    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

                    //recyclerView.setAdapter(new CardAdapter(listWorkers, this));
                    recyclerView.setHasFixedSize(true);
                    layoutManager = new LinearLayoutManager(getApplicationContext());
                    recyclerView.setLayoutManager(layoutManager);
                    //Finally initializing our adapter
                    Log.d("listworkers",listWorkers.get(1).getName());
                    adapter = new WorkerAdapter(listWorkers);
                    recyclerView.setAdapter(adapter);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                   Log.d("error",error.toString());
                    Toast.makeText(getApplicationContext(),error.toString(),Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put("tag", "get_list");
            params.put("service", service);
            return params;
        }
   };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}


//This method will parse json data
private void parseData(JSONArray array){
    for(int i = 0; i<array.length(); i++) {
        Worker worker = new Worker();
        JSONObject json = null;
        try {
            json = array.getJSONObject(i);
            worker.setImageUrl(json.getString("pic"));
            worker.setName(json.getString("name"));
            worker.setLocation(json.getString("location"));
            worker.setRating(json.getInt("rating"));
            worker.setId(json.getInt("id"));
            worker.setPhone(json.getInt("phonenumber"));
            worker.setOccupation(json.getString("occupation"));
            worker.setPrice(json.getInt("price"));
            worker.setReview(json.getString("Review"));

        } catch (JSONException e) {
            e.printStackTrace();
        }
        listWorkers.add(worker);
    }

}
}

listWorkers包含2名工作人员。但是只有一个人在卡片视频中填充。 worker是普通的bean类。我无法在屏幕上看到第二张工作卡。请帮助我并建议更改。

0 个答案:

没有答案