我正在尝试使用Android Chat和ListView进行Adapter并测试我的代码,我正在使用两个TextView及其发送按钮,事实是,当我依次使用以下命令第一次发送消息时,它可以正常工作按钮1和按钮2依次类推,并依次在Listview中定位消息,但是如果我仅使用按钮1或按钮2发送了几条消息,则此序列丢失,并且Listview被弄乱了,变量应该可以控制,但无法正常工作,有人可以告诉我如何解决。这是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;@Overrideprotected 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 firstadapter = new MessageAdapter(this, R.layout.left_chat_bubble, ChatBubbles);listView.setAdapter(adapter);//event for button SEND1btnSend1.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 SEND2btnSend2.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;}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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; }} (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 我认为ViewType在偶数和奇数之间切换。这个代码怎么样?消息适配器@Overridepublic int getItemViewType(int position) { ChatBubble chatBubble = messages.get(position); return chatBubble.myMessage() ? 1 : 0;} (adsbygoogle = window.adsbygoogle || []).push({}); 07-27 22:55