Answer the question
In order to leave comments, you need to log in
How to make a transition to the desired item in the RecyclerView list?
Hello! I am making a simple app with two activities.
The first is the list of products, the second is the internal page of the product, in which I will do something.
All information is stored in a database.
The problem is that when I click on any of the elements, I get to the first product.
Here is my adapter code
class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.ViewHolder>{
private Listener listener;
public static interface Listener {
public void onClick(int position);
}
private List<Product> products = Collections.emptyList();
public ProductsAdapter(){
}
// Метод передает продукты в адаптер
public void setProducts(List<Product> products ) {
this.products = products;
notifyDataSetChanged();
}
//Предоставляет ссылку на представления, используемые в RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView productName, apparatCount, factCount, stockCount;
public ViewHolder(CardView v) {
super(v);
productName = v.findViewById(R.id.productName);
apparatCount = v.findViewById(R.id.apparatCount);
factCount = v.findViewById(R.id.factAppCount);
stockCount = v.findViewById(R.id.stockCount);
}
//Определение класса ViewHolder
}
public void setListener(Listener listener){
this.listener = listener;
}
@Override
public ProductsAdapter.ViewHolder onCreateViewHolder(
ViewGroup parent, int viewType){
CardView cv = (CardView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.product_card, parent, false);
return new ViewHolder(cv);
//Создание нового представления
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position){
final Product product = products.get(position);
holder.productName.setText(product.getProduct_name());
holder.apparatCount.setText(String.valueOf(product.getPositions()));
holder.stockCount.setText(String.valueOf(product.getStock()));
View cardView = holder.itemView;
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onClick(position);
}
}
});
}
@Override
public int getItemCount(){
//Возвращает количество вариантов в наборе данных
return products.size();
}
}
public class MainActivity extends AppCompatActivity {
//Объявим переменные компонентов
private ApparatDatabaseHelper dbHelper = new ApparatDatabaseHelper(this);
//Переменная для работы с БД
private ProductsAdapter productsAdapter = new ProductsAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.products_rec);
recyclerView.setAdapter(productsAdapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
productsAdapter.setListener(new ProductsAdapter.Listener() {
public void onClick(int position) {
Intent intent = new Intent(MainActivity.this, InnerActivity.class);
intent.putExtra(InnerActivity.EXTRA_PRODUCT, position);
startActivity(intent);
}
});
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("products", null, null, null, null, null, null);
List<Product> products = new ArrayList<>();
while(cursor.moveToNext()) {
Product product = new Product();
product.setProduct_name(cursor.getString(1));
product.setPositions(cursor.getInt(2));
product.setFact(cursor.getInt(3));
product.setStock(cursor.getInt(4));
products.add(product);
}
cursor.close();
productsAdapter.setProducts(products);
}
}
public class InnerActivity extends AppCompatActivity {
private ApparatDatabaseHelper dbHelper = new ApparatDatabaseHelper(this);
private ProductsAdapter productsAdapter = new ProductsAdapter();
public static final String EXTRA_PRODUCT = "extraProduct";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inner_product);
Intent intent = getIntent();
intent.getStringExtra(EXTRA_PRODUCT);
SQLiteDatabase db = dbHelper.getReadableDatabase();
final Cursor cursor = db.query ("products", new String[]{},;
final List<Product> products = new ArrayList<>();
if (cursor.moveToFirst()) {
//Получение данных напитка из курсора
int id_product = cursor.getInt(0);
String nameText = cursor.getString(1);
int position = cursor.getInt(2);
int factCount = cursor.getInt(3);
int stockCount = cursor.getInt(4);
TextView name = (TextView) findViewById(R.id.productName);
name.setText(nameText);
TextView pos = (TextView) findViewById(R.id.posCount);
pos.setText(String.valueOf(position));
TextView fact = (TextView) findViewById(R.id.factAppCount);
fact.setText(String.valueOf(factCount));
TextView stock = (TextView) findViewById(R.id.stockCount);
stock.setText(String.valueOf(stockCount));
}
cursor.close();
}
Answer the question
In order to leave comments, you need to log in
ProductsAdapter
public static interface Listener {
public void onClick(int id);
}
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onClick(product.id);
}
}
});
productsAdapter.setListener(new ProductsAdapter.Listener() {
public void onClick(int id) {
Intent intent = new Intent(MainActivity.this, InnerActivity.class);
intent.putExtra(InnerActivity.EXTRA_PRODUCT, id);
startActivity(intent);
}
});
final Cursor cursor = db.query ("products", null, "id = ?", new String[] { id }, null, null, null);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question