如何在弹出对话框上显示Recycler View?

时间:2017-07-27 04:55:01

标签: android arraylist android-recyclerview recycler-adapter

我使用2017-07-27 03:58:53.643+0000 INFO [o.n.k.AvailabilityGuard] Fulfilling of requirement makes database available: Database available 2017-07-27 03:58:53.644+0000 INFO [o.n.k.i.f.GraphDatabaseFacadeFactory] Database is now ready 创建了一个列表,并使用RecyclerView自定义列表 这是代码:

activity_caddy.xml CardView):

RecyclerView

<?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="match_parent" android:background="@android:color/white" android:orientation="vertical" tools:context="com.example.user.ayogolf.CaddyActivity"> <android.support.v7.widget.RecyclerView android:layout_width="368dp" android:layout_height="551dp" android:id="@+id/rvcaddy" tools:layout_editor_absoluteY="8dp" tools:layout_editor_absoluteX="8dp"></android.support.v7.widget.RecyclerView> </LinearLayout>

activity_custom_list_caddy.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:background="@android:color/white" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" app:cardBackgroundColor="@color/cardview_light_background" app:cardCornerRadius="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/imgcaddy" android:layout_width="96dp" android:layout_height="96dp" android:layout_margin="6dp" android:src="@drawable/no_photo" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/txtcaddy_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:text="Caddy Name" android:textSize="18sp" android:textStyle="bold" android:textColor="@android:color/black"/> <TextView android:id="@+id/txtgender" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:text="Gender" android:textSize="15sp" android:textColor="@android:color/black"/> </LinearLayout> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout> 这样的适配器:

RecyclerView

CaddyAdapter.java

我希望通过点击buttoncaddy在我的CoForm上显示弹出窗口但是当我点击按钮时,只显示一个白色的空白弹出窗口..

这里是代码:

package com.example.user.ayogolf;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.user.ayogolf.Retrofit.Caddy;

import java.util.ArrayList;

/**
 * Created by arifina on 7/26/2017.
 */

public class CaddyAdapter extends RecyclerView.Adapter<CaddyAdapter.MyHolder>{

    ArrayList<Caddy> data;
    RecyclerView r;
    Context c;
    int resource;

    public CaddyAdapter(ArrayList<Caddy> data, Context c, int resource) {
        this.data = data;
        //this.r = r;
        this.c = c;
        this.resource=resource;
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //array position
            int posisi = r.getChildLayoutPosition(v);
            Intent intent = new Intent(c,CoFormActivity.class);
            intent.putExtra("pic",data.get(posisi).getCaddy_pic());
            intent.putExtra("name",data.get(posisi).getCaddy_name());
            intent.putExtra("gender",data.get(posisi).getGender());
            c.startActivity(intent);
        }
    };
    @Override
    public CaddyAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.activity_custom_list_caddy,parent,false);

        v.setOnClickListener(listener);
        return new MyHolder(v);
    }

    @Override
    public void onBindViewHolder(CaddyAdapter.MyHolder holder, int position) {
        holder.textnama.setText(data.get(position).getCaddy_name());
        holder.textgender.setText(data.get(position).getGender());
        Glide.with(c)
                .load("http://192.168.2.32/ayogolf/uploads/caddy/"+data.get(position).getCaddy_pic())
                .into(holder.image);
    }

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

    public class MyHolder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView textnama;
        TextView textgender;
        public MyHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.imgcaddy);
            textnama = (TextView) itemView.findViewById(R.id.txtcaddy_name);
            textgender = (TextView) itemView.findViewById(R.id.txtgender);
        }
    }
}

我该怎么做才能向我的弹出窗口显示数据列表?我该修什么?

1 个答案:

答案 0 :(得分:0)

您在设置回收视图时确实将任何项目添加到列表中。您刚刚创建了这个ArrayList<Caddy> data = new ArrayList<>();,并且您已添加到列表中,因此大小将始终为&#34; 0&#34;。

   case R.id.buttoncaddy:
        //create dialog
        final Dialog dialog = new Dialog(CoFormActivity.this);
        //set layout custom
        dialog.setContentView(R.layout.activity_caddy);
        final RecyclerView rvcaddy = (RecyclerView) dialog.findViewById(R.id.rvcaddy);

        ArrayList<Caddy> data = new ArrayList<>();
        CaddyAdapter adapter = new CaddyAdapter(data, this, R.layout.activity_caddy);
         // Add some item here to show the list.
        rvcaddy.setAdapter(adapter);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        rvcaddy.setLayoutManager(mLayoutManager);
        dialog.show();

        break;
相关问题