我正在尝试从SQLite数据库中获取数据,并将其显示在RecyclerView上。但是,适配器仅显示第一个项目。许多相关主题都在谈论将父布局的高度更改为xxdp或wrap_content,我尝试过。这是代码:
QuoteAdapter.java:
public class QuoteAdapter extends RecyclerView.Adapter<QuoteAdapter.QuoteViewHolder> {
private QuoteItemBinding binding;
private Context mContext;
private Cursor mCursor;
public QuoteAdapter(Context context,Cursor cursor) {
this.mContext = context;
this.mCursor = cursor;
}
@Override
public QuoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
binding = DataBindingUtil.setContentView(
(Activity)mContext, R.layout.quote_item);
int quoteLayoutId = R.layout.quote_item;
LayoutInflater inflater = LayoutInflater.from(mContext);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(quoteLayoutId,parent,shouldAttachToParentImmediately);
QuoteViewHolder quoteViewHolder = new QuoteViewHolder(view);
return quoteViewHolder;
}
@Override
public void onBindViewHolder(QuoteViewHolder holder, int position) {
if(!mCursor.moveToPosition(position)) {
return;
}
String category = mCursor.getString(mCursor.getColumnIndex(QuoteContract.QuoteEntry.COLUMN_CATEGORY));
String author = mCursor.getString(mCursor.getColumnIndex(QuoteContract.QuoteEntry.COLUMN_AUTHOR));
String quote = mCursor.getString(mCursor.getColumnIndex(QuoteContract.QuoteEntry.COLUMN_CONTENT));
Log.d(this.getClass().getSimpleName(),quote);
holder.mQuoteAuthor.setText(author);
holder.mQuoteCategory.setText(category);
holder.mQuoteTextView.setText(quote);
//Passer l'ID en tag du itemView avec holder.itemView.setTag(id) si on veut une action paritculiere sur un item
}
@Override
public int getItemCount() {
return mCursor.getCount();
}
public void swapCursor(Cursor newCursor) {
if(mCursor != null) mCursor.close();
mCursor = newCursor;
if(newCursor != null) {
this.notifyDataSetChanged();
}
}
public class QuoteViewHolder extends RecyclerView.ViewHolder{
public TextView mQuoteTextView;
public TextView mQuoteAuthor;
public TextView mQuoteCategory;
public QuoteViewHolder(View itemView) {
super(itemView);
mQuoteTextView = binding.quote;
mQuoteAuthor = binding.quoteAuthor;
mQuoteCategory = binding.quoteCategory;
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"Selected",Toast.LENGTH_LONG).show();
}
});
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private QuoteAdapter mQuoteAdapter;
private SQLiteDatabase mDb;
private static final String TAG = MainActivity.class.getSimpleName();
private ActivityMainBinding mainBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
RecyclerView quoteListRecycler = mainBinding.recyclerMain;
quoteListRecycler.setLayoutManager(new LinearLayoutManager(this));
QuoteDbHelper dbHelper = new QuoteDbHelper(this);
mDb = dbHelper.getWritableDatabase();
Cursor cursor = getAllQuotes();
mQuoteAdapter = new QuoteAdapter(this,cursor);
quoteListRecycler.setAdapter(mQuoteAdapter);
}
private Cursor getAllQuotes() {
return mDb.query(QuoteContract.QuoteEntry.TABLE_NAME,
null,
null,
null,
null,
null,
QuoteContract.QuoteEntry.COLUMN_DATE);
}
}
quote_item.xml
<?xml version="1.0" encoding="utf-8"?>
android:paddingBottom="@dimen/quote_item_padding"
android:paddingTop="@dimen/quote_item_padding"
android:id="@+id/item_for_recycler"
android:layout_width="match_parent"
android:layout_height="56dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/quoteCategory"
style="@style/TextAppearance.AppCompat.Display2"
android:paddingLeft="@dimen/quote_item_padding"
android:paddingTop="@dimen/quote_item_padding"
android:textSize="10sp"
tools:text="Category : Business"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/quote"
tools:text="Hello there"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/quoteAuthor"
android:layout_gravity="bottom|right"
style="@style/TextAppearance.AppCompat.Display2"
android:paddingLeft="@dimen/quote_item_padding"
android:paddingTop="@dimen/quote_item_padding"
android:paddingRight="@dimen/quote_item_padding"
android:textSize="10sp"
tools:text="Leonardo DeMontesquieu"
/>
</FrameLayout>
</layout>
提前谢谢!