データベースからリストビューにデータを渡すために特別なアダプタを使用するリストビューがあります。ブレークポイントを配置してプロジェクトをデバッグすると、すべての詳細がハッシュマップに渡されますが、リストビューには特定の項目が 1 つだけ繰り返し表示されます。コードを以下に示します。
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();
}
}