【发布时间】:2017-04-24 07:03:52
【问题描述】:
我正在尝试在 ListView 中动态添加项目,但是当我使用自定义适配器时,只有数组的最后一个元素与 ListView 绑定。
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;
}
}
这是我调用自定义适配器类的代码,我的数组大小为 5,在自定义适配器构造函数中我得到了正确的值,但在 getView 方法中,i 仅在 0 中传递。
public class ChatPage extends ListActivity {
String listItem[]={"Dell Inspiron", "HTC One X", "HTC Wildfire S",
"HTC Sense", "HTC Sensation XE"};
EditText et;
List<String> arrayList;
ImageView button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_page);
et = (EditText) findViewById(R.id.writemessage);
ArrayList values = new ArrayList();
for (int i = 0; i < listItem.length; i++) {
values.add(listItem[i]);
}
CustomAdapterForChat adapter = new CustomAdapterForChat(this,values);
setListAdapter(adapter);
}
public void onClick(View view) {
CustomAdapterForChat adapter = (CustomAdapterForChat) getListAdapter();
String device;
ArrayList myList = new ArrayList();
for (int i=0;i<adapter.getCount();i++)
{
myList.add(adapter.getItem(i));
}
myList.add(et.getText().toString());
CustomAdapterForChat adapternew = new CustomAdapterForChat(this,myList);
setListAdapter(adapternew);
}
}
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>
【问题讨论】:
-
显示你的代码
loop -
ArrayList
消息的大小是多少? -
请将您的设计 xml 文件和 java 活动放在创建和附加适配器对象的位置。
-
请看我的编辑,我有更新 xml 和 java 活动