我的Android项目中有一个围绕群发消息传递的活动。消息包含在ListView中,每行都有一个类似的按钮。当按下类似按钮时,具有总喜欢数量的标签增加1(如果未被点击则减去相似的标签)。但是,当您在键盘打开时想要一条消息,然后按下后退按钮时,键盘将关闭,最近喜欢/不喜欢的消息将恢复为之前的类似计数。有什么我可以添加到TextView以防止它这样做?或者我可以对onBackButtonPressed()
做些什么?谢谢你的帮助。
这是我更改likeCount文本的代码:
class MessageInfo {
public String messageID;
public String messageContent;
public String messageLikes;
public String senderID;
}
class ViewHolder {
public TextView messageContent = null;
public TextView messageLikeCount = null;
public Button likeButton = null;
public String messageLikes = null;
public String messageID = null;
public String messageSendTime;
public String senderID;
final CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(),
keys.getIdPool(), // Identity Pool ID
Regions.US_EAST_1 // Region
);
public AmazonDynamoDBClient dynamoDb = Region.getRegion(Regions.US_EAST_1)
.createClient(
AmazonDynamoDBClient.class,
credentialsProvider,
new ClientConfiguration()
);
ViewHolder(View row) {
messageContent = (TextView)row.findViewById(R.id.messageText);
messageLikeCount = (TextView)row.findViewById(R.id.likeCountText);
likeButton = (Button)row.findViewById(R.id.likeBtn);
likeButton.setTypeface(fontAwesome);
}
void populateFrom(final MessageInfo message) {
Log.e("MESSAGE2", "The message is " + message.messageContent);
messageContent.setText(message.messageContent);
messageID = message.messageID;
messageLikes = message.messageLikes;
int likeCount = 0;
if (messageLikes == null) {
messageLikes = "";
}
Log.e("MessageLikes", "Mesage likes is " + messageLikes);
if (messageLikes.contains(userInfo.getProfId())) {
likeButton.setText(getString(R.string.icon_heart_closed));
likeButton.setTextColor(getResources().getColor(R.color.colorHeart));
}
for (int i = 0; i < messageLikes.length(); i++) {
if (messageLikes.charAt(i) == ',') {
likeCount += 1;
}
} // end for loop to get number of likes
messageLikeCount.setText(String.valueOf(likeCount));
likeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("Clicked", "Like button clicked");
likeButton.setText(getString(R.string.icon_heart_closed));
likeButton.setTextColor(getResources().getColor(R.color.colorHeart));
int newLikeCount = Integer.parseInt(messageLikeCount.getText().toString()) + 1;
messageLikeCount.setText(String.valueOf(newLikeCount));
}
});
}
}
class MessageAdapter extends ArrayAdapter<MessageInfo> {
MessageAdapter() {
super(SingleGroupActivity.this, android.R.layout.simple_list_item_1, messageArr);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder messageViewHolder;
Log.e("MESSAGE", "The message is " + messageArr.get(position).messageContent);
if (convertView == null) {
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.message_row_layout, null);
messageViewHolder = new ViewHolder(convertView);
convertView.setTag(messageViewHolder);
} else {
messageViewHolder = (ViewHolder)convertView.getTag();
}
if (messageArr.size() > 0) {
MessageInfo tempMessage = messageArr.get(position);
messageViewHolder.populateFrom(tempMessage);
}
return (convertView);
}
}
答案 0 :(得分:0)
当用户使用以下代码点按“按钮”时,您可以隐藏键盘:
char *