我有一个listview,它使用一个特殊的适配器将数据从数据库传递到listview。但问题是我只是重复了一个listview项目,而其他细节没有显示。当我放置断点并调试项目时,所有细节都将传递给hashmap,但listview只是重复显示一个特定项目。代码如下所示:
static class ViewHolder {
TextView txtmername,txtmerid,txtmeradd,txtmermeasure;
Button btnselect;
}
private LayoutInflater mInflater;
public SpecialAdapter(Context ctx,List<HashMap<String, String>> listData, int resourceId, String[] columnTags, int[] columnIds) {
super(ctx, listData, resourceId, columnTags, columnIds);
ctx=MerchantList.this;
listData=listData;
resourceId=R.layout.merchant_listview;
columnTags=columnTags;
columnIds=columnIds;
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return super.getItem(position);
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
Context con=getApplicationContext();
if(convertView==null)
// convertView = getActivity().getLayoutInflater().inflate(R.layout.stores_listview_layout, pa
mInflater = (LayoutInflater)con.getSystemService(con.LAYOUT_INFLATER_SERVICE);
convertView=mInflater.inflate(R.layout.merchantlistview, null);
holder = new ViewHolder();
holder.txtmername = (TextView) convertView.findViewById(R.id.lblMerchantName);
holder.txtmeradd=(TextView)convertView.findViewById(R.id.lblAddress);
holder.txtmerid=(TextView)convertView.findViewById(R.id.lblMerchantId);
holder.txtmermeasure=(TextView)convertView.findViewById(R.id.lblMeasure);
holder.btnselect=(Button)convertView.findViewById(R.id.btnSelect);
holder.btnselect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
});
convertView.setTag(holder);
if(convertView !=null)
{
holder=(ViewHolder)convertView.getTag();
// Binding the data efficiently with the holder
SQLiteDatabase db=openOrCreateDatabase("NEW.db", MODE_PRIVATE, null);
Cursor OrderCursor = db.rawQuery("SELECT DISTINCT(A.ID), B.NAME, B.ADDRESS, A.MEASURE FROM (SELECT ID, MIN( CAST(STOCK_IN_HAND AS REAL) /REORDER_LEVEL) AS MEASURE FROM INVENTORY GROUP BY ID) A INNER JOIN MASTER B ON A.ID=B.ID ORDER BY A.MEASURE", null);
listData.clear();
if(OrderCursor!= null)
{
if(OrderCursor.moveToFirst()){
for (int i = 0; i < OrderCursor.getCount(); i++){
// String first,second,third,fourth=null;
HashMap<String,String> map = new HashMap<String, String>();
map.put(columnTags[0], OrderCursor.getString(OrderCursor.getColumnIndex("NAME")));
map.put(columnTags[1], OrderCursor.getString(OrderCursor.getColumnIndex("ADDRESS")));
map.put(columnTags[2], OrderCursor.getString(OrderCursor.getColumnIndex("ID")));
map.put(columnTags[3], OrderCursor.getString(OrderCursor.getColumnIndex("MEASURE")));
listData.add(map);
String measure = map.get("measure").toString();
String name=map.get("Name").toString();
String address=map.get("Address").toString();
String id=map.get("Id").toString();
holder.txtmerchantname.setText(name);
holder.txtmeradd.setText(address);
holder.txtmerid.setText(id);
holder.txtmermeasure.setText(measure);
double measure1=Double.parseDouble(measure);
if(measure1 > 1.5)
{
convertView.setBackgroundColor(getResources().getColor(R.color.Green));
}
else if((1.5 >= measure1 ) && (measure1>1.0))
{
convertView.setBackgroundColor(getResources().getColor(R.color.Yellow));
}
else if(1.0>=measure1)
{
convertView.setBackgroundColor(Color.RED);
}
OrderCursor.moveToNext();
}//end of for
}
OrderCursor.close();
db.close();
}
}
return convertView;
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public int getViewTypeCount() {
return super.getViewTypeCount();
}
}
答案 0 :(得分:0)
尝试使用以下代码,我猜这将有所帮助。因此,您正在使用可以使用arrayAdapter的hashmap。
答案 1 :(得分:0)
getViewTypeCount()
与您的listView
项目数量不匹配,因此会累计另一项目
在你的情况下,我认为它是关于你正在使用的measure
条件
最后,您应该使用else
代替else if
OR
如果还有其他情况,你必须对待它。