我正在尝试做聊天应用程序。发送应在右侧的消息时,收到的消息应在左侧。我一切都做得很好。当我在列表视图中添加新值时,以前的值已波动。请问有人能解决我的问题吗?我已附上我的源代码。

主要活动 :

public class MainActivity extends AppCompatActivity {
ArrayList<ChatData> data = new ArrayList<ChatData>();

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

    final ListView listView = (ListView) findViewById(R.id.list_item);

    final ChatAdaptor chatAdaptor = new ChatAdaptor(this, getDetails());
    listView.setAdapter(chatAdaptor);
    listView.setDivider(null);

    ImageView imageView = (ImageView) findViewById(R.id.submit);
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            EditText objText = (EditText) findViewById(R.id.text_msg);

          chatAdaptor.add(new ChatData(objText.getText().toString(), false));
            listView.setSelection(chatAdaptor.getCount() - 1);
            objText.setText(null);
          //  chatAdaptor.notifyDataSetChanged();
        }
    });
}

public ArrayList<ChatData> getDetails() {
    data.add(new ChatData("Hi! Agent", false));
    data.add(new ChatData("Hi! Agent ,Are you there ", false));
    data.add(new ChatData("Yeah tell me how can I help you", true));
    data.add(new ChatData("I am getting wrong balance", false));
    return data;

}


}

自定义适配器:

public class ChatAdaptor extends ArrayAdapter<ChatData> {
private Activity activity;

String TAG = "bks";

private List<ChatData> originalData = null;


public ChatAdaptor(Activity activity, ArrayList<ChatData> objects) {
    super(activity, 0, objects);
    this.activity = activity;
    this.originalData = objects;

}

@Override
//called when rendering the list
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if (convertView == null) {
        // convertView = mInflater.inflate(R.layout.search_data, true,null);
        LayoutInflater inflater = activity.getLayoutInflater();
        convertView = inflater.inflate(R.layout.test, null, false);


        if (originalData.get(position).isAgentMsg())
            convertView = inflater.inflate(R.layout.show_chat_agent, null, true);
        else
            convertView = inflater.inflate(R.layout.show_chat_client, null, true);


        // Creates a ViewHolder and store references to the three views
        // we want to bind data to.

        holder = new ViewHolder();
        holder.objMsg = ( TextView ) convertView.findViewById(R.id.msg) ;


        // Bind the data efficiently with the holder.

        convertView.setTag(holder);
    } else {
        // Get the ViewHolder back to get fast access to the TextView
        holder = (ViewHolder) convertView.getTag();
    }
    holder.objMsg.setText(originalData.get(position).getMsg());


    return convertView;
}


static class ViewHolder {
    TextView objMsg;
    /**  TextView objClientText, objAgentText;
     ImageView objClientImage1, objClientImage2;
     ImageView objAgentImage1, objAgentImage2;
     */
}


}

谢谢
卡拉尼迪M

最佳答案

只需在您的getView方法中更改此行

LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.test, parent, true);


if (originalData.get(position).isAgentMsg())
            convertView = inflater.inflate(R.layout.show_chat_agent, parent, true);
        else
            convertView = inflater.inflate(R.layout.show_chat_client, parent, true);

10-05 17:42