2014-03-06 56 views
0

我有一个使用CustomAdapter的列表视图。首先,我尝试使用ArrayAdapter显示我的listvew,并且所有项目看起来都不错。但是当我尝试用自定义适配器更改它们时,所有项目都消失了。这里是我的ArrayList我的列表视图为空

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; 
    } 

这里是我的适配器

ListAssignment= dataSource.getAllAssignment(); 
adapter = new CustomAdapter(ListAssignment.this, ListAssignment, R.layout.item_list, 
       new String[] {"two"}, 
       new int[] {R.id.edtype}); 

和我CustomAdapter()

package ims.app.mobileorder; 
import java.util.ArrayList; 
import java.util.HashMap; 
import android.content.Context; 
import android.graphics.Color; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.SimpleAdapter; 

public class CustomAdapter extends SimpleAdapter{ 
    private int[] colors = new int[] { Color.parseColor("#3BB9FF"), Color.parseColor("#306EFF") }; 
    public CustomAdapter(Context context, ArrayList<HashMap<String,String>> mylist, int itemTo, 
      String[] from, int[] to) { 
    super(context, mylist, itemTo, from, to); 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = super.getView(position, convertView, parent); 
     int colorPos = position % colors.length; 
     view.setBackgroundColor(colors[colorPos]); 
     return view; 
    } 

} 
+0

显示适配器代码 –

+1

第一次尝试使用呼叫日志类getAllAssignment()方法后打印列表的大小。 –

+0

Check getCount mush返回data.size()/ data.length –

回答

0

你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; 
    } 
} 

而且当添加项目上E活动:

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:

<?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> 
+0

我应该从哪里得到sqlite的值 – WardaLyn