I have a database, and I am trying to show the columns on cardview on my app. Nothing wrong database side I guess. I have checked it, for example I can add items to the database. I can login-register. But when I add my items, I should be seeing them on the cardview after addition process. When I add item on the database, it goes back to the recyclerview layout but shows nothing. Only an empty page.
No errors on the debug process until now.
Here is my cardview layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="15dp"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stock Name"
android:textColor="@color/colorPrimary" />
<TextView
android:id="@+id/barcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stock Code" />
<TextView
android:id="@+id/tvQuantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantity" />
<TextView
android:id="@+id/tvPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price" />
<TextView
android:id="@+id/tvCost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cost" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
recyclerlayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:orientation="horizontal"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
RECYCLER.JAVA
public class recycler extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private DatabaseHelper myDb;
private itemAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler);
myDb = new DatabaseHelper(this);
Intent intent =getIntent();
mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.addMenu:
goToAddActivity();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void goToAddActivity(){
Intent intent = new Intent(recycler.this, add.class);
startActivity(intent);
}
}
itemAdapter.java
public class itemAdapter extends RecyclerView.Adapter<itemAdapter.ViewHolder> {
private List<list> mItemsList;
private Context mContext; //to inflate list layout
private RecyclerView mRecyclerV;
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView nameTxt;
public TextView quantityTxt;
public TextView priceTxt;
public TextView costTxt;
public TextView barcodeTxt;
public View layout;
public ViewHolder( View v) {
super(v);
layout = v;
nameTxt = (TextView) v.findViewById(R.id.itemName);
quantityTxt = (TextView) v.findViewById(R.id.tvQuantity);
priceTxt = (TextView) v.findViewById(R.id.tvPrice);
costTxt = (TextView) v.findViewById(R.id.tvCost);
barcodeTxt = (TextView) v.findViewById(R.id.barcode);
}
}
public void add(int position, list list) {
mItemsList.add(position, list);
notifyItemInserted(position);
}
public void remove(int position) {
mItemsList.remove(position);
notifyItemRemoved(position);
}
// Provide a suitable constructor (depends on the kind of dataset)
public itemAdapter(List<list> myDataset, Context context, RecyclerView recyclerView) {
mItemsList = myDataset;
mContext = context;
mRecyclerV = recyclerView;
}
// Create new views (invoked by the layout manager)
@Override
public itemAdapter.ViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
//create a new view
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v =inflater.inflate(R.layout.activity_list, parent, false);
// set the view's size, margins, paddings and layout parameters
return new ViewHolder(v);
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final list list = mItemsList.get(position);
holder.nameTxt.setText("Stock Name: " + list.getName());
holder.barcodeTxt.setText("Barcode: " + list.getBarcode());
holder.quantityTxt.setText("Quantity: " + list.getQuantity());
holder.priceTxt.setText("Price: " + list.getPrice());
holder.costTxt.setText("Cost: " + list.getCost());
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Choose option");
builder.setMessage("Update or delete stock?");
builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//go to update activity
goToUpdateActivity(list.getId());
}
});
builder.setNeutralButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DatabaseHelper dbHelper = new DatabaseHelper(mContext);
dbHelper.deleteRecord(list.getId(), mContext);
mItemsList.remove(position);
mRecyclerV.removeViewAt(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mItemsList.size());
notifyDataSetChanged();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
});
}
private void goToUpdateActivity(long listID){
Intent goToUpdate = new Intent(mContext, update.class);
goToUpdate.putExtra("listID", listID);
mContext.startActivity(goToUpdate);
}
@Override
public int getItemCount() {
return mItemsList.size();
}
}
" with data. Then add that data to your `Adapter` "itemAdapter" (ouch--that hurt just a little). Then use `setAdapter(adapter)` on your `RecyclerView`. There you go... :: I hate to sound harsh, but there is so much wrong with this code... eg. There is no need to pass the `RecyclerView` as a parameter to the `Adapter`! You have totally unnecessary code: `Intent intent =getIntent();`? never used! Mixed naming conventions makes me wonder if this is your code.
– Barns Dec 29 '18 at 20:00