我如何在recyclerview中的setadapter中传递多个数组

时间:2018-08-28 19:17:58

标签: android android-recyclerview

因此,我有一个包含表请求的数据库,其中包含其他用户发布的请求,该请求将在管理员部分中列出,管理员将在该部分批准或拒绝该请求。

每当管理员打开请求页面时,我要提取将以json格式存储在android中的数据。我的问题是我如何传递json数组作为参数并将其设置在recyclerview中,并为每个视图分配ID,以便每当单击检查或拒绝时,特定请求都会被拒绝。以及如何动态设置每个视图的onclicklistener。下面是我要复制的视图

View i want to replicate          

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:orientation="vertical"
    android:weightSum="1"
    android:paddingHorizontal="15dp"
    android:paddingVertical="5dp">


    <TextView
        android:id="@+id/request_u_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Faculty Name"
        android:textColor="@color/faculty_name"
        android:textSize="25sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/request_u_desig"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Designation"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/request_u_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Date to Date"
        android:textColor="@android:color/background_dark" />

    <TextView
        android:id="@+id/request_u_reason"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="reason"
        android:textColor="@android:color/background_dark" />

    <TextView
        android:id="@+id/request_u_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="address"
        android:textColor="@android:color/background_dark" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_marginTop="5dp"
        android:orientation="horizontal"
        >
        <ImageView
            android:id="@+id/request_u_tick"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:srcCompat="@drawable/ic_check_black_24dp"
            tools:ignore="VectorDrawableCompat" />

        <ImageView
            android:id="@+id/request_u_cross"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:srcCompat="@drawable/ic_close_black_24dp"
            tools:ignore="VectorDrawableCompat" />
    </LinearLayout>

  </LinearLayout>

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

这是request_hod.java文件。

package com.roastergmail.recycletest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;

 public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RecyclerView proview = (RecyclerView) findViewById(R.id.prolist);
    proview.setLayoutManager(new LinearLayoutManager(this));
    String[] lang = {"Java", "kava", "Pava", "Mava", "Lava", "sava"};
    proview.setAdapter(new proadapter(lang));

}
}

这是proadapter.java文件

   package com.roastergmail.roastergmain;

 import android.annotation.SuppressLint;
 import android.support.annotation.NonNull;
 import android.support.v7.widget.RecyclerView;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.EditText;
 import android.widget.ImageView;
 import android.widget.TextView;

 public class proadapter extends RecyclerView.Adapter<proadapter.proviewholder> {

private String[][] data;
proadapter(String[][] data){
    this.data= data;
}
@NonNull
@Override
public proviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.customform,parent,false);
    return new proviewholder(view);
}

@SuppressLint("ResourceType")
@Override
public void onBindViewHolder(@NonNull proviewholder holder, int position) {
    String faculty_name = "dummy";
    holder.faculty_name.setText("dummy");
    holder.faculty_name.setId(1);
}

@Override
public int getItemCount() {
    return data.length;
}

public class proviewholder extends RecyclerView.ViewHolder {
    TextView faculty_name, faculty_designation, date, reason, address;
    ImageView tick, cross;

    public proviewholder(View itemView) {
        super(itemView);
        faculty_name  = (TextView) 
   itemView.findViewById(R.id.request_u_name);
        faculty_designation  = (TextView) 
  itemView.findViewById(R.id.request_u_desig);
        date  = (TextView) itemView.findViewById(R.id.request_u_date);
        reason  = (TextView) itemView.findViewById(R.id.request_u_reason);
        address  = (TextView) itemView.findViewById(R.id.request_u_address);
        tick = (ImageView) itemView.findViewById(R.id.request_u_tick);
        cross = (ImageView) itemView.findViewById(R.id.request_u_cross);
        }
    }
}

0 个答案:

没有答案