gpt4 book ai didi

java - 使用自定义 ListView 布局中的按钮增加 Int 的值

转载 作者:行者123 更新时间:2023-12-02 11:41:39 25 4
gpt4 key购买 nike

我最近成功制作了自定义 ListView 布局并从 ListView 填充该数据。
ListView 包含一个“喜欢”按钮和一个存储喜欢数量的 TextView 。但我似乎无法弄清楚如何获取该 int 并在按下按钮时增加它,因为这将在 CustomAdapter 中执行。

数据模型:

public class MessagesListDataModel {
private String uid;
private String msg;
private String likes;
private String date;
private Button like;
private Button reply;


public MessagesListDataModel(String uid, String msg, String date) {
this.uid = uid;
this.msg = msg;
this.date = date;
this.likes = likes;
}
public MessagesListDataModel(){

}

public String getUid() {
return uid;
}

public void setUid(String uid) {
this.uid = uid;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}
public String getLikes() {
return likes;
}

public void setLikes(String likes) {
this.likes = likes;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public Button getLike() {
return like;
}

public void setLike(Button like) {
this.like = like;
}
}

适配器:

public class MessagesListAdapter extends ArrayAdapter<MessagesListDataModel> {

private ArrayList<MessagesListDataModel> dataModels;

public MessagesListAdapter(Context context, ArrayList<MessagesListDataModel> dataModels){
super(context,0, dataModels);
this.dataModels = dataModels;

}

/*
* we are overriding the getView method here - this is what defines how each
* list item will look.
*/
public View getView(int position, View convertView, ViewGroup parent){

MessagesListDataModel messagesListDataModel = dataModels.get(position);

// first check to see if the view is null. if so, we have to inflate it.
// to inflate it basically means to render, or show, the view.
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.chat_messages_layout, parent, false);
}


TextView uid = (TextView) convertView.findViewById(R.id.textViewUserID);
TextView message = (TextView) convertView.findViewById(R.id.textViewMessage);
TextView likes = (TextView) convertView.findViewById(R.id.textViewLikes);
TextView date = (TextView) convertView.findViewById(R.id.textViewDateTime);
Button like = (Button) convertView.findViewById(R.id.buttonLike);
Button reply = (Button) convertView.findViewById(R.id.buttonReply);

uid.setText(messagesListDataModel.getUid());
message.setText(messagesListDataModel.getMsg());
date.setText(messagesListDataModel.getDate());
likes.setText(messagesListDataModel.getLikes());

like.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast toast = Toast.makeText(getContext(), "Like button pressed", Toast.LENGTH_SHORT);
toast.show();
//Increment the value of the likes textview and reload that textview to display new likes. Limit the likes to only be able to like a post once.

}
});

reply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast toast = Toast.makeText(getContext(), "reply button pressed", Toast.LENGTH_SHORT);
toast.show();
//passing data for the reference in the replies class.



}
});

return convertView;


}
}

在like.SetOnClickListener中,我如何从Firebase数据库中检索存储为“喜欢”的数据,通过添加1来更新int并将其存储回数据库?

这是否可以在适配器中完成,或者是否需要在填充数据的“主要 Activity ”中进行?我不知道该怎么做。

另一个问题是 TextView 不接受 Int,所以我需要在执行此操作时从字符串转换为 int 并返回?

主要 Activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_room);

//for sending messages to database
btn_send_msg = (Button) findViewById(R.id.btn_send);
input_msg = (EditText) findViewById(R.id.msg_Input);

//Date time
//DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
//Date date = new Date();
//String dtSent = ((dateFormat.format(date).toString()));



//UserData student ID
FirebaseUser loggedinFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
String userId = loggedinFirebaseUser.getUid();

room_name = getIntent().getExtras().get("room_name").toString();
setTitle(" Room - " + room_name);


//stores the reference as a string to be passed onto the userDataReference table
databaseUrlRef = "users/userData" + "/" + userId;
userDataRef = FirebaseDatabase.getInstance().getReference(databaseUrlRef + "/SID");
chatroomsref = FirebaseDatabase.getInstance().getReference("Chatrooms").child(room_name);

///////
final MessagesListAdapter arrayAdapter = new MessagesListAdapter(this,arrayMessages);

ListViewMessages = (ListView) findViewById(R.id.chatRoomMessagesListview);
ListViewMessages.setAdapter(arrayAdapter);


chatroomsref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//fetchData();
//for (DataSnapshot child : dataSnapshot.getChildren()) {
MessagesListDataModel messagesListDataModel =
dataSnapshot.getValue(MessagesListDataModel.class);
arrayMessages.add(messagesListDataModel);

arrayAdapter.notifyDataSetChanged();
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});



