2017-07-27 86 views
0

我使用RecyclerView製作了一個列表,並使用CardView自定義列表。
下面的代碼:如何在彈出對話框中顯示Recycler視圖?

activity_caddy.xml(在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

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); 
     } 
    } 
} 

,我想通過點擊buttoncaddy但是當我按一下按鈕,還有的只顯示一個白色的空白的彈出窗口,以顯示我的同成形彈出..

下面的代碼:

package com.example.user.ayogolf; 

import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.DatePicker; 
import android.widget.Spinner; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.example.user.ayogolf.Retrofit.ApiServices; 
import com.example.user.ayogolf.Retrofit.Caddy; 
import com.example.user.ayogolf.Retrofit.InitRetrofit; 
import com.example.user.ayogolf.Retrofit.ResponseServer; 

import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.Locale; 

import butterknife.ButterKnife; 
import butterknife.InjectView; 
import butterknife.OnClick; 
import retrofit2.Call; 
import retrofit2.Callback; 
import retrofit2.Response; 


public class CoFormActivity extends AppCompatActivity { 

    @InjectView(R.id.txtcourse) 
    TextView txtcourse; 
    @InjectView(R.id.spinnerhour) 
    Spinner spinnerhour; 
    @InjectView(R.id.txthour) 
    TextView txthour; 
    @InjectView(R.id.spinnerplayer) 
    Spinner spinnerplayer; 
    @InjectView(R.id.txtplayer) 
    TextView txtplayer; 
    @InjectView(R.id.checkBox3) 
    CheckBox checkBox3; 
    @InjectView(R.id.button20) 
    Button button20; 
    @InjectView(R.id.txtprice) 
    TextView txtprice; 
    @InjectView(R.id.txtn) 
    TextView txtn; 
    @InjectView(R.id.buttoncaddy) 
    Button buttoncaddy; 
    private DatePickerDialog datePickerDialog; 
    private SimpleDateFormat dateFormatter; 
    private TextView tvDateResult; 
    private Button btDatePicker; 

    String[] hour = {"--Hour--", "06:00", "06:30", "07:00", "07:30", "12:00", "12:30", "13:00", "13:30"}; 
    String[] player = {"--Player--", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_co_form); 
     ButterKnife.inject(this); 

     Intent in = getIntent(); 
     //buat variabel untuk menangkap string yang dipindahkan dari activity lain 
     String course = in.getStringExtra("course"); 
     String price = in.getStringExtra("price"); 
     txtcourse.setText(course); 
     txtprice.setText(price); 
     ArrayAdapter adapterhour = new ArrayAdapter(this, android.R.layout.simple_spinner_item, hour); 
     spinnerhour.setAdapter(adapterhour); 

     spinnerhour.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       txthour.setText(hour[position]); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 

      } 
     }); 

     ArrayAdapter adapterplayer = new ArrayAdapter(this, android.R.layout.simple_spinner_item, player); 
     spinnerplayer.setAdapter(adapterplayer); 

     spinnerplayer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       txtplayer.setText(player[position]); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 

      } 
     }); 

     dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US); 

     tvDateResult = (TextView) findViewById(R.id.tv_dateresult); 
     btDatePicker = (Button) findViewById(R.id.bt_datepicker); 
     btDatePicker.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       showDateDialog(); 
      } 
     }); 

    } 

    private void showDateDialog() { 

     Calendar newCalendar = Calendar.getInstance(); 

     datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { 

      @Override 
      public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 

       /** 
       * Method ini dipanggil saat kita selesai memilih tanggal di DatePicker 
       */ 

       /** 
       * Set Calendar untuk menampung tanggal yang dipilih 
       */ 
       Calendar newDate = Calendar.getInstance(); 
       newDate.set(year, monthOfYear, dayOfMonth); 

       /** 
       * Update TextView dengan tanggal yang kita pilih 
       */ 
       tvDateResult.setText(dateFormatter.format(newDate.getTime())); 
      } 

     }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH)); 

     /** 
     * Tampilkan DatePicker dialog 
     */ 
     datePickerDialog.show(); 
    } 

    @OnClick({R.id.checkBox3, R.id.button20, R.id.buttoncaddy}) 
    public void onViewClicked(View view) { 
     switch (view.getId()) { 
      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); 
       rvcaddy.setAdapter(adapter); 
       RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
       rvcaddy.setLayoutManager(mLayoutManager); 
       dialog.show(); 

       break; 
      case R.id.checkBox3: 
       //create dialog 
       //final Dialog dialog = new Dialog(CoFormActivity.this); 
       //set layout custom 
       //dialog.setContentView(R.layout.activity_caddy); 

       //dialog.show(); 

       break; 
      case R.id.button20: 
       String hour = txthour.getText().toString(); 
       String player = txtplayer.getText().toString(); 
       String price = txtprice.getText().toString(); 
       if (hour.equalsIgnoreCase("--Hour--")) { 
        Toast.makeText(this, "Please Choose Time!", Toast.LENGTH_SHORT).show(); 
       } else if (player.equalsIgnoreCase("--Player--")) { 
        Toast.makeText(this, "Please Choose Number of Player!", Toast.LENGTH_SHORT).show(); 
       } else { 
        int player2; 
        player2 = Integer.parseInt(player); 
        int price2 = Integer.parseInt(price); 
        int total = price2 * player2; 
        String total2 = String.valueOf(total).toString(); 
        String price3 = String.valueOf(price2).toString(); 

        //pindah activity dan mengangkut data 
        Intent intent = new Intent(CoFormActivity.this, ReceiptActivity.class); 
        //untuk mengangkut data dari class ke class lain 
        intent.putExtra("course", txtcourse.getText().toString()); 
        intent.putExtra("date", tvDateResult.getText().toString()); 
        intent.putExtra("time", txthour.getText().toString()); 
        intent.putExtra("player", txtplayer.getText().toString()); 
        intent.putExtra("price", price3); 
        intent.putExtra("total", total2); 
        if (checkBox3.isChecked()) { 
         intent.putExtra("caddy", "YES"); 
        } else { 
         intent.putExtra("caddy", "NO"); 
        } 
        //eksekusi intent 
        startActivity(intent); 
       } 
       break; 
     } 
    } 

} 

我應該怎麼做才能顯示彈出式數據列表?我應該修理什麼東西?

回答

0

在設置回收視圖時,您確實已將任何項目添加到列表中。你剛剛創建了這樣的ArrayList<Caddy> data = new ArrayList<>();,並將其添加到列表中,因此大小將始終爲「0」。

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;