RecyclerView 里面的 RecyclerView 弄错了 getItemCount()
RecyclerView inside RecyclerView made wrong getItemCount()
所以它看起来像这样:
SCREENSHOT
但是当我点击添加或减少按钮时,它 return 显示整个列表的大小。
In this case its returns 2(because the otitles.size() returns 2)在我点击的项目下面出现了列表中的另一个项目。
父 RecyclerView 适配器:
public class recyclercart extends RecyclerView.Adapter<recyclercart.myViewHolder> {
Context context;
ArrayList<String> list;
public static View.OnClickListener onclickup;
public static View.OnClickListener onclickdown;
public static ArrayList<String> otitles = new ArrayList<>();
public static ArrayList<String> oimages = new ArrayList<>();
public static ArrayList<String> oingridients = new ArrayList<>();
public static ArrayList<String> odependencies = new ArrayList<>();
public static ArrayList<String> oquantities = new ArrayList<>();
public static ArrayList<String> oprices = new ArrayList<>();
public static ArrayList<String> ostartprices = new ArrayList<>();
public static ArrayList<String> obrand = new ArrayList<>();
public static ArrayList<String> oid = new ArrayList<>();
Cursor gettitle, getimage, getingridients, getdependencies,getquantities, getprices, getstartprice, getbrand, getid;
ArrayList<Integer> postoremove= new ArrayList<>();
ProgressBar progressBar;
SQLiteDatabase database;
public recyclercart(Context c, ProgressBar progressBar){
context=c;
this.progressBar=progressBar;
SQLHelper helper = new SQLHelper(context);
database = helper.getWritableDatabase();
Cursor cursor = database.rawQuery("SELECT * FROM "+SQLHelper.DATABASE_TABLE, null);
list =new ArrayList<>();
cursor.moveToFirst();
do {
String lol = cursor.getString(cursor.getColumnIndex(SQLHelper.BRAND_NAME));
list.add(lol.substring(0,lol.length()-5));
}while (cursor.moveToNext());
if(cursor.moveToLast()) {
postoremove.clear();
Log.d("glavniy", "LIST: " + list.toString());
list = new ArrayList<>(removeDuplicates(list));
}
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.templatecart, parent, false );
return new myViewHolder(view);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull myViewHolder holder, int position) {
otitles.clear();
oimages.clear();
oingridients.clear();
odependencies.clear();
oprices.clear();
oquantities.clear();
ostartprices.clear();
obrand.clear();
oid.clear();
Cursor cur = database.rawQuery("SELECT "+SQLHelper.TITLE_NAME+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "+SQLHelper.BRAND_NAME+" LIKE '%"+
list.get(position)+"%'", null);
SQLHelper helper = new SQLHelper(context);
SQLiteDatabase database = helper.getWritableDatabase();
gettitle = database.rawQuery("SELECT "+SQLHelper.TITLE_NAME+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
getimage = database.rawQuery("SELECT "+SQLHelper.IMAGESRC_NAME+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
getingridients = database.rawQuery("SELECT "+SQLHelper.INGRIDIENTS_NAME+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
getdependencies = database.rawQuery("SELECT "+SQLHelper.DEPENDENCIES_NAME+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
getprices = database.rawQuery("SELECT "+SQLHelper.PRICE_NAME+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
getquantities = database.rawQuery("SELECT "+SQLHelper.QUANTITY_NAME+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
getstartprice = database.rawQuery("SELECT "+SQLHelper.START_PRICE+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
getbrand = database.rawQuery("SELECT "+SQLHelper.BRAND_NAME+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
getid = database.rawQuery("SELECT "+SQLHelper.KEY_ID+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
if (gettitle.moveToFirst() && getimage.moveToNext() && getingridients.moveToFirst() && getdependencies.moveToFirst()
&& getprices.moveToFirst() && getquantities.moveToFirst() && getstartprice.moveToFirst() && getbrand.moveToFirst() && getid.moveToFirst()) {
do {
otitles.add(gettitle.getString(gettitle.getColumnIndex(SQLHelper.TITLE_NAME)));
oimages.add(getimage.getString(getimage.getColumnIndex(SQLHelper.IMAGESRC_NAME)));
oingridients.add(getingridients.getString(getingridients.getColumnIndex(SQLHelper.INGRIDIENTS_NAME)));
odependencies.add(getdependencies.getString(getdependencies.getColumnIndex(SQLHelper.DEPENDENCIES_NAME)));
oprices.add(getprices.getString(getprices.getColumnIndex(SQLHelper.PRICE_NAME)));
oquantities.add(getquantities.getString(getquantities.getColumnIndex(SQLHelper.QUANTITY_NAME)));
ostartprices.add(getstartprice.getString(getstartprice.getColumnIndex(SQLHelper.START_PRICE)));
obrand.add(getbrand.getString(getbrand.getColumnIndex(SQLHelper.BRAND_NAME)));
oid.add(getid.getString(getid.getColumnIndex(SQLHelper.KEY_ID)));
Log.d("glavniy", String.valueOf(ostartprices));
} while (gettitle.moveToNext() && getimage.moveToNext() && getingridients.moveToNext() && getdependencies.moveToNext()
&& getprices.moveToNext() && getquantities.moveToNext() && getstartprice.moveToNext() && getbrand.moveToNext() && getid.moveToNext());
}
if(gettitle.moveToLast()) {
Log.d("glavniy", "MOVETOLAST");
holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
holder.recyclerView.setAdapter(new recyclerOrdersAdapter(context, otitles, oimages, oingridients, oquantities, oprices, ostartprices
, obrand, odependencies, oid));
}
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("resources");
reference.child(list.get(position)).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
holder.title.setText(snapshot.child("nameBrand").getValue().toString());
Glide.with(context).load(snapshot.child("imageBrand").getValue().toString()).into(holder.imageView);
try {
if(recyclerSearchRecycler.timeCheck(snapshot.child("times").child("startTime").getValue().toString(),
snapshot.child("times").child("closeTime").getValue().toString())){
holder.open.setText("OPEN");
}else {holder.open.setText("CLOSED");}
} catch (ParseException e) {
e.printStackTrace();
}
reference.child(list.get(position)).child("addresses").addListenerForSingleValueEvent(new ValueEventListener() {
@SuppressLint("SetTextI18n")
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()){
ArrayList<String> addresses = new ArrayList<>();
addresses.add(dataSnapshot.getValue().toString());
Log.d("glavniy", dataSnapshot.getValue().toString());
if(addresses.size()==snapshot.getChildrenCount()) {
try {
float distance = activityBrandPage.getNearestRest(addresses, context);
Log.d("glavniy", "DIST: " + distance);
if (distance <= 1000) {
progressBar.setVisibility(View.INVISIBLE);
holder.delivery.setText("Delivery: 400 AMD");
} else if (distance > 1000 && distance <= 2000) {
holder.delivery.setText("Delivery: 500 AMD");
progressBar.setVisibility(View.INVISIBLE);
} else if (distance > 2000 && distance <= 3000) {
holder.delivery.setText("Delivery: 600 AMD");
} else if (distance > 3000 && distance <= 4000) {
holder.delivery.setText("Delivery: 700 AMD");
progressBar.setVisibility(View.INVISIBLE);
}
if (search.prog != null) {
search.prog.setVisibility(View.INVISIBLE);
}
} catch (IOException e) {
e.printStackTrace();
progressBar.setVisibility(View.VISIBLE);
Toast.makeText(context, "Failed, please restart", Toast.LENGTH_SHORT).show();
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public static class myViewHolder extends RecyclerView.ViewHolder {
TextView title, delivery, open;
ImageView imageView;
public static RecyclerView recyclerView;
public myViewHolder(@NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.ivBrandCart);
recyclerView=itemView.findViewById(R.id.recyclerMainCart);
title=itemView.findViewById(R.id.tvBrandCart);
delivery=itemView.findViewById(R.id.tvdelivery);
open=itemView.findViewById(R.id.opentv);
}
}
CHILD RecyclerView 适配器:
public class recyclerOrdersAdapter extends RecyclerView.Adapter<recyclerOrdersAdapter.myViewHolder> {
public static Context context;
SQLiteDatabase database;
public static int pos;
public static ArrayList<String> otitles = new ArrayList<>();
public static ArrayList<String> oimages = new ArrayList<>();
public static ArrayList<String> oingridients = new ArrayList<>();
public static ArrayList<String> odependencies = new ArrayList<>();
public static ArrayList<String> oquantities = new ArrayList<>();
public static ArrayList<String> oprices = new ArrayList<>();
public static ArrayList<String> ostartprices = new ArrayList<>();
public static ArrayList<String> obrand = new ArrayList<>();
public static ArrayList<String> oids = new ArrayList<>();
@SuppressLint("Recycle")
public recyclerOrdersAdapter(Context c, ArrayList<String> titles, ArrayList<String> images, ArrayList<String> ingridients,
ArrayList<String> quantities, ArrayList<String> prices, ArrayList<String> startprices, ArrayList<String> brands,
@Nullable ArrayList<String> dependencies, ArrayList<String> ids){
System.out.println("hi");
otitles=new ArrayList<>(titles);
oimages= new ArrayList<>(images);
oingridients=new ArrayList<>(ingridients);
oquantities=new ArrayList<>(quantities);
oprices=new ArrayList<>(prices);
ostartprices=new ArrayList<>(startprices);
obrand=new ArrayList<>(brands);
oids=new ArrayList<>(ids);
assert dependencies != null;
odependencies=new ArrayList<>(dependencies);
context = c;
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.templateorders, parent, false );
return new myViewHolder(view);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull myViewHolder holder, int position) {
System.out.println("hello n");
if(obrand.size()!=0) {
String title = otitles.get(position);
String image = oimages.get(position);
String ingridient = oingridients.get(position);
String dependencie = odependencies.get(position);
String priceik = oprices.get(position);
String quanik = oquantities.get(position);
ingridient = ingridient.replaceAll("[^a-zA-Z0-9, ]", "");
if (ingridient.contains("null, ")) {
ingridient = ingridient.replaceAll("null, ", "");
} else if (ingridient.contains("null,")) {
ingridient = ingridient.replaceAll("null,", "");
} else {
ingridient = ingridient.replaceAll("null", "");
}
dependencie = dependencie.replaceAll("[^a-zA-Z0-9, ]", "");
System.out.println("ingridients " + ingridient);
holder.title.setText(title);
Glide.with(context).load(image).placeholder(R.drawable.elipsis).into(holder.imageView);
holder.ingridients.setText(ingridient);
if(odependencies.get(position)!=null) {
holder.dependencies.setText(dependencie);
}
if(!priceik.contains("$")) {
holder.price.setText(priceik + "$");
}else {
holder.price.setText(priceik);
}
holder.quantity.setText(quanik);
holder.startprice.setText(ostartprices.get(position));
holder.id.setText(oids.get(position));
SQLHelper helper = new SQLHelper(context);
database = helper.getWritableDatabase();
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("glavniy", "TITLE: "+holder.title.getText().toString());
Log.d("glavniy", "position is" + String.valueOf(position));
SQLHelper helper = new SQLHelper(context);
SQLiteDatabase database = helper.getWritableDatabase();
database.execSQL("DELETE FROM " + SQLHelper.DATABASE_TABLE + " WHERE (" + SQLHelper.KEY_ID +
"='" + holder.id.getText().toString() + "')");
notifyItemRemoved(position);
Log.d("glavniy", DatabaseUtils.dumpCursorToString(database.rawQuery("SELECT * FROM " + SQLHelper.DATABASE_TABLE, null)));
}
});
holder.up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("glavniy", "TITLE: "+holder.title.getText().toString());
int o = Integer.parseInt(holder.quantity.getText().toString())+1;
holder.quantity.setText(String.valueOf(o));
int o1 = Integer.parseInt(holder.startprice.getText().toString()) * Integer.parseInt(holder.quantity.getText().toString());
holder.price.setText(o1 + "$");
Log.d("glavniy", "UPDATED");
database.execSQL("UPDATE " + SQLHelper.DATABASE_TABLE + " SET " + SQLHelper.QUANTITY_NAME + "='" + holder.quantity.getText() + "' WHERE ("
+ SQLHelper.KEY_ID + "='" + holder.id.getText() + "')");
database.execSQL("UPDATE " + SQLHelper.DATABASE_TABLE + " SET " + SQLHelper.PRICE_NAME + "='" + holder.price.getText() + "' WHERE ("
+ SQLHelper.KEY_ID + "='" + holder.id.getText() + "')");
Log.d("glavniy", DatabaseUtils.dumpCursorToString(database.rawQuery("SELECT * FROM " + SQLHelper.DATABASE_TABLE, null)));
}
});
holder.down.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Integer.parseInt(holder.quantity.getText().toString()) != 1) {
holder.quantity.setText(String.valueOf(Integer.parseInt(holder.quantity.getText().toString())-1));
int o1 = Integer.parseInt(holder.price.getText().toString().replace("$","")) - Integer.parseInt(holder.startprice.getText().toString());
holder.price.setText(o1 + "$");
database.execSQL("UPDATE " + SQLHelper.DATABASE_TABLE + " SET " + SQLHelper.QUANTITY_NAME + "='" + holder.quantity.getText() + "' WHERE ("
+ SQLHelper.KEY_ID + "='" + holder.id.getText() + "')");
database.execSQL("UPDATE " + SQLHelper.DATABASE_TABLE + " SET " + SQLHelper.PRICE_NAME + "='" + holder.price.getText() + "' WHERE ("
+ SQLHelper.KEY_ID + "='" + holder.id.getText() + "')");
Log.d("glavniy", DatabaseUtils.dumpCursorToString(database.rawQuery("SELECT * FROM " + SQLHelper.DATABASE_TABLE, null)));
}
}
});
}
}
@Override
public int getItemCount() {
return otitles.size();
}
@SuppressLint("StaticFieldLeak")
public class myViewHolder extends RecyclerView.ViewHolder {
public TextView title, price, ingridients, dependencies, up,down, quantity, startprice, id;
public ImageView imageView;
public ImageView delete;
public myViewHolder(@NonNull View itemView) {
super(itemView);
id=itemView.findViewById(R.id.textView7);
startprice=itemView.findViewById(R.id.textView6);
quantity = itemView.findViewById(R.id.ordersQuanity);
up=itemView.findViewById(R.id.upBtn);
down=itemView.findViewById(R.id.downBtn);
delete=itemView.findViewById(R.id.removeButton);
title = itemView.findViewById(R.id.titleOfOrder);
price = itemView.findViewById(R.id.priceOfOrder);
imageView=itemView.findViewById(R.id.imageOfOrde);
ingridients = itemView.findViewById(R.id.ingridientsOfOrder);
dependencies = itemView.findViewById(R.id.dependenciesOfOrder);
}
}
}
我怎样才能 return PARENT RecyclerView 发送的(那个位置的)列表的确切大小?
哎呀。。。真的好难看啊。
我将尝试为您提供一些改进架构的技巧。希望你没问题。
- 为什么要使用这么多列表?为什么你不制作一个自定义 madr classes 列表,其中包含标题、图像、成分 itp 等所有数据。
- 为什么你命名 class recyclercart 的类型而不是 RecyclerCard。在 Kotlin 和 Java 中,程序员没有编写规则 classes 应该以大写字母命名,并且采用陈旧的驼峰式命名。
- 另一件事是您使用数据库将数据融合在一起并将数据上传到视图进程中。这不好。最好有其他 class 之类的 DatabaseManager 或类似的东西,这将使从数据库等中获取数据的工作成为可能。如果你这样做,你的 RecyclerViewAdapter 将在构造函数和class 的唯一职责是在视图中显示数据。代码将被简化,也许错误会更加明显。如果您想动态上传 children 数据(在滚动期间),您可以驾驶出租车,但它的代码也应该放在 DatabaseManager 或 Helper 中。如果数据量不大 - 甚至在创建适配器之前上传它可能会更好,只需将 Map
-
- 我也会看看SQLiteHelper的使用方法。我不确定,但有些东西告诉我你在一个程序中创建了几个数据库。这可能是危险的。请检查一下。请记住创建一次数据库并连接几次。关于这个我不确定。我需要更少的时间来调查你的代码。
关于我的提示:结帐 SOLID;你会有更清晰的架构。
好的,我终于找到了解决方案。这很有趣,但问题是 ArrayLists 是静态的。所以我刚刚删除了 static
字段,现在一切正常。
所以它看起来像这样: SCREENSHOT
但是当我点击添加或减少按钮时,它 return 显示整个列表的大小。 In this case its returns 2(because the otitles.size() returns 2)在我点击的项目下面出现了列表中的另一个项目。
父 RecyclerView 适配器:
public class recyclercart extends RecyclerView.Adapter<recyclercart.myViewHolder> {
Context context;
ArrayList<String> list;
public static View.OnClickListener onclickup;
public static View.OnClickListener onclickdown;
public static ArrayList<String> otitles = new ArrayList<>();
public static ArrayList<String> oimages = new ArrayList<>();
public static ArrayList<String> oingridients = new ArrayList<>();
public static ArrayList<String> odependencies = new ArrayList<>();
public static ArrayList<String> oquantities = new ArrayList<>();
public static ArrayList<String> oprices = new ArrayList<>();
public static ArrayList<String> ostartprices = new ArrayList<>();
public static ArrayList<String> obrand = new ArrayList<>();
public static ArrayList<String> oid = new ArrayList<>();
Cursor gettitle, getimage, getingridients, getdependencies,getquantities, getprices, getstartprice, getbrand, getid;
ArrayList<Integer> postoremove= new ArrayList<>();
ProgressBar progressBar;
SQLiteDatabase database;
public recyclercart(Context c, ProgressBar progressBar){
context=c;
this.progressBar=progressBar;
SQLHelper helper = new SQLHelper(context);
database = helper.getWritableDatabase();
Cursor cursor = database.rawQuery("SELECT * FROM "+SQLHelper.DATABASE_TABLE, null);
list =new ArrayList<>();
cursor.moveToFirst();
do {
String lol = cursor.getString(cursor.getColumnIndex(SQLHelper.BRAND_NAME));
list.add(lol.substring(0,lol.length()-5));
}while (cursor.moveToNext());
if(cursor.moveToLast()) {
postoremove.clear();
Log.d("glavniy", "LIST: " + list.toString());
list = new ArrayList<>(removeDuplicates(list));
}
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.templatecart, parent, false );
return new myViewHolder(view);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull myViewHolder holder, int position) {
otitles.clear();
oimages.clear();
oingridients.clear();
odependencies.clear();
oprices.clear();
oquantities.clear();
ostartprices.clear();
obrand.clear();
oid.clear();
Cursor cur = database.rawQuery("SELECT "+SQLHelper.TITLE_NAME+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "+SQLHelper.BRAND_NAME+" LIKE '%"+
list.get(position)+"%'", null);
SQLHelper helper = new SQLHelper(context);
SQLiteDatabase database = helper.getWritableDatabase();
gettitle = database.rawQuery("SELECT "+SQLHelper.TITLE_NAME+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
getimage = database.rawQuery("SELECT "+SQLHelper.IMAGESRC_NAME+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
getingridients = database.rawQuery("SELECT "+SQLHelper.INGRIDIENTS_NAME+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
getdependencies = database.rawQuery("SELECT "+SQLHelper.DEPENDENCIES_NAME+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
getprices = database.rawQuery("SELECT "+SQLHelper.PRICE_NAME+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
getquantities = database.rawQuery("SELECT "+SQLHelper.QUANTITY_NAME+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
getstartprice = database.rawQuery("SELECT "+SQLHelper.START_PRICE+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
getbrand = database.rawQuery("SELECT "+SQLHelper.BRAND_NAME+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
getid = database.rawQuery("SELECT "+SQLHelper.KEY_ID+" FROM "+SQLHelper.DATABASE_TABLE+" WHERE "
+SQLHelper.BRAND_NAME+" LIKE '%"+list.get(position)+"%'", null);
if (gettitle.moveToFirst() && getimage.moveToNext() && getingridients.moveToFirst() && getdependencies.moveToFirst()
&& getprices.moveToFirst() && getquantities.moveToFirst() && getstartprice.moveToFirst() && getbrand.moveToFirst() && getid.moveToFirst()) {
do {
otitles.add(gettitle.getString(gettitle.getColumnIndex(SQLHelper.TITLE_NAME)));
oimages.add(getimage.getString(getimage.getColumnIndex(SQLHelper.IMAGESRC_NAME)));
oingridients.add(getingridients.getString(getingridients.getColumnIndex(SQLHelper.INGRIDIENTS_NAME)));
odependencies.add(getdependencies.getString(getdependencies.getColumnIndex(SQLHelper.DEPENDENCIES_NAME)));
oprices.add(getprices.getString(getprices.getColumnIndex(SQLHelper.PRICE_NAME)));
oquantities.add(getquantities.getString(getquantities.getColumnIndex(SQLHelper.QUANTITY_NAME)));
ostartprices.add(getstartprice.getString(getstartprice.getColumnIndex(SQLHelper.START_PRICE)));
obrand.add(getbrand.getString(getbrand.getColumnIndex(SQLHelper.BRAND_NAME)));
oid.add(getid.getString(getid.getColumnIndex(SQLHelper.KEY_ID)));
Log.d("glavniy", String.valueOf(ostartprices));
} while (gettitle.moveToNext() && getimage.moveToNext() && getingridients.moveToNext() && getdependencies.moveToNext()
&& getprices.moveToNext() && getquantities.moveToNext() && getstartprice.moveToNext() && getbrand.moveToNext() && getid.moveToNext());
}
if(gettitle.moveToLast()) {
Log.d("glavniy", "MOVETOLAST");
holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
holder.recyclerView.setAdapter(new recyclerOrdersAdapter(context, otitles, oimages, oingridients, oquantities, oprices, ostartprices
, obrand, odependencies, oid));
}
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("resources");
reference.child(list.get(position)).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
holder.title.setText(snapshot.child("nameBrand").getValue().toString());
Glide.with(context).load(snapshot.child("imageBrand").getValue().toString()).into(holder.imageView);
try {
if(recyclerSearchRecycler.timeCheck(snapshot.child("times").child("startTime").getValue().toString(),
snapshot.child("times").child("closeTime").getValue().toString())){
holder.open.setText("OPEN");
}else {holder.open.setText("CLOSED");}
} catch (ParseException e) {
e.printStackTrace();
}
reference.child(list.get(position)).child("addresses").addListenerForSingleValueEvent(new ValueEventListener() {
@SuppressLint("SetTextI18n")
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()){
ArrayList<String> addresses = new ArrayList<>();
addresses.add(dataSnapshot.getValue().toString());
Log.d("glavniy", dataSnapshot.getValue().toString());
if(addresses.size()==snapshot.getChildrenCount()) {
try {
float distance = activityBrandPage.getNearestRest(addresses, context);
Log.d("glavniy", "DIST: " + distance);
if (distance <= 1000) {
progressBar.setVisibility(View.INVISIBLE);
holder.delivery.setText("Delivery: 400 AMD");
} else if (distance > 1000 && distance <= 2000) {
holder.delivery.setText("Delivery: 500 AMD");
progressBar.setVisibility(View.INVISIBLE);
} else if (distance > 2000 && distance <= 3000) {
holder.delivery.setText("Delivery: 600 AMD");
} else if (distance > 3000 && distance <= 4000) {
holder.delivery.setText("Delivery: 700 AMD");
progressBar.setVisibility(View.INVISIBLE);
}
if (search.prog != null) {
search.prog.setVisibility(View.INVISIBLE);
}
} catch (IOException e) {
e.printStackTrace();
progressBar.setVisibility(View.VISIBLE);
Toast.makeText(context, "Failed, please restart", Toast.LENGTH_SHORT).show();
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public static class myViewHolder extends RecyclerView.ViewHolder {
TextView title, delivery, open;
ImageView imageView;
public static RecyclerView recyclerView;
public myViewHolder(@NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.ivBrandCart);
recyclerView=itemView.findViewById(R.id.recyclerMainCart);
title=itemView.findViewById(R.id.tvBrandCart);
delivery=itemView.findViewById(R.id.tvdelivery);
open=itemView.findViewById(R.id.opentv);
}
}
CHILD RecyclerView 适配器:
public class recyclerOrdersAdapter extends RecyclerView.Adapter<recyclerOrdersAdapter.myViewHolder> {
public static Context context;
SQLiteDatabase database;
public static int pos;
public static ArrayList<String> otitles = new ArrayList<>();
public static ArrayList<String> oimages = new ArrayList<>();
public static ArrayList<String> oingridients = new ArrayList<>();
public static ArrayList<String> odependencies = new ArrayList<>();
public static ArrayList<String> oquantities = new ArrayList<>();
public static ArrayList<String> oprices = new ArrayList<>();
public static ArrayList<String> ostartprices = new ArrayList<>();
public static ArrayList<String> obrand = new ArrayList<>();
public static ArrayList<String> oids = new ArrayList<>();
@SuppressLint("Recycle")
public recyclerOrdersAdapter(Context c, ArrayList<String> titles, ArrayList<String> images, ArrayList<String> ingridients,
ArrayList<String> quantities, ArrayList<String> prices, ArrayList<String> startprices, ArrayList<String> brands,
@Nullable ArrayList<String> dependencies, ArrayList<String> ids){
System.out.println("hi");
otitles=new ArrayList<>(titles);
oimages= new ArrayList<>(images);
oingridients=new ArrayList<>(ingridients);
oquantities=new ArrayList<>(quantities);
oprices=new ArrayList<>(prices);
ostartprices=new ArrayList<>(startprices);
obrand=new ArrayList<>(brands);
oids=new ArrayList<>(ids);
assert dependencies != null;
odependencies=new ArrayList<>(dependencies);
context = c;
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.templateorders, parent, false );
return new myViewHolder(view);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull myViewHolder holder, int position) {
System.out.println("hello n");
if(obrand.size()!=0) {
String title = otitles.get(position);
String image = oimages.get(position);
String ingridient = oingridients.get(position);
String dependencie = odependencies.get(position);
String priceik = oprices.get(position);
String quanik = oquantities.get(position);
ingridient = ingridient.replaceAll("[^a-zA-Z0-9, ]", "");
if (ingridient.contains("null, ")) {
ingridient = ingridient.replaceAll("null, ", "");
} else if (ingridient.contains("null,")) {
ingridient = ingridient.replaceAll("null,", "");
} else {
ingridient = ingridient.replaceAll("null", "");
}
dependencie = dependencie.replaceAll("[^a-zA-Z0-9, ]", "");
System.out.println("ingridients " + ingridient);
holder.title.setText(title);
Glide.with(context).load(image).placeholder(R.drawable.elipsis).into(holder.imageView);
holder.ingridients.setText(ingridient);
if(odependencies.get(position)!=null) {
holder.dependencies.setText(dependencie);
}
if(!priceik.contains("$")) {
holder.price.setText(priceik + "$");
}else {
holder.price.setText(priceik);
}
holder.quantity.setText(quanik);
holder.startprice.setText(ostartprices.get(position));
holder.id.setText(oids.get(position));
SQLHelper helper = new SQLHelper(context);
database = helper.getWritableDatabase();
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("glavniy", "TITLE: "+holder.title.getText().toString());
Log.d("glavniy", "position is" + String.valueOf(position));
SQLHelper helper = new SQLHelper(context);
SQLiteDatabase database = helper.getWritableDatabase();
database.execSQL("DELETE FROM " + SQLHelper.DATABASE_TABLE + " WHERE (" + SQLHelper.KEY_ID +
"='" + holder.id.getText().toString() + "')");
notifyItemRemoved(position);
Log.d("glavniy", DatabaseUtils.dumpCursorToString(database.rawQuery("SELECT * FROM " + SQLHelper.DATABASE_TABLE, null)));
}
});
holder.up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("glavniy", "TITLE: "+holder.title.getText().toString());
int o = Integer.parseInt(holder.quantity.getText().toString())+1;
holder.quantity.setText(String.valueOf(o));
int o1 = Integer.parseInt(holder.startprice.getText().toString()) * Integer.parseInt(holder.quantity.getText().toString());
holder.price.setText(o1 + "$");
Log.d("glavniy", "UPDATED");
database.execSQL("UPDATE " + SQLHelper.DATABASE_TABLE + " SET " + SQLHelper.QUANTITY_NAME + "='" + holder.quantity.getText() + "' WHERE ("
+ SQLHelper.KEY_ID + "='" + holder.id.getText() + "')");
database.execSQL("UPDATE " + SQLHelper.DATABASE_TABLE + " SET " + SQLHelper.PRICE_NAME + "='" + holder.price.getText() + "' WHERE ("
+ SQLHelper.KEY_ID + "='" + holder.id.getText() + "')");
Log.d("glavniy", DatabaseUtils.dumpCursorToString(database.rawQuery("SELECT * FROM " + SQLHelper.DATABASE_TABLE, null)));
}
});
holder.down.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Integer.parseInt(holder.quantity.getText().toString()) != 1) {
holder.quantity.setText(String.valueOf(Integer.parseInt(holder.quantity.getText().toString())-1));
int o1 = Integer.parseInt(holder.price.getText().toString().replace("$","")) - Integer.parseInt(holder.startprice.getText().toString());
holder.price.setText(o1 + "$");
database.execSQL("UPDATE " + SQLHelper.DATABASE_TABLE + " SET " + SQLHelper.QUANTITY_NAME + "='" + holder.quantity.getText() + "' WHERE ("
+ SQLHelper.KEY_ID + "='" + holder.id.getText() + "')");
database.execSQL("UPDATE " + SQLHelper.DATABASE_TABLE + " SET " + SQLHelper.PRICE_NAME + "='" + holder.price.getText() + "' WHERE ("
+ SQLHelper.KEY_ID + "='" + holder.id.getText() + "')");
Log.d("glavniy", DatabaseUtils.dumpCursorToString(database.rawQuery("SELECT * FROM " + SQLHelper.DATABASE_TABLE, null)));
}
}
});
}
}
@Override
public int getItemCount() {
return otitles.size();
}
@SuppressLint("StaticFieldLeak")
public class myViewHolder extends RecyclerView.ViewHolder {
public TextView title, price, ingridients, dependencies, up,down, quantity, startprice, id;
public ImageView imageView;
public ImageView delete;
public myViewHolder(@NonNull View itemView) {
super(itemView);
id=itemView.findViewById(R.id.textView7);
startprice=itemView.findViewById(R.id.textView6);
quantity = itemView.findViewById(R.id.ordersQuanity);
up=itemView.findViewById(R.id.upBtn);
down=itemView.findViewById(R.id.downBtn);
delete=itemView.findViewById(R.id.removeButton);
title = itemView.findViewById(R.id.titleOfOrder);
price = itemView.findViewById(R.id.priceOfOrder);
imageView=itemView.findViewById(R.id.imageOfOrde);
ingridients = itemView.findViewById(R.id.ingridientsOfOrder);
dependencies = itemView.findViewById(R.id.dependenciesOfOrder);
}
}
}
我怎样才能 return PARENT RecyclerView 发送的(那个位置的)列表的确切大小?
哎呀。。。真的好难看啊。 我将尝试为您提供一些改进架构的技巧。希望你没问题。
- 为什么要使用这么多列表?为什么你不制作一个自定义 madr classes 列表,其中包含标题、图像、成分 itp 等所有数据。
- 为什么你命名 class recyclercart 的类型而不是 RecyclerCard。在 Kotlin 和 Java 中,程序员没有编写规则 classes 应该以大写字母命名,并且采用陈旧的驼峰式命名。
- 另一件事是您使用数据库将数据融合在一起并将数据上传到视图进程中。这不好。最好有其他 class 之类的 DatabaseManager 或类似的东西,这将使从数据库等中获取数据的工作成为可能。如果你这样做,你的 RecyclerViewAdapter 将在构造函数和class 的唯一职责是在视图中显示数据。代码将被简化,也许错误会更加明显。如果您想动态上传 children 数据(在滚动期间),您可以驾驶出租车,但它的代码也应该放在 DatabaseManager 或 Helper 中。如果数据量不大 - 甚至在创建适配器之前上传它可能会更好,只需将 Map
- 我也会看看SQLiteHelper的使用方法。我不确定,但有些东西告诉我你在一个程序中创建了几个数据库。这可能是危险的。请检查一下。请记住创建一次数据库并连接几次。关于这个我不确定。我需要更少的时间来调查你的代码。
关于我的提示:结帐 SOLID;你会有更清晰的架构。
好的,我终于找到了解决方案。这很有趣,但问题是 ArrayLists 是静态的。所以我刚刚删除了 static
字段,现在一切正常。