【发布时间】:2021-08-25 05:45:55
【问题描述】:
我正在尝试复制 Recyclerview 方法的操作,其中我在 ArrayList itemname 中有 3 个项目,并且还存储在 sqlite 表中。对应于每个项目名称,在表中分配了一个“id”。在这里,我希望对于下面的每个迭代,所有 3 个项目都应根据 Adapter 类的 onBindViewHolder() 方法中定义的条件改变颜色。
for (int i=0; i<itemname.size(); i++) {
String query = "SELECT id FROM sec_table WHERE address='"+itemname.get(i)+"'";
Cursor c1 = SecDB.rawQuery(query,null);
if (c1 != null && c1.getCount() != 0)
{
while (c1.moveToNext()) {
id = c1.getInt(c1.getColumnIndex("id"));
}
c1.close();
}
alert_position = id; // Class variable 'alert_position'
userAdapter.notifyItemChanged(alert_position);
}
onBindViewHolder()方法负责根据Main类中位置变量的值改变卡片的颜色。
@Override // Inside the Adapter class
public void onBindViewHolder(@NonNull final UserViewHolder holder, final int position) {
final SecurityDetails userDetails = secDetailsList.get(position);
LogsDatabaseHelper logsDBHelper = new LogsDatabaseHelper(context);
SecurityDatabaseHelper secDBHelper = new SecurityDatabaseHelper(context);
LogsDB = logsDBHelper.getWritableDatabase();
SecDB = secDBHelper.getWritableDatabase();
if (requestQueue==null)
requestQueue = Volley.newRequestQueue(context); // Creating RequestQueue...
holder.tvName.setText(userDetails.getName());
if(position == Security.alert_position) {
Toast.makeText(context, position+"", Toast.LENGTH_SHORT).show();
holder.state.setText("< Activity Detected >");
holder.state.setTextColor(Color.RED);
holder.cards.setBackgroundColor(0x33FF0000);
}
}
虽然上面的 for 循环同时将位置值 1、2、3 提供给 notifyItemChanged(),但这里只是改变了最后一张卡片的颜色。
但是当通过 android Volley 执行更新卡片的相同逻辑时,所有 3 张卡片都一张一张变红。截击方法如下图:
for (int i=0; i<itemname.size(); i++) { // Calls the Volley method with different items name from arraylist
CardsUpdate(itemname.get(i));
}
private void CardsUpdate(final String email) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://plateau.com/SecuritySensorUpdate.php", new Response.Listener<String>()
{
int id;
@Override
public void onResponse(String ServerResponse) {
if (ServerResponse.equalsIgnoreCase("1")) {
ContentValues values = new ContentValues();
values.put(SecurityDatabaseContract.UserDatabase2.SEC_NAME_COL4, "1");
SecDB.update(SecurityDatabaseContract.UserDatabase2.TABLE_NAME2, values, "address='" + email + "'", null);
String query = "SELECT id FROM sec_table WHERE address='" + email + "'";
Cursor c1 = SecDB.rawQuery(query, null);
if (c1 != null && c1.getCount() != 0) {
while (c1.moveToNext()) {
id = c1.getInt(c1.getColumnIndex("id"));
}
c1.close();
}
alert_position = id;
userAdapter.notifyItemChanged(alert_position);
}
else
Toast.makeText(Security.this, ServerResponse, Toast.LENGTH_SHORT).show();
}},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
// Toast.makeText(Security.this, volleyError.toString(), Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<>();
params.put("email", email);
return params;
}};
requestQueue.add(stringRequest); // Adding the StringRequest object into requestQueue.
}
请提出一些解决方法,以实现更新所有 3 张卡片颜色的预期输出。
【问题讨论】:
-
欢迎来到 SO。你没有在这里使用列表视图,对吧?我已经删除了它的标签,好像你正在使用回收站
标签: android android-recyclerview android-volley notifydatasetchanged