[英]How to fix this error in ListView in an Android Chat
我正在嘗試使用ListView
和Adapter
進行Android Chat
,並測試我的代碼,我使用了兩個TextView及其發送按鈕,事實是,當我依次先使用button1發送消息,然后按button2依次類推,並依次在Listview中定位消息,但是,如果我僅使用按鈕1或按鈕2發送了幾條消息,則此序列丟失並且Listview被弄亂了,變量myMessage
應該位於控制但無法正常工作,有人可以告訴我如何解決。
這是MainActivity:
public class MainActivity extends AppCompatActivity {
private ListView listView;
private View btnSend;
private EditText editText;
boolean myMessage = true;
private List<ChatBubble> ChatBubbles;
private ArrayAdapter<ChatBubble> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ChatBubbles = new ArrayList<>();
listView = (ListView) findViewById(R.id.list_msg);
btnSend1 = findViewById(R.id.btn_chat_send1);
btnSend2 = findViewById(R.id.btn_chat_send2);
editText1 = (EditText) findViewById(R.id.msg_type1);
editText2 = (EditText) findViewById(R.id.msg_type2);
//set ListView adapter first
adapter = new MessageAdapter(this, R.layout.left_chat_bubble, ChatBubbles);
listView.setAdapter(adapter);
//event for button SEND1
btnSend1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (editText1.getText().toString().trim().equals("")) {
Toast.makeText(MainActivity.this, "Please input some text...", Toast.LENGTH_SHORT).show();
} else {
myMessage = true;
ChatBubble ChatBubble = new ChatBubble(editText1.getText().toString(), myMessage);
ChatBubbles.add(ChatBubble);
adapter.notifyDataSetChanged();
editText1.setText("");
}
}
});
//event for button SEND2
btnSend2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (editText2.getText().toString().trim().equals("")) {
Toast.makeText(MainActivity.this, "Please input some text...", Toast.LENGTH_SHORT).show();
} else {
myMessage = false;
ChatBubble ChatBubble = new ChatBubble(editText2.getText().toString(), myMessage);
ChatBubbles.add(ChatBubble);
adapter.notifyDataSetChanged();
editText2.setText("");
}
}
});
}
}
這是MessageAdapter類:
public class MessageAdapter extends ArrayAdapter<ChatBubble> {
private Activity activity;
private List<ChatBubble> messages;
public MessageAdapter(Activity context, int resource, List<ChatBubble> objects) {
super(context, resource, objects);
this.activity = context;
this.messages = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
int layoutResource = 0; // determined by view type
ChatBubble ChatBubble = getItem(position);
int viewType = getItemViewType(position);
if (ChatBubble.myMessage()) {
layoutResource = R.layout.left_chat_bubble;
} else {
layoutResource = R.layout.right_chat_bubble;
}
if (convertView != null) {
holder = (ViewHolder) convertView.getTag();
} else {
convertView = inflater.inflate(layoutResource, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
//set message content
holder.msg.setText(ChatBubble.getContent());
return convertView;
}
@Override
public int getViewTypeCount() {
// return the total number of view types. this value should never change
// at runtime. Value 2 is returned because of left and right views.
return 2;
}
@Override
public int getItemViewType(int position) {
// return a value between 0 and (getViewTypeCount - 1)
return position % 2;
}
private class ViewHolder {
private TextView msg;
public ViewHolder(View v) {
msg = (TextView) v.findViewById(R.id.txt_msg);
}
}
}
還有ChatBubble類:
public class ChatBubble {
private String content;
private boolean myMessage;
public ChatBubble(String content, boolean myMessage) {
this.content = content;
this.myMessage = myMessage;
}
public String getContent() {
return content;
}
public boolean myMessage() {
return myMessage;
}
}
錯誤#1:變量類型和變量本身不能使用相同的名稱。
ChatBubble ChatBubble = new ChatBubble(...); // INCORRECT
ChatBubble chatBubble = new ChatBubble(...); // CORRECT
我認為ViewType在偶數和奇數之間切換。 這個代碼怎么樣?
消息適配器
@Override
public int getItemViewType(int position) {
ChatBubble chatBubble = messages.get(position);
return chatBubble.myMessage() ? 1 : 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.