Android 我的列表视图是空的
我有一个使用Android 我的列表视图是空的,android,listview,Android,Listview,我有一个使用CustomAdapter的列表视图。首先,我尝试用ArrayAdapter显示我的listvew,所有项目看起来都很好。但当我尝试用自定义适配器更改它们时,所有项都消失了。这是我的数组列表 public ArrayList<HashMap<String,String>> getAllAssignment() { ArrayList<HashMap<String,String>> daftarAssignment = new A
CustomAdapter
的列表视图。首先,我尝试用ArrayAdapter显示我的listvew,所有项目看起来都很好。但当我尝试用自定义适配器更改它们时,所有项都消失了。这是我的数组列表
public ArrayList<HashMap<String,String>> getAllAssignment() {
ArrayList<HashMap<String,String>> daftarAssignment = new ArrayList<HashMap<String,String>>();
Cursor cursor = database.rawQuery("select customer.name from assignment, customer where"+
" assignment.customerid=customer.customerid and employeeid = '"+Globals.salesid+"'", null);
cursor.moveToFirst();
if (cursor.moveToFirst()) {
do {
map1 = new HashMap<String,String>();
name=cursor.getString(cursor.getColumnIndex(DBHelper.NAME));
map1.put("one",String.valueOf(daftarAssignment.size()));
map1.put("two",name);
daftarAssignment.add(map1);
} while (cursor.moveToNext());
}
cursor.close();
return daftarAssignment;
}
和我的CustomAdapter()
包ims.app.mobileorder;
导入java.util.ArrayList;
导入java.util.HashMap;
导入android.content.Context;
导入android.graphics.Color;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.simpledapter;
公共类CustomAdapter扩展了SimpleAdapter{
private int[]colors=new int[]{Color.parseColor(“#3BB9FF”)、Color.parseColor(“#306EFF”)};
公共CustomAdapter(上下文上下文、ArrayList mylist、int itemTo、,
字符串[]from,int[]to){
super(上下文、mylist、itemTo、from、to);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=super.getView(位置、转换视图、父级);
int colorPos=位置%colors.length;
视图.setBackgroundColor(颜色[colorPos]);
返回视图;
}
}
您的CustomAdapter应该如下所示:
public class CustomAdapter extends BaseAdapter {
public static final String KEY_ID = "id";
public static final String KEY_TITLE = "title";
public static final String KEY_MESSAGE = "message";
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public CustomAdapter(Activity activity, ArrayList<HashMap<String, String>> data) {
this.activity = activity;
this.data=data;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
HashMap<String, String> map = new HashMap<String, String>();
map = data.get(position);
long id = Long.valueOf(map.get(KEY_ID));
return id;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(convertView==null) {
view = inflater.inflate(R.layout.custom_list_item, null);
}
TextView title = (TextView)view.findViewById(R.id.list_item_title);
TextView message = (TextView)view.findViewById(R.id.list_item_message);
HashMap<String, String> map = new HashMap<String, String>();
map = data.get(position);
title.setText(map.get(KEY_TITLE));
message.setText(map.get(KEY_MESSAGE));
return view;
}
}
公共类CustomAdapter扩展了BaseAdapter{
公共静态最终字符串键\u ID=“ID”;
公共静态最终字符串键\u TITLE=“TITLE”;
公共静态最终字符串键\u MESSAGE=“MESSAGE”;
私人活动;
私有数组列表数据;
专用静态充气机=空;
公共CustomAdapter(活动、ArrayList数据){
这个。活动=活动;
这个。数据=数据;
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
}
public int getCount(){
返回data.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
HashMap=newHashMap();
map=data.get(位置);
long id=long.valueOf(map.get(KEY_id));
返回id;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=转换视图;
if(convertView==null){
视图=充气机。充气(R.layout.custom\u list\u项,空);
}
TextView title=(TextView)view.findViewById(R.id.list\u item\u title);
TextView消息=(TextView)view.findViewById(R.id.list\u item\u消息);
HashMap=newHashMap();
map=data.get(位置);
title.setText(map.get(KEY_title));
message.setText(map.get(KEY_message));
返回视图;
}
}
在活动中添加项目时:
ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < 10; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(CustomAdapter.KEY_ID, String.valueOf(i));
map.put(CustomAdapter.KEY_TITLE, "Title " + i);
map.put(CustomAdapter.KEY_MESSAGE, "Message " + i);
data.add(map);
}
mAdapter = new CustomAdapter(this, data);
mList.setAdapter(mAdapter);
ArrayList data=new ArrayList();
对于(int i=0;i<10;i++){
HashMap=newHashMap();
map.put(CustomAdapter.KEY_ID,String.valueOf(i));
映射放置(CustomAdapter.KEY_TITLE,“TITLE”+i);
map.put(CustomAdapter.KEY_消息,“消息”+i);
数据。添加(地图);
}
mAdapter=新的CustomAdapter(此,数据);
mList.setAdapter(mAdapter);
以及xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="4dip"
>
<TextView
android:id="@+id/list_item_title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textStyle="bold"
/>
<TextView
android:id="@+id/list_item_message"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textStyle="italic"
/>
</LinearLayout>
显示适配器代码在使用Log类调用getAllAssignment()方法后,首先尝试打印列表大小。检查getCount mush return data.size()/data.length检查daftarAssignment.size()的大小;以及演示如何调用CustomAdapter函数。因此我可以提供精确的解决方案。您将此ArrayList ListAssignment=getAllAssignment();?我应该在哪里从sqlite获取值
ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < 10; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(CustomAdapter.KEY_ID, String.valueOf(i));
map.put(CustomAdapter.KEY_TITLE, "Title " + i);
map.put(CustomAdapter.KEY_MESSAGE, "Message " + i);
data.add(map);
}
mAdapter = new CustomAdapter(this, data);
mList.setAdapter(mAdapter);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="4dip"
>
<TextView
android:id="@+id/list_item_title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textStyle="bold"
/>
<TextView
android:id="@+id/list_item_message"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textStyle="italic"
/>
</LinearLayout>