我是android的新手,在这里我尝试使用RecyclerView和Cardview绑定ListView。在我的情况下,我没有得到任何错误,但我无法获取ListView项目。
这是我的适配器类
RecyclerviewAdapter_list.java
public class RecyclerviewAdapter_list extends RecyclerView.Adapter<RecyclerviewAdapter_list.MyViewHolder> {
private Context mContext;
private List<myAlarms> myAlarmsdata;
public RecyclerviewAdapter_list(Context mContext, List<myAlarms>myAlarms){
this.mContext = mContext;
this.myAlarmsdata =myAlarms;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.cardview_alarm, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
myAlarms myAlarms =myAlarmsdata.get(position);
holder.mTime.setText(myAlarmsdata.get(position).getALARM_TIME());
holder.mTitle.setText(myAlarmsdata.get(position).getALARM_TITLE());
}
@Override
public int getItemCount() {
return myAlarmsdata.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView mTime,mTitle,mAmPm;
SwitchButton mbtn;
public MyViewHolder(View itemView) {
super(itemView);
mTime = (TextView) itemView.findViewById(R.id.mTime);
// mAmPm = (TextView) itemView.findViewById(R.id.mAmPm);
mTitle = (TextView) itemView.findViewById(R.id.mTitle);
// mbtn = (SwitchButton) itemView.findViewById(R.id.mTime);
}
}
}
AlarmActivity.java
public class AlarmActivity extends Activity {
private ImageView mAddIcon;
List<myAlarms>myAlarms;
RecyclerView recyclerView;
Database_Helper database_helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
Utils.darkenStatusBar(this, R.color.StatusbarColor);
database_helper = new Database_Helper(this);
mAddIcon = (ImageView) findViewById(R.id.mAddIcon);
myAlarms = new ArrayList<>();
recyclerView=(RecyclerView)findViewById(R.id.mRecyclerView_id);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
RecyclerviewAdapter_list recyclerviewAdapter_list =new RecyclerviewAdapter_list(this,myAlarms);
recyclerView.setAdapter(recyclerviewAdapter_list);
mAddIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<myAlarms> myAlarms = database_helper.getMyAlarms();
Intent intent = new Intent(AlarmActivity.this, CreateAlarm.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_up, R.anim.slide_out);
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
}
任何人都可以告诉我需要做哪些更改以及我在哪里犯错误。 感谢。
答案 0 :(得分:0)
之前我添加了同样的问题,解决方法是删除您的CardView并将此行添加到布局中:
android:background="@android:drawable/dialog_holo_light_frame"
此drawable与CardView具有完全相同的设计。此外,您不需要任何实现来使用它,因此您可以从Gradle中删除您的CardView实现。
答案 1 :(得分:0)
您将此代码放入项目中
//当你需要横向时 layoutManager.setOrientation(上下文,LinearLayoutManager.HORIZONTAL,FALSE);
//当你想要垂直时 layoutManager.setOrientation(上下文,LinearLayoutManager.VERTICAL,FALSE);