我試圖動態地在ListView
中添加項目,但是當我使用自定義適配器時,只有數組的最後一個元素與ListView
綁定。在Android的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>
顯示你的代碼'循環' –
什麼是大小ArrayList消息? –
sasikumar
http://stackoverflow.com/a/18348294/2725196 –