简体   繁体   English

如何在Android聊天中的ListView中修复此错误

[英]How to fix this error in ListView in an Android Chat

I'm trying to do an Android Chat using ListView and Adapter and to test my code I'm using two TextView each with its send button, the fact is that it works OK when I sequentially first sent a message using the button1 and then the button2 and so on successively and in the Listview the messages are sequentially located, but if I sent several messages only using the button 1 or the button 2, this sequence is lost and the Listview is messed up, the variable myMessage is supposed to be in control but not is working, someone could tell me how I can correct this. 我正在尝试使用ListViewAdapter进行Android Chat ,并测试我的代码,我使用了两个TextView及其发送按钮,事实是,当我依次先使用button1发送消息,然后按button2依次类推,并依次在Listview中定位消息,但是,如果我仅使用按钮1或按钮2发送了几条消息,则此序列丢失并且Listview被弄乱了,变量myMessage应该位于控制但无法正常工作,有人可以告诉我如何解决。

Here is MainActivity: 这是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("");
        }
      }
  });
 }
}

Here is the MessageAdapter class: 这是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);
    }
  }
}

And also ChatBubble class: 还有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;
 }
}

Error #1: Variable type and the variable itself must not have the same name. 错误#1:变量类型和变量本身不能使用相同的名称。

ChatBubble ChatBubble = new ChatBubble(...); // INCORRECT
ChatBubble chatBubble = new ChatBubble(...); // CORRECT

I think that ViewType is switched between even and odd. 我认为ViewType在偶数和奇数之间切换。 How about this code? 这个代码怎么样?

MessageAdapter 消息适配器

@Override
public int getItemViewType(int position) {
    ChatBubble chatBubble = messages.get(position);
    return chatBubble.myMessage() ? 1 : 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM