可能听起来像是重复的问题,但已检查过帐的问题,这些问题与getItemCount()
未设置或布局高度/宽度问题有关。
我正在使用CardView
作为RecyclerView
项目。适配器获取60条记录(检查计数),但CardView
甚至没有显示数据。
使用我仍然是新手的ConstraintLayout
,它完美展现了设计视图。数据正在从外部API填充并使用改进库,该库工作正常。
活动xml。
<android.support.constraint.ConstraintLayout 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:scrollbars="vertical"
tools:context="com.techn.rails.Activity.TrainStatus">
/>
<android.support.v7.widget.CardView
android:id="@+id/search_cardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:cardElevation="5dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/trainno_search_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Train Number"
app:layout_constraintEnd_toStartOf="@+id/trainno_search_et"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_weight="1"
/>
<android.support.v7.widget.AppCompatEditText
android:id="@+id/trainno_search_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@id/trainno_search_tv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBaseline_toBaselineOf="@+id/trainno_search_tv"
android:inputType="number"
android:maxLength="6"
android:hint="@string/train_number"
app:layout_constraintHorizontal_weight="1.5"
/>
<android.support.v7.widget.AppCompatEditText
android:id="@+id/doj_et"
android:layout_width="0dp"
app:layout_constraintEnd_toEndOf="@id/trainno_search_tv"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/trainno_search_tv"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="12dp"
/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/doj_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/doj_et"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/trainno_search_et"
app:layout_constraintBaseline_toBaselineOf="@+id/doj_et"
android:layout_marginTop="12dp"
android:text="@string/select_date"
/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/submit"
android:layout_width="100dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/doj_btn"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:text="@string/submit_btn"
/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:id="@+id/train_list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/result_cardview"
app:layout_constraintBottom_toBottomOf="parent"
android:scrollbars="vertical"
android:padding="10dp"
android:layout_margin="10dp"></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
Cardview xml
<android.support.constraint.ConstraintLayout
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.support.v7.widget.CardView
android:id="@+id/train_status_card_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_gravity="center"
android:layout_margin="5dp"
android:elevation="5dp"
app:cardCornerRadius="5dp"
android:background="?android:attr/selectableItemBackground">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/train_stn_code_lbl"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/train_stn_code"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@id/train_stn_code_lbl"
app:layout_constraintRight_toRightOf="parent" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/train_sch_arr_lbl"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintTop_toBottomOf="@id/train_stn_code_lbl"
app:layout_constraintLeft_toLeftOf="parent"
/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/train_sch_arr"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintTop_toBottomOf="@id/train_stn_code"
app:layout_constraintLeft_toRightOf="@id/train_sch_arr_lbl"
app:layout_constraintRight_toRightOf="parent" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/train_act_dep_lbl"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintTop_toBottomOf="@id/train_sch_arr_lbl"
app:layout_constraintLeft_toLeftOf="parent"
/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/train_act_dep"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintTop_toBottomOf="@id/train_sch_arr_lbl"
app:layout_constraintLeft_toRightOf="@id/train_act_dep_lbl"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
活动
public class TrainStatus extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {
@BindView(R.id.doj_btn)
AppCompatButton mDojBtn;
@BindView(R.id.trainno_search_et)
AppCompatEditText mTrainNumber;
@BindView(R.id.submit)
AppCompatButton mSubmit;
@BindView(R.id.train_list)
RecyclerView recyclerView;
private Calendar calendar;
private DatePickerDialog datePickerDialog;
int Year, Month, Day ;
String dateSelected, dateToDisplay;
private TrainStatusRoute mAdapter;
private List<Route> mRoutes;
private static final String TAG = TrainStatus.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_train_status);
ButterKnife.bind(this);
mRoutes = new ArrayList<>();
mAdapter = new TrainStatusRoute(this,mRoutes);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mAdapter);
calendar = Calendar.getInstance();
Year = calendar.get(Calendar.YEAR) ;
Month = calendar.get(Calendar.MONTH);
Day = calendar.get(Calendar.DAY_OF_MONTH);
mDojBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
datePickerDialog = DatePickerDialog.newInstance(TrainStatus.this, Year, Month, Day);
datePickerDialog.setThemeDark(false);
datePickerDialog.showYearPickerFirst(false);
}
});
mSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestAPIQuery(mTrainNumber.getText().toString(),dateSelected);
}
});
}
@Override
public void onDateSet(DatePickerDialog view, int Year, int Month, int Day) {
dateSelected = String.valueOf(Year)+String.valueOf(Month + 1)+String.valueOf(Day);
}
private void requestAPIQuery (String trainNumber, String doj){
IApi apiService = APIClient.getClient().create(IApi.class);
Call<TrainStatus> call = apiService.getTrainStatus(trainNumber,doj,API_KEY);
call.enqueue(new Callback<TrainStatus>() {
@Override
public void onResponse(Call<TrainStatus> call, Response<TrainStatus> response) {
List<Route> trainStatuses = response.body().getRoute();
mAdapter.notifyDataSetChanged();
Log.d(TAG,"Number of trains"+trainStatuses.size());//getting count 60 here
}
@Override
public void onFailure(Call<com.technovibe.railstat.Model.TrainStatus> call, Throwable t) {
Log.e(TAG, t.toString());
}
});
}
}
适配器代码
public class TrainStatusRoute extends RecyclerView.Adapter<TrainStatusRoute.RouteViewHolder> {
private Context mContext;
private List<Route> mRoutes;
public static class RouteViewHolder extends RecyclerView.ViewHolder{
@BindView(R.id.train_act_arr)
TextView mActArr;
@BindView(R.id.train_act_dep)
TextView mActDep;
@BindView(R.id.train_sch_arr)
TextView mSchArr;
@BindView(R.id.train_sch_dep)
TextView mSchDep;
public RouteViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
public TrainStatusRoute(Context context, List<Route> routeList) {
this.mContext = context;
this.mRoutes = routeList;
}
@Override
public TrainStatusRoute.RouteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.train_status_card, parent, false);
return new RouteViewHolder(itemView);
}
@Override
public void onBindViewHolder(TrainStatusRoute.RouteViewHolder holder, int position) {
Route route = mRoutes.get(position);
holder.mActArr.setText(route.getActarr());
holder.mActDep.setText(route.getActdep());
holder.mSchDep.setText(route.getSchdep());
holder.mSchArr.setText(route.getScharr());
}
@Override
public int getItemCount() {
return mRoutes.size();
}
}
请耐心地张贴冗长的代码。
感谢任何帮助,提前谢谢。
答案 0 :(得分:1)
我猜问题可能出在卡片视图的宽度和高度上:
<android.support.v7.widget.CardView
android:id="@+id/search_cardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
尝试更改宽度和高度值,让宽度match_parent和height为60 dp,看看是否会出现这些项目。
答案 1 :(得分:0)
public void onResponse(Call<TrainStatus> call, Response<TrainStatus> response) {
List<Route> trainStatuses = response.body().getRoute();
//Add this line here
mAdapter.setRoutes(trainStatuses);
mAdapter.notifyDataSetChanged();
Log.d(TAG,"Number of trains"+trainStatuses.size());//getting count 60 here
}
在适配器中为列表定义一个setter
public void setRoutes(List<Route> routes){
mRoutes = routes;
}