我創建了一個應用程式,它在 Fragment 中有一個 RecyclerView 來顯示資料庫中的資料。幾周前,我用 RecyclerView 和 android 支持包創建了另一個應用程式,一切都很好。現在,使用 androidx,視圖不顯示任何內容。這是我的 RecyclerViewAdapter 類:
public class HistoryRecyclerViewAdapter extends RecyclerView.Adapter<HistoryRecyclerViewAdapter.ViewHolder> {
private static final String TAG = "HistoryRecyclerView";
private List<HistoryType> mHistory;
HistoryRecyclerViewAdapter(List<HistoryType> history){
mHistory = history;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View historyView = inflater.inflate(R.layout.history_list_item,parent,false);
ViewHolder viewHolder = new ViewHolder(historyView);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
HistoryType history = mHistory.get(position);
SimpleDateFormat formatter;
Calendar calendar;
TextView nameText = holder.mNameText;
nameText.setText(history.getName());
TextView durationText = holder.mDurationText;
formatter = new SimpleDateFormat("HH:mm:ss");
calendar = Calendar.getInstance();
calendar.setTimeInMillis(history.getDuration());
String durationString = formatter.format(calendar.getTime());
durationText.setText(durationString);
TextView distanceText = holder.mDistanceText;
distanceText.setText(String.format("%.2f km",history.getDistance()));
TextView totalAirTimeText = holder.mTotalAirTimeText;
formatter = new SimpleDateFormat("mm:ss.SS");
calendar = Calendar.getInstance();
calendar.setTimeInMillis(history.getTotalAirTime());
String totalAirTimeString = formatter.format(calendar.getTime());
totalAirTimeText.setText(totalAirTimeString);
TextView dateText= holder.mDateText;
formatter = new SimpleDateFormat("dd MMMM yyyy");
String dateString = formatter.format(history.getDate());
dateText.setText(dateString);
}
@Override
public int getItemCount() {
return mHistory.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView mNameText;
public TextView mDurationText;
public TextView mDistanceText;
public TextView mTotalAirTimeText;
public TextView mDateText;
public ViewHolder(View itemView) {
super(itemView);
mNameText = (TextView) itemView.findViewById(R.id.history_name);
mDurationText = (TextView) itemView.findViewById(R.id.history_duration);
mDistanceText = (TextView) itemView.findViewById(R.id.history_distance);
mTotalAirTimeText = (TextView) itemView.findViewById(R.id.history_airtime);
mDateText = (TextView) itemView.findViewById(R.id.history_date);
}
}
這里是呼叫配接器的片段(ArrayList 有一項):
public class HistoryFragment extends Fragment {
private ArrayList<HistoryType> mHistoryList = new ArrayList<>();
private RecyclerView mRecyclerView;
private HistoryRecyclerViewAdapter mHistoryAdapter;
private SQLiteOpenHelper mSqliteOpenHelper;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.history_recycler);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mHistoryAdapter = new HistoryRecyclerViewAdapter(mHistoryList);
mRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
mRecyclerView.setAdapter(mHistoryAdapter);
mSqliteOpenHelper = new SQLiteOpenHelper(getActivity());
mHistoryList = mSqliteOpenHelper.getHistory();
for (HistoryType dataSet: mHistoryList) {
mHistoryAdapter.notifyItemInserted(mHistoryList.indexOf(dataSet));
}
}
fragment_history.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@ id/history_recycler"
android:layout_marginLeft="@dimen/text_margin"
android:layout_marginRight="@dimen/text_margin"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
history_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
//Name
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@ id/history_name"
android:layout_margin="@dimen/text_margin"
android:text="activity name"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textSize="@dimen/big_text_size"
android:textStyle="bold"
/>
//Duration
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@ id/history_duration_icon"
android:layout_marginTop="@dimen/text_margin"
android:layout_marginRight="@dimen/icon_margin"
android:layout_marginLeft="@dimen/text_margin"
app:layout_constraintTop_toBottomOf="@id/history_name"
app:layout_constraintLeft_toLeftOf="parent"
android:background="@drawable/timer_24" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@ id/history_duration"
android:text="01.01.2021"
android:layout_margin="@dimen/text_margin"
app:layout_constraintTop_toBottomOf="@id/history_name"
app:layout_constraintLeft_toRightOf="@id/history_duration_icon"
android:textSize="@dimen/medium_text_size"/>
//Distance
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@ id/history_distance_icon"
android:layout_marginTop="@dimen/text_margin"
android:layout_marginLeft="@dimen/text_margin"
android:layout_marginRight="@dimen/icon_margin"
app:layout_constraintTop_toBottomOf="@id/history_name"
app:layout_constraintLeft_toRightOf="@id/history_duration"
android:background="@drawable/arrow_right_alt_24"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@ id/history_distance"
android:text="1h 35min"
android:layout_marginTop="@dimen/text_margin"
android:layout_marginLeft="@dimen/icon_margin"
app:layout_constraintTop_toBottomOf="@id/history_name"
app:layout_constraintLeft_toRightOf="@id/history_distance_icon"
android:textSize="@dimen/medium_text_size"/>
//Total Airtime
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@ id/history_airtime_icon"
android:layout_marginTop="@dimen/text_margin"
android:layout_marginLeft="@dimen/text_margin"
android:layout_marginRight="@dimen/icon_margin"
app:layout_constraintTop_toBottomOf="@id/history_name"
app:layout_constraintLeft_toRightOf="@id/history_distance"
android:background="@drawable/keyboard_arrow_up_24"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@ id/history_airtime"
android:text="1h 35min"
android:layout_marginTop="@dimen/text_margin"
android:layout_marginLeft="@dimen/icon_margin"
app:layout_constraintTop_toBottomOf="@id/history_name"
app:layout_constraintLeft_toRightOf="@id/history_airtime_icon"
android:textSize="@dimen/medium_text_size"/>
//Date
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@ id/history_date"
android:text="01.01.2021"
android:layout_margin="@dimen/text_margin"
app:layout_constraintTop_toBottomOf="@id/history_duration_icon"
app:layout_constraintLeft_toLeftOf="parent"
android:textSize="@dimen/small_text_size" />
</androidx.constraintlayout.widget.ConstraintLayout>
有人幫我嗎?非常感謝
uj5u.com熱心網友回復:
據我所知,您的參考資料有問題。首先,您使用 ArrayList 的空實體宣告您的配接器,這沒問題。配接器現在已與此 ArrayList 連接。但是隨后您將串列重新分配給另一個實體,即從
mSqliteOpenHelper.getHistory()
如果比較這兩個實體,您會發現它們并不相同。您應該做的是使用“addAll()”將“mSqliteOpenHelper.getHistory()”的結果添加到已與配接器連接的串列(您已經定義的空ArrayList)。
編輯:只是為了讓自己更清楚:一開始你宣告并初始化你的串列
private ArrayList<HistoryType> mHistoryList = new ArrayList<>();
我們稱這個實體為Instance#1。您將 Instance#1 設定為配接器的資料源。配接器現在有一個空的資料源。
之后,您重新分配此串列
mHistoryList = mSqliteOpenHelper.getHistory();
這是一個不同的 ArrayList 實體,我們稱之為 Instance#2。盡管 mHistoryList 變數保存了這個實體,但配接器仍然與 Instance#1 鏈接,只是您不再參考它。因此,配接器與空的 ArrayList(Instance#1)鏈接,而不是與有資料的 ArrayList(Instance#2)鏈接。不要重新分配變數,使其成為最終的。ArrayList 是可變的,因此您可以向其中添加資料。因此,將資料添加到其中并通知配接器它已更改。
如果您有任何問題,請告訴我
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/403265.html
標籤:
上一篇:如何使用fn_map將陣列C中的每一行映射到陣列B中的對應行
下一篇:img不顯示在Android上