/////
//reference to the database that is within the chatrooms and the room name of the name that was clicked and passed onto this activity.
root = FirebaseDatabase.getInstance().getReference("Chatrooms/" + room_name);

userDataRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String Name = dataSnapshot.getValue().toString();
user_name = Name;
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


//https://stackoverflow.com/questions/40891268/how-to-get-firebase-data-into-a-listview
btn_send_msg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String dtSent = ((dateFormat.format(date).toString()));

Map<String, Object> map = new HashMap<String, Object>();
temp_key = root.push().getKey();
// temp key is the randomly generated key
root.updateChildren(map);

DatabaseReference message_root = root.child(temp_key);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("uid", user_name);
map2.put("msg", input_msg.getText().toString());
map2.put("likes","0");
map2.put("date",dtSent);

message_root.updateChildren(map2);
input_msg.setText("");
}
});

}
}

我主要寻找的是如何访问数据库中的结构(Chatrooms/RoomName/UNIQUE_ID/likes),并更新它。其中Unique id 是实际的唯一id。

FATAL EXCEPTION: main Process: com.brunelcs.group13.anyquestions, PID: 8808 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference at com.brunelcs.group13.anyquestions.ChatRoom$1.onButtonClick(ChatRoom.java:87) at com.brunelcs.group13.anyquestions.MessagesListAdapter$1.onClick(MessagesListAdapter.java:81) at android.view.View.performClick(View.java:6256) at android.view.View$PerformClick.run(View.java:24697) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Toast toast = Toast.makeText(getApplicationContext(), getPostKey.toString(), Toast.LENGTH_SHORT);
toast.show();

我放在 getPostKey 之后,

另一个:

if (btnClickListener != null)
btnClickListener.onButtonClick((Integer) view.getTag());}

此外,arrayAdapter 也必须更改为此,因为它出现错误:

 final MessagesListAdapter arrayAdapter = new  MessagesListAdapter(this, arrayMessages, new MessagesListAdapter.ButtonClickListener() {}

这就是我将数据发布到 firebase 的方式:

btn_send_msg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String dtSent = ((dateFormat.format(date).toString()));

Map<String, Object> map = new HashMap<String, Object>();
temp_key = root.push().getKey();
// temp key is the randomly generated key
root.updateChildren(map);

DatabaseReference message_root = root.child(temp_key);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("uid", user_name);
map2.put("msg", input_msg.getText().toString());
map2.put("likes","0");
map2.put("date",dtSent);

message_root.updateChildren(map2);
input_msg.setText("");
}
});

最佳答案

正如您从我在评论中发布的答案中看到的,您可以在 Adapter 类中使用一个 void 方法和 int 创建一个接口(interface)作为参数,例如:

public interface ButtonClickListener {
public abstract void onButtonClick(int position);
}

然后在适配器类的构造函数中使用它,以便您可以在 Activity覆盖它并预制 ButtonClick。例如:

private ButtonClickListener btnClickListener = null;
public MessagesListAdapter(Context context, ArrayList<MessagesListDataModel> dataModels, ButtonClickListener btnClickListener){
super(context,0, dataModels);
this.dataModels = dataModels;
this.btnClickListener = btnClickListener;
}

之后,在 like 按钮上的 AdaptersetTag 和其上的 setOnclickListener 并获取标签。例如:

likes.setTag(position);
likes.setOnClickListener(new View.OnClickListener{
@Override
public void onClick(View v) {

if(btnClickListener != null)
btnClickListener.onButtonClick((Integer) v.getTag());
}
});

完成此操作后,您将能够在 Activity 中的适配器创建中实现点击监听器,然后在内部使用 position 获取 post 的 key 并使用该 key 键,检索您对特定帖子的点赞数,增加并再次设置该值。在模型类中创建一个新的 gettersetter 来存储 key,例如:

private String postKey;

public String getPostKey() {
return postKey;
}

public void setPostKey(String postKey) {
this.postKey = postKey;
}

您需要将 key 存储在 sePostKey 中,以便可以从 getPostKey 获取 key 。然后您可以轻松地从 MessagesListDataModel 获取它:

final MessagesListAdapter arrayAdapter = new  MessagesListAdapter(this,arrayMessages, new ButtonClickListener(){
@Override
public void onButtonClick(int position) {
String getPostKey = arrayMessages.get(position).getPostKey();
//Now you have a key to run another query to get data from specific post and with that number of likes as well
}
});

可能可以简化流程或使用其他更好的方法,但这只是您如何实现您想要的目标的一个想法。我没有测试这段代码,我希望它能起作用。

关于java - 使用自定义 ListView 布局中的按钮增加 Int 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48508013/

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