Android的listView中只显示了一次数组的最后一项
我试图在Android的listView中只显示了一次数组的最后一项,android,listview,Android,Listview,我试图在列表视图中动态添加项,但当我使用自定义适配器时,只有数组的最后一个元素与列表视图绑定 public class CustomAdapterForChat extends BaseAdapter { Context context; ArrayList<String> message; LayoutInflater inflter; public CustomAdapterForChat(Context applicationContext,
列表视图
中动态添加项,但当我使用自定义适配器时,只有数组的最后一个元素与列表视图
绑定
public class CustomAdapterForChat extends BaseAdapter
{
Context context;
ArrayList<String> message;
LayoutInflater inflter;
public CustomAdapterForChat(Context applicationContext, ArrayList<String>
message)
{//, int[] images,String[] statuses,String[] PhoneNumbers
this.message=message;
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return message.size();
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.senderlayout, null);
TextView SenderMesssage=(TextView)view.findViewById(R.id.SenderMesssage);
TextView time=(TextView)view.findViewById(R.id.timeOfmessage);
SenderMesssage.setText(message.get(i));
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
time.setText(date.toString());
return view;
}
}
ListView.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="tahatechno.tahatechno.ChatPage">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical"
>
<ScrollView
android:layout_width="match_parent"
android:layout_weight="20"
android:layout_height="wrap_content">
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stackFromBottom="true">
</ListView>
</ScrollView>
<include
layout="@layout/type_message_area"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Sender.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:paddingLeft="@dimen/paddinfleftchatpage_"
android:paddingRight="@dimen/padding_right_chat_page"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/SenderLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/SenderMesssage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/layout_marginTop_chat_page"
android:layout_weight="6"
android:background="@drawable/rounded_corner"
android:padding="@dimen/text_view_padding"
android:text="Android charting application xml ui design tutorial
with example. Android charting application xml ui design tutorial with
example. Android charting application xml ui design tutorial with example.
Android charting application xml ui design tutorial with example."
android:textColor="#000" />
</LinearLayout>
<LinearLayout
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:id="@+id/timeOfmessage"
android:text="5:25 am"/>
</LinearLayout>
</LinearLayout>
您的循环错误。每次运行循环时,都会将同一项插入列表中。创建要插入列表中的对象的单个实例。或者粘贴循环的代码显示您的代码循环数组列表消息的大小是多少?请将您的设计xml文件和java活动放在创建和附加适配器对象的位置。请查看我的编辑,我已真正更新了xml和java活动??这不是答案,这一定是因为你的循环是错误的。每次运行循环时,都会将同一项插入列表中。创建要插入列表中的对象的单个实例。或者像你说的那样粘贴Loop OK先生的代码@ShayanPourvatan请检查我的编辑..我也添加了循环代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:paddingLeft="@dimen/paddinfleftchatpage_"
android:paddingRight="@dimen/padding_right_chat_page"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/SenderLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/SenderMesssage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/layout_marginTop_chat_page"
android:layout_weight="6"
android:background="@drawable/rounded_corner"
android:padding="@dimen/text_view_padding"
android:text="Android charting application xml ui design tutorial
with example. Android charting application xml ui design tutorial with
example. Android charting application xml ui design tutorial with example.
Android charting application xml ui design tutorial with example."
android:textColor="#000" />
</LinearLayout>
<LinearLayout
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:id="@+id/timeOfmessage"
android:text="5:25 am"/>
</LinearLayout>
</LinearLayout>