我只是一个创建的Recycler View包含CardsView我已经创建了一个项目和模型和适配器,我还设置了适配器这个Recycelr View显示为警告对话框我已经填充了这个循环视图但它显示为空这样 这是MainActivity.Java
package yourneeds.find.com.modifieritemsalertdialog;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Button click;
private List<ItemModifierModel> Modifiers;
RecyclerView modifierItemList;
Context context;
public static ItemModifierAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click = (Button) findViewById(R.id.button);
initializeData();
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Button cancelBtn, okBtn;
LayoutInflater inflater2 = MainActivity.this.getLayoutInflater();
View view2 = inflater2.inflate(R.layout.remove_item_alert_dialog, null);
modifierItemList = (RecyclerView) view2.findViewById(R.id.ipcET);
modifierItemList.setHasFixedSize(true);
modifierItemList.setLayoutManager(new LinearLayoutManager(MainActivity.this));
LinearLayoutManager llm = new LinearLayoutManager(MainActivity.this);
modifierItemList.setLayoutManager(llm);
okBtn = (Button) view2.findViewById(R.id.okBtn);
cancelBtn = (Button) view2.findViewById(R.id.cancelBtn);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
modifierItemList.setAdapter(adapter);
builder.setView(view2);
builder.setCancelable(false);
final AlertDialog alert11 = builder.create();
okBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alert11.dismiss();
}
});
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alert11.dismiss();
}
});
alert11.show();
}
});
}
List<ItemModifierModel> ItemModifierModels;
private void initializeData(){
Modifiers = new ArrayList<>();
Modifiers.add(new ItemModifierModel("204", "Regular"));
Modifiers.add(new ItemModifierModel("205", "Regular"));
Modifiers.add(new ItemModifierModel("206", "Regular"));
Modifiers.add(new ItemModifierModel("207", "Regular"));
Modifiers.add(new ItemModifierModel("208", "Regular"));
Modifiers.add(new ItemModifierModel("209", "Regular"));
Modifiers.add(new ItemModifierModel("210", "Regular"));
Modifiers.add(new ItemModifierModel("211", "Regular"));
Modifiers.add(new ItemModifierModel("212", "Regular"));
}
private void initializeAdapter(){
ItemModifierAdapter adapter = new ItemModifierAdapter(Modifiers);
}
}
答案 0 :(得分:3)
将适配器放在onClick()方法中,然后将其添加到recyclerview
modifierItemList.setLayoutManager(llm);
ItemModifierAdapter adapter = new ItemModifierAdapter(Modifiers);
modifierItemList.setAdapter(adapter);
okBtn = (Button) view2.findViewById(R.id.okBtn);
答案 1 :(得分:0)
这应该可以使它工作。
package yourneeds.find.com.modifieritemsalertdialog;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Button click;
private List<ItemModifierModel> Modifiers;
RecyclerView modifierItemList;
Context context;
public static ItemModifierAdapter adapter;
List<ItemModifierModel> ItemModifierModels;
LinearLayoutManager llm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click = (Button) findViewById(R.id.button);
modifierItemList = (RecyclerView) view2.findViewById(R.id.ipcET);
modifierItemList.setHasFixedSize(true);
llm = new LinearLayoutManager(MainActivity.this);
modifierItemList.setLayoutManager(llm);
Modifiers = new ArrayList<>();
// Initialize Data
Modifiers.add(new ItemModifierModel("204", "Regular"));
Modifiers.add(new ItemModifierModel("205", "Regular"));
Modifiers.add(new ItemModifierModel("206", "Regular"));
Modifiers.add(new ItemModifierModel("207", "Regular"));
Modifiers.add(new ItemModifierModel("208", "Regular"));
Modifiers.add(new ItemModifierModel("209", "Regular"));
Modifiers.add(new ItemModifierModel("210", "Regular"));
Modifiers.add(new ItemModifierModel("211", "Regular"));
Modifiers.add(new ItemModifierModel("212", "Regular"));
// Initialize Adapter
adapter = new ItemModifierAdapter(Modifiers);
// Set Adapter
modifierItemList.setAdapter(adapter);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Button cancelBtn, okBtn;
LayoutInflater inflater2 = MainActivity.this.getLayoutInflater();
View view2 = inflater2.inflate(R.layout.remove_item_alert_dialog, null);
okBtn = (Button) view2.findViewById(R.id.okBtn);
cancelBtn = (Button) view2.findViewById(R.id.cancelBtn);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(view2);
builder.setCancelable(false);
final AlertDialog alert11 = builder.create();
okBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alert11.dismiss();
}
});
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alert11.dismiss();
}
});
alert11.show();
}
});
}
}