gpt4 book ai didi

android - 在 RecyclerAdapter 中单击按钮时增加徽章值

转载 作者:行者123 更新时间:2023-11-29 03:19:50 25 4
gpt4 key购买 nike

我正在开发一个应用程序,它允许用户通过单击 AlertDialog 的“确定”按钮来增加通知标记的值。该值将存储在 MySQL 数据库中。

AlertDialog 在 RecyclerAdapter 中设置,通知 Badge 在 Main Activity 中设置,我使用 Utils 类来计算 Badge 数量。

请问实现它的最佳方法是什么?

更新

计数器类

public interface Counter {
public void setCounter(int count);}

适配器类:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {

Context c;
ArrayList<criteria> critere;
private Counter counter;

public RecyclerAdapter(Context c, ArrayList<criteria> critere, Counter counter) {
this.c = c;
this.critere = critere;
this.counter = counter;
}

public class MyViewHolder extends RecyclerView.ViewHolder {
Integer count;
TextView criteria, update;
ImageButton btn1, btn2, btn3, btn4;
Boolean click1, click2, click3, click4;
ImageView image;
RelativeLayout layout;
LinearLayout ranked;
ImageView check;
private ItemClickListener itemClickListener;
public ClipData.Item currentItem;
public View view;

public MyViewHolder(View itemView) {
super(itemView);
view = itemView;

btn1 = itemView.findViewById(R.id.btn1);
btn2 = itemView.findViewById(R.id.btn2);
btn3 = itemView.findViewById(R.id.btn3);
btn4 = itemView.findViewById(R.id.btn4);

layout = itemView.findViewById(R.id.check_view);
ranked = itemView.findViewById(R.id.rank_layout);
check = itemView.findViewById(R.id.check_image);

criteria = itemView.findViewById(R.id.criteria_text);
image = itemView.findViewById(R.id.criteria_pic);

}

}

@Override
public RecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(c).inflate(R.layout.recycler_model,parent,false);
return new MyViewHolder(v);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {

final criteria crit = critere.get(position);

final String name = crit.getName();

//BIND
holder.criteria.setText(name);
holder.layout.setVisibility(View.GONE);

PicassoClient.downloadImage(c,crit.getImageurl(),holder.image);

holder.btn1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {

LayoutInflater li = LayoutInflater.from(c);
View dialogView = li.inflate(R.layout.custom_dialog_rank, null);

AlertDialog.Builder dialog = new AlertDialog.Builder(c);

dialog.setView(dialogView);
dialog.setIcon(R.drawable.btn_press_rank1);
dialog.setCancelable(false);
dialog.setTitle(R.string.crit1);
dialog.setMessage("The " + name + " was very unsatisfiying");
final EditText userInput = (EditText) dialogView
.findViewById(R.id.comment_field);

dialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//checked.setVisibility(View.VISIBLE);
holder.layout.setVisibility(View.VISIBLE);
holder.check.setVisibility(View.VISIBLE);
holder.check.bringToFront();
holder.ranked.setVisibility(View.INVISIBLE);
counter.setCounter(1);

}
})
.setNegativeButton(R.string.annuler, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//checked.setVisibility(View.GONE);
holder.layout.setVisibility(View.GONE);
holder.ranked.setVisibility(View.VISIBLE);
}
});

final AlertDialog alert = dialog.create();
alert.show();

}
});

}

@Override
public int getItemCount() {
return critere.size();
}}

MainActivity 类:

public class RankActivity extends AppCompatActivity {
String jsonURL="xx/criteria.php";
RecyclerView rv;
private int mNotificationsCount = 0;
private Counter counter;
TextView notif;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rank);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

notif = (TextView) findViewById(R.id.badge_notification_1);

new RankActivity.FetchCountTask().execute();

counter = new Counter() {
@Override
public void setCounter(int count) {
Log.e("Ser_Counter", "" + count);
notif.setText(count + "");
Toast.makeText(RankActivity.this, "Called", Toast.LENGTH_SHORT).show();
}
};

rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new GridLayoutManager(RankActivity.this, 2));

new Downloader_recycler(RankActivity.this,jsonURL, rv, counter).execute();
}

public void OnSave(MenuItem item){
Toast.makeText(this, "Saved !", Toast.LENGTH_LONG).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_user, menu);
MenuItem item = menu.findItem(R.id.action_notifications);
MenuItemCompat.setActionView(item, R.layout.notification_update);
//notificationCount = (RelativeLayout) MenuItemCompat.getActionView(item);
//LayerDrawable icon = (LayerDrawable) item.getIcon();
//Utils.setBadgeCount(this, icon, mNotificationsCount);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_notifications) {
// TODO: display unread notifications.
return true;
}
return super.onOptionsItemSelected(item);
}

private void updateNotificationsBadge(int count) {
mNotificationsCount = count;
invalidateOptionsMenu();
}

class FetchCountTask extends AsyncTask<Void, Void, Integer> {

@Override
protected Integer doInBackground(Void... params) {
return 5;
}

@Override
public void onPostExecute(Integer count) {
updateNotificationsBadge(count);
}
}}

最佳答案

按照这个步骤

1.像这样创建一个interface

public interface Counter
{
public void setCounter(int count);
}

2.然后在您的 Activity 中添加此代码

private Counter counter;
counter = new Counter() {
@Override
public void setCounter(int count) {
Log.e("Ser_Counter", "" + count);
tvCountFav.setText(count + "");
Toast.makeText(SearchResultClass.this, "Called", Toast.LENGTH_SHORT).show();
}
};

3.像这样将这个interface作为参数传递给你的适配器类

YourAdapteradapter = new YourAdapter(SearchResultClass.this, ArrayList, counter);

4.在适配器类构造函数中添加counter代码

 private Counter counter;
public SearchResultAdapter(Context context, ArrayList<FilterDataModel> filterDataModelArrayList, Counter counter) {
this.context = context;
this.filterDataModelArrayList = filterDataModelArrayList;
this.counter = counter;
}

5.像这样在侧边点击监听器中触发计数器接口(interface)方法

holder.addLikeImagevView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter.setCounter(1);
}
});

关于android - 在 RecyclerAdapter 中单击按钮时增加徽章值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47092736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com