我正在尝试动态添加SELECT *
FROM `locate`
ORDER BY
STR_TO_DATE(`Date`,'%Y-%m-%d') DESC
中的项目但是当我使用自定义适配器时,只有数组的最后一个元素与ListView
绑定。
ListView
这是我调用自定义适配器类的代码,我的数组大小为5,并且在自定义适配器构造函数中,我正确地获得了值,但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;
}
}
方法getView
正在进行i
仅
0
ListView.xml
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);
}
}
Sender.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>
答案 0 :(得分:-2)
你的循环错了。每次运行循环时,都会在列表中插入相同的项目。创建要在列表中插入的对象的单个实例。或者粘贴循环代码