我的应用程序中有一个自定义列表视图。用户的需求因他选择的选项而异,结果显示在列表视图中。我通过 usingArrayList<String>
和其他时间代表同一个列表,同时使用 ArrayList<HashMap<String,String>>
. 现在,在使用前一个 ieArrayList<String>
时,listView 行中的结果会完美对齐,但是在使用后一个 ieArrayList<HashMap<String,String>>
时,对齐会变得更糟。我不明白问题可能是什么。请帮忙!
下面我发布了在我的自定义 Listview 中用作一行的 XML 的图像、代码和源代码。
图片链接:(因为我是新用户,所以无法上传图片)
Using ArrayList<String>
http://i46.tinypic.com/2r2b0g5.jpg
Using ArrayList<HashMap<String, String>>
http://i47.tinypic.com/11w7tqe.jpg
代码:
对于Arraylist<HashMap<String,String>>
ArrayList<HashMap<String,String>> searchList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<productName.size();i++)
{
String currentListItem=productName.get(i).toString();
if(textLength<=currentListItem.length()){
//compare the String in EditText with Names in the ArrayList
if(searchText.equalsIgnoreCase(currentListItem.substring(0,textLength))){
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("PRODUCTNAME",productName.get(i));
temp.put("PRODUCTID", productID.get(i));
temp.put("BRANDNAME", brandName.get(i));
temp.put("VENDORNAME", vendor.get(i));
temp.put("OPENINGSTOCK", openingStock.get(i));
temp.put("AVAILABLESTOCK", availableStock.get(i));
temp.put("ADDSTOCK", addStock.get(i));
temp.put("PURCHASRPRICE", purchasePrice.get(i));
temp.put("SALESPRICE", salesPrice.get(i));
searchList.add(temp);
}
}
}
SimpleAdapter aaadapter = new SimpleAdapter(
new_stock_io.this,
searchList,
R.layout.list_otcmain123,
new String[] {"PRODUCTID", "PRODUCTNAME", "BRANDNAME","VENDORNAME","OPENINGSTOCK","AVAILABLESTOCK","ADDSTOCK","SALESPRICE", "PURCHASEPRICE"},
new int[] {R.id.txt_id,R.id.txt_name, R.id.txt_bname, R.id.txt_vendor, R.id.txt_openingstock,R.id.txt_availablestock,R.id.txt_addstock,R.id.txt_salesprice,R.id.txt_purchaseprice}
);
lv.setAdapter(aaadapter);
对于ArrayList<字符串>
List<String> vendorName = new ArrayList<String>();
List<String> vendorID = new ArrayList<String>();
List<Object> objectArray = new ArrayList<Object>();
List<String> stockID = new ArrayList<String>();
List<String> picture = new ArrayList<String>();
List<String> productID = new ArrayList<String>();
List<String> productName = new ArrayList<String>();
List<String> brandName = new ArrayList<String>();
List<String> vendor = new ArrayList<String>();
List<String> openingStock = new ArrayList<String>();
List<String> availableStock = new ArrayList<String>();
List<String> addStock= new ArrayList<String>();
List<String> purchasePrice= new ArrayList<String>();
List<String> salesPrice= new ArrayList<String>();
List<String> subCategoryID = new ArrayList<String>();
List<String> subCategoryName= new ArrayList<String>();
List<String> productCategory= new ArrayList<String>();
List<String> subCategoriesNameArray = new ArrayList<String>();
List<String> subCategoriesIDArray= new ArrayList<String>();
for (int i = 0; i < productID.size(); i++)
{
Object obj = new Object();
objectArray.add(obj);
}
adapter = new ListItemsAdapter(objectArray, 1);
lv.setAdapter(adapter);
//List Adapter for listView.
public class ListItemsAdapter extends ArrayAdapter<Object> {
ViewHolder holder1;
public ListItemsAdapter(List<Object> items, int x) {
super(new_stock_io.this,android.R.layout.simple_list_item_1, items);
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.list_otcmain123, null);
holder1 = new ViewHolder();
holder1.tv_id=(TextView)convertView.findViewById(R.id.txt_id);
holder1.tv_name=(TextView)convertView.findViewById(R.id.txt_name);
holder1.tv_brandname=(TextView)convertView.findViewById(R.id.txt_bname);
holder1.tv_openingstock=(TextView)convertView.findViewById(R.id.txt_openingstock);
holder1.tv_availablestock=(TextView)convertView.findViewById(R.id.txt_availablestock);
holder1.tv_addstock=(TextView)convertView.findViewById(R.id.txt_addstock);
holder1.tv_addstock.setTag(position);
holder1.tv_addstock.setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
// TODO Auto-generated method stub
Object o = (Object)holder1.tv_addstock.getTag();
Log.d("method focus", "call");
Log.d("position", position+"");
Log.d("edit text value", o.toString());
Log.d("value",":"+holder1.tv_addstock.getText().toString());
addStock.set(position, holder1.tv_addstock.getText().toString());
}
});
holder1.tv_prizeperunit=(TextView)convertView.findViewById(R.id.txt_salesprice);
holder1.tv_vendor=(TextView)convertView.findViewById(R.id.txt_vendor);
holder1.tv_prizeperunitpurchase=(TextView)convertView.findViewById(R.id.txt_purchaseprice);
convertView.setTag(holder1);
if (checkboxClicked==0)
{
holder1.tv_addstock.setTag(objectArray.get(position));
holder1.tv_id.setText(productID.get(position));
holder1.tv_name.setText(productName.get(position));
holder1.tv_brandname.setText(brandName.get(position));
holder1.tv_openingstock.setText(openingStock.get(position));
holder1.tv_availablestock.setText(availableStock.get(position));
holder1.tv_addstock.setId(position);
holder1.tv_addstock.setText(addStock.get(position));
holder1.tv_prizeperunit.setText(salesPrice.get(position));
holder1.tv_vendor.setText(vendor_Name);
holder1.tv_prizeperunitpurchase.setText(purchasePrice.get(position));
}
else if (checkboxClicked==1)
{
//holder1.tv_addstock.setTag(objectArray.get(position));
holder1.tv_id.setText(onCheckProductID.get(position));
holder1.tv_name.setText(onCheckProductName.get(position));
holder1.tv_brandname.setText(onCheckBrandName.get(position));
holder1.tv_openingstock.setText(onCheckOpeningStock.get(position));
holder1.tv_availablestock.setText(onCheckAvailableStock.get(position));
holder1.tv_addstock.setId(position);
holder1.tv_addstock.setText(onCheckAddStock.get(position));
holder1.tv_prizeperunit.setText(onCheckSalesPrice.get(position));
holder1.tv_vendor.setText(onCheckVendor.get(position));
holder1.tv_prizeperunitpurchase.setText(onCheckPurchasePrice.get(position));
}
return convertView;
}
}
//Simple holder class for above Adapter class.
public class ViewHolder
{
TextView tv_id,tv_name,tv_brandname,tv_vendor,tv_openingstock,tv_availablestock,tv_addstock,tv_prizeperunit,tv_prizeperunitpurchase;
}
XML:
<LinearLayout android:id="@+id/ll_list_header" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/txt_id" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.05" android:gravity="center" android:text="ID" android:textSize="6dip" android:textStyle="bold" /> <View android:id="@+id/view1" android:layout_width="1dip" android:layout_height="fill_parent" android:background="#e5e5e5" /> <TextView android:id="@+id/txt_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.07" android:gravity="center" android:text="Name" android:textSize="6dip" android:textStyle="bold"/> <View android:id="@+id/view2" android:layout_width="1dip" android:layout_height="fill_parent" android:background="#e5e5e5" /> <View android:id="@+id/view3" android:layout_width="1dip" android:layout_height="fill_parent" android:background="#e5e5e5" /> --> <TextView android:id="@+id/txt_bname" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.12" android:gravity="center" android:text="Brand Name" android:textSize="6dip" android:textStyle="bold"/> <View android:id="@+id/view3" android:layout_width="1dip" android:layout_height="fill_parent" android:background="#e5e5e5" /> <TextView android:id="@+id/txt_vendor" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.08" android:gravity="center" android:text="Vendor" android:textSize="6dip" android:textStyle="bold"/> <View android:id="@+id/view27" android:layout_width="1dip" android:layout_height="fill_parent" android:background="#e5e5e5" /> <TextView android:id="@+id/txt_openingstock" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.09" android:gravity="center" android:text="Opening Stock" android:textSize="6dip" android:textStyle="bold"/> <View android:id="@+id/view26" android:layout_width="1dip" android:layout_height="fill_parent" android:background="#e5e5e5" /> <TextView android:id="@+id/txt_availablestock" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.09" android:gravity="center" android:text="Available Stock" android:textSize="6dip" android:textStyle="bold"/> <View android:id="@+id/view25" android:layout_width="1dip" android:layout_height="fill_parent" android:background="#e5e5e5" /> <TextView android:id="@+id/txt_addstock" android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="0.10" android:gravity="center" android:hint="Rs." android:textSize="6dip" android:textStyle="bold" /> <View android:id="@+id/view24" android:layout_width="1dip" android:layout_height="fill_parent" android:background="#e5e5e5" /> <TextView android:id="@+id/txt_salesprice" android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="0.10" android:gravity="center" android:hint="Rs." android:textSize="6dip" android:textStyle="bold" android:width="7dp" /> <View android:id="@+id/view24" android:layout_width="1dip" android:layout_height="fill_parent" android:background="#e5e5e5" /> <TextView android:id="@+id/txt_purchaseprice" android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="0.12" android:gravity="center" android:hint="Rs." android:textSize="6dip" android:textStyle="bold" android:width="7dp"/> </LinearLayout> </RelativeLayout>