Java 自定义列表视图值在添加新值时波动?

Java 自定义列表视图值在添加新值时波动?,java,android,listview,Java,Android,Listview,我正在尝试做聊天应用程序。当发送一条应该在右侧的消息时,收到的消息应该在左侧。我每件事都做得很好。当我在列表视图中添加新值时,上一个值会波动。请问谁能解决我的问题?我已经附上了我的源代码 主要活动: } 自定义适配器: } 谢谢 Kalanidhi M只需在getView方法中更改这一行即可 convertView=充气机.充气机.布局.测试,父项,真;并删除默认convertView行刚刚设置if和else..不工作,我已经发送了msg和agentStatus,如false,但第一次只显示右侧

我正在尝试做聊天应用程序。当发送一条应该在右侧的消息时,收到的消息应该在左侧。我每件事都做得很好。当我在列表视图中添加新值时,上一个值会波动。请问谁能解决我的问题?我已经附上了我的源代码

主要活动:

}

自定义适配器:

}

谢谢
Kalanidhi M

只需在getView方法中更改这一行即可


convertView=充气机.充气机.布局.测试,父项,真;并删除默认convertView行刚刚设置if和else..不工作,我已经发送了msg和agentStatus,如false,但第一次只显示右侧。下一次消息更改为左侧后,请检查此链接并检查其适配器,尝试与您的匹配…谢谢您的参考
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;
     */
}
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);