我有一个ListView和自定义适配器,并试图让我的数据显示在ListView内部。我有三个布局来显示数据但无法显示数据。我在列表视图的每一行中都填充了CSSC的硬编码文本,而不是我想要的三种布局及其所需的数据。
CustomAdapter
public class CompleteEReceiptDisplay extends Activity implements AppCompatCallback {
private AppCompatDelegate delegate;
Toolbar mToolbar;
private ImageView menuBtn, backBtn;
ListView mFullReceiptLV;
List<EreceiptPojo> mainList = new ArrayList<>();
private LayoutInflater mInflater;
private RelativeLayout mFullItemLine;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Show Toolbar without extending AppCompatActivity
delegate = AppCompatDelegate.create(this, this);
delegate.onCreate(savedInstanceState);
delegate.setContentView(R.layout.complete_ereceipt_display);
delegate.setSupportActionBar(mToolbar);
mFullReceiptLV = (ListView) findViewById(R.id.fullEReceiptListView);
menuBtn = (ImageView) findViewById(R.id.menuBtn);
backBtn = (ImageView) findViewById(R.id.icnBackArrow);
menuBtn.setVisibility(View.GONE);
backBtn.setVisibility(View.VISIBLE);
backBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
parseList();
FullEReceiptAdapter mFullAdapter = new FullEReceiptAdapter(this);
mFullReceiptLV.setAdapter(mFullAdapter);
}
private void parseList() {
String itemCateg = "", itemPrice = "", itemDesc = "";
HashMap<String, List<HashMap<String, String>>> mHashMap = new HashMap<>();
List<LineDetail> mLineDetailList = MainActivity.mCompleteReceiptData.getLineDetailList().getLineDetail();
for (int i = 0; i < mLineDetailList.size(); i++) {
LineDetail lineDetail = mLineDetailList.get(i);
itemPrice = lineDetail.getPrice();
List<Item> itemList = lineDetail.getCustomAttributes().getItem();
for (int j = 0; j < itemList.size(); j++) {
Item it = itemList.get(j);
if (it.getKey().equalsIgnoreCase("PrintCategory")) {
itemCateg = it.getValue();
} else if (it.getKey().equalsIgnoreCase("ItemDescription")) {
itemDesc = it.getValue();
}
}
HashMap<String, String> hm = new HashMap<>();
hm.put(itemDesc, itemPrice);
if (mHashMap.containsKey(itemCateg)) {
mHashMap.get(itemCateg).add(hm);
} else {
List<HashMap<String, String>> mMap = new ArrayList<>();
mMap.add(hm);
mHashMap.put(itemCateg, mMap);
}
}
for (Map.Entry<String, List<HashMap<String, String>>> entry : mHashMap.entrySet()) {
itemCateg = entry.getKey();
double mDouble = 0.00;
EreceiptPojo ereceiptPojo = new EreceiptPojo();
ereceiptPojo.setItemName(itemCateg);
ereceiptPojo.setCategory(true);
ereceiptPojo.setSubTotal(false);
mainList.add(ereceiptPojo);
List<HashMap<String, String>> dummyList = entry.getValue();
for (int i = 0; i < dummyList.size(); i++) {
HashMap<String, String> hm1 = new HashMap<>();
hm1 = dummyList.get(i);
for (Map.Entry<String, String> entry1 : hm1.entrySet()) {
EreceiptPojo ereceiptPojo1 = new EreceiptPojo();
ereceiptPojo.setItemName(entry1.getKey());
ereceiptPojo1.setItemPrice(entry1.getValue());
ereceiptPojo1.setCategory(false);
ereceiptPojo1.setSubTotal(false);
mainList.add(ereceiptPojo1);
mDouble = mDouble + Double.parseDouble(entry1.getValue());
}
}
EreceiptPojo mEreceiptPojo = new EreceiptPojo();
mEreceiptPojo.setItemPrice(String.valueOf(mDouble));
mEreceiptPojo.setCategory(false);
mEreceiptPojo.setSubTotal(true);
mainList.add(mEreceiptPojo);
}
}
@Override
public void onSupportActionModeStarted(ActionMode mode) {
}
@Override
public void onSupportActionModeFinished(ActionMode mode) {
}
@Nullable
@Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
return null;
}
public class FullEReceiptAdapter extends BaseAdapter {
public static final int TYPE_ITEM = 0;
public static final int TYPE_CATEGORY = 1;
public static final int TYPE_ITEM_NAME = 2;
public static final int TYPE_PRICE = 3;
public FullEReceiptAdapter(Context context) {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mainList.size();
}
@Override
public Object getItem(int position) {
return mainList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
EreceiptPojo obj = mainList.get(position);
ViewHolder holder = null;
int layout_type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.full_receipt_category_header, parent, false);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (obj.isCategory() == true) {
holder = new ViewHolder();
holder.mHeaderView = (LinearLayout) convertView.findViewById(R.id.full_receipt_category_header_layout);
}
if (obj.isSubTotal() == true) {
holder = new ViewHolder();
holder.mLineItemsTotalPrice = (LinearLayout) convertView.findViewById(R.id.subTotalView);
}
if(obj.getItemName().length() > 0 && obj.getItemPrice().length() > 0) {
holder = new ViewHolder();
holder.mLineItemName = (RelativeLayout) convertView.findViewById(R.id.full_receipt_item_line);
}
return convertView;
}
}
public static class ViewHolder {
public LinearLayout mHeaderView;
public TextView mLineItemName;
public TextView mLineItemPrice;
public LinearLayout mLineItemsTotalPrice;
}
}
我尝试连接适配器的三种布局。 第一个布局是在ListView中不断重复的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/full_receipt_category_header_layout">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/full_receipt_category_header_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingLeft="15dp"
android:text="GROCERY"
android:textAllCaps="true"
android:textColor="@color/colorBlack"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/txtLineItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CUB SPLT TOP WHEAT"
android:textSize="16sp"/>
<TextView
android:id="@+id/txtLineItemPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="$1.70*"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/subTotalView">
<View
android:layout_width="50dp"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:background="@color/colorGrey"
android:layout_gravity="right"/>
<TextView
android:id="@+id/txtCategoryTotalPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="7dp"
android:text="$5.70"
android:textColor="@color/colorBlack"
android:layout_gravity="right"/>
</LinearLayout>
The layout that contains the ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_receipt"
android:padding="10dp">
<ImageView
android:id="@+id/bannerReceiptLogo"
android:layout_width="170dp"
android:layout_height="75dp"
android:background="@drawable/img_logo_receipt_cub"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"/>
<TextView
android:id="@+id/bannerAddressHeader"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="@string/storeHeader"
android:layout_below="@id/bannerReceiptLogo"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:textSize="18sp"/>
<ListView
android:id="@+id/fullEReceiptListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/bannerAddressHeader"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp">
</ListView>
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:0)
我能够让这个工作,因为我必须为视图中的每个位置充气我的适配器内的每个布局。可能性在getView()。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
EreceiptPojo obj = mainList.get(position);
TaxDetail taxDetail = new TaxDetail();
ViewHolder holder = null;
if (obj.isCategory()) {
convertView = mInflater.inflate(R.layout.full_receipt_category_header, parent, false);
TextView mCat = (TextView) convertView.findViewById(R.id.full_receipt_category_header_text);
mCat.setText(obj.getItemName());
}
else if (obj.isSubTotal()) {
convertView = mInflater.inflate(R.layout.full_receipt_category_total, parent, false);
TextView subTotal = (TextView) convertView.findViewById(R.id.txtCategoryTotalPrice);
subTotal.setText(obj.getItemPrice());
}
else if(!obj.isSubTotal() && !obj.isCategory()){
convertView = mInflater.inflate(R.layout.full_receipt_item_line, parent, false);
TextView itemName = (TextView) convertView.findViewById(R.id.txtLineItem);
TextView itemPrice = (TextView) convertView.findViewById(R.id.txtLineItemPrice);
itemName.setText(obj.getItemName());
itemPrice.setText(obj.getItemPrice());
} else {
convertView = mInflater.inflate(R.layout.tax_layout, parent, false);
TextView mFinalSubtotal = (TextView) convertView.findViewById(R.id.txtSubTotalFinal);
TextView mTotalTax = (TextView) convertView.findViewById(R.id.txtTaxTotal);
TextView mPurchaseTotal = (TextView) convertView.findViewById(R.id.txtCompleteTotalNumber);
for(int i = 0; i < obj.getItemPrice().length(); i++) {
String subTotalFinal = obj.setItemPrice(i);
}