ListView不显示项目(使用改造来检索列表)

时间:2016-04-01 12:43:35

标签: android listview android-fragments android-adapter

大家好我还在学习安卓系统,这是我第一次使用改造来从服务器填充列表来构建列表。我没有收到任何错误,应用程序没有崩溃,我的日志显示我从服务器接收数据。

我的适用于列表视图的适配器:

public class TransactionsAdapter extends BaseAdapter {
ArrayList<Transactions> transactions;
public TransactionsAdapter(ArrayList<Transactions> transactions) {
    this.transactions=transactions;
}

@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view=null;
    ViewHolder viewHolder = null;

    if(convertView == null)
    {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.izvjestaji_item,parent,false);
        viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);

    }
    else{
        view = convertView;
        viewHolder= (ViewHolder) view.getTag();

    }

    Transactions transactions = (Transactions) getItem(position);

    if(transactions != null) {
        viewHolder.datum.setText(transactions.getPurchaseDate());
        viewHolder.partner.setText(transactions.getMerchantName());
        viewHolder.iznos.setText(transactions.getTransactionMoneyAmount().toString());
        viewHolder.brojbodova.setText(transactions.getSalesAmount().toString());
    }

    return view;
}


private class ViewHolder{
    TextView datum;
    TextView partner;
    TextView iznos;
    TextView brojbodova;

    public ViewHolder(View view) {
        this.datum = (TextView) view.findViewById(R.id.izvjestaji_datum);
        this.partner = (TextView) view.findViewById(R.id.izvjestaji_partner);
        this.iznos = (TextView) view.findViewById(R.id.izvjestaji_iznos);
        this.brojbodova=(TextView)view.findViewById(R.id.izvjestaji_brojbodova);
    }
}

}

通过jsonschema2pojo.org生成服务器返回的模型

import javax.annotation.Generated;

@Generated("org.jsonschema2pojo")
public class Transactions {

private Integer Id;
private String PurchaseDate;
private Integer Month;
private Integer Year;
private Double SalesAmount;
private Object SpecialSalesActionId;
private Integer PosTerminalId;
private Integer CardId;
private String MerchantName;
private Integer TransactionPointsAmount;
private Double TransactionMoneyAmount;
private Boolean IsDeleted;
private Boolean IsVoucher;
private Integer LoyaltyLevel;
private Integer CategoryId;
private Object CardNo;
private Integer MerchantId;

/**
 *
 * @return
 * The Id
 */
public Integer getId() {
    return Id;
}

/**
 *
 * @param Id
 * The Id
 */
public void setId(Integer Id) {
    this.Id = Id;
}

/**
 *
 * @return
 * The PurchaseDate
 */
public String getPurchaseDate() {
    return PurchaseDate;
}

/**
 *
 * @param PurchaseDate
 * The PurchaseDate
 */
public void setPurchaseDate(String PurchaseDate) {
    this.PurchaseDate = PurchaseDate;
}

/**
 *
 * @return
 * The Month
 */
public Integer getMonth() {
    return Month;
}

/**
 *
 * @param Month
 * The Month
 */
public void setMonth(Integer Month) {
    this.Month = Month;
}

/**
 *
 * @return
 * The Year
 */
public Integer getYear() {
    return Year;
}

/**
 *
 * @param Year
 * The Year
 */
public void setYear(Integer Year) {
    this.Year = Year;
}

/**
 *
 * @return
 * The SalesAmount
 */
public Double getSalesAmount() {
    return SalesAmount;
}

/**
 *
 * @param SalesAmount
 * The SalesAmount
 */
public void setSalesAmount(Double SalesAmount) {
    this.SalesAmount = SalesAmount;
}

/**
 *
 * @return
 * The SpecialSalesActionId
 */
public Object getSpecialSalesActionId() {
    return SpecialSalesActionId;
}

/**
 *
 * @param SpecialSalesActionId
 * The SpecialSalesActionId
 */
public void setSpecialSalesActionId(Object SpecialSalesActionId) {
    this.SpecialSalesActionId = SpecialSalesActionId;
}

/**
 *
 * @return
 * The PosTerminalId
 */
public Integer getPosTerminalId() {
    return PosTerminalId;
}

/**
 *
 * @param PosTerminalId
 * The PosTerminalId
 */
public void setPosTerminalId(Integer PosTerminalId) {
    this.PosTerminalId = PosTerminalId;
}

/**
 *
 * @return
 * The CardId
 */
public Integer getCardId() {
    return CardId;
}

/**
 *
 * @param CardId
 * The CardId
 */
public void setCardId(Integer CardId) {
    this.CardId = CardId;
}

/**
 *
 * @return
 * The MerchantName
 */
public String getMerchantName() {
    return MerchantName;
}

/**
 *
 * @param MerchantName
 * The MerchantName
 */
public void setMerchantName(String MerchantName) {
    this.MerchantName = MerchantName;
}

/**
 *
 * @return
 * The TransactionPointsAmount
 */
public Integer getTransactionPointsAmount() {
    return TransactionPointsAmount;
}

/**
 *
 * @param TransactionPointsAmount
 * The TransactionPointsAmount
 */
public void setTransactionPointsAmount(Integer TransactionPointsAmount) {
    this.TransactionPointsAmount = TransactionPointsAmount;
}

/**
 *
 * @return
 * The TransactionMoneyAmount
 */
public Double getTransactionMoneyAmount() {
    return TransactionMoneyAmount;
}

/**
 *
 * @param TransactionMoneyAmount
 * The TransactionMoneyAmount
 */
public void setTransactionMoneyAmount(Double TransactionMoneyAmount) {
    this.TransactionMoneyAmount = TransactionMoneyAmount;
}

/**
 *
 * @return
 * The IsDeleted
 */
public Boolean getIsDeleted() {
    return IsDeleted;
}

/**
 *
 * @param IsDeleted
 * The IsDeleted
 */
public void setIsDeleted(Boolean IsDeleted) {
    this.IsDeleted = IsDeleted;
}

/**
 *
 * @return
 * The IsVoucher
 */
public Boolean getIsVoucher() {
    return IsVoucher;
}

/**
 *
 * @param IsVoucher
 * The IsVoucher
 */
public void setIsVoucher(Boolean IsVoucher) {
    this.IsVoucher = IsVoucher;
}

/**
 *
 * @return
 * The LoyaltyLevel
 */
public Integer getLoyaltyLevel() {
    return LoyaltyLevel;
}

/**
 *
 * @param LoyaltyLevel
 * The LoyaltyLevel
 */
public void setLoyaltyLevel(Integer LoyaltyLevel) {
    this.LoyaltyLevel = LoyaltyLevel;
}

/**
 *
 * @return
 * The CategoryId
 */
public Integer getCategoryId() {
    return CategoryId;
}

/**
 *
 * @param CategoryId
 * The CategoryId
 */
public void setCategoryId(Integer CategoryId) {
    this.CategoryId = CategoryId;
}

/**
 *
 * @return
 * The CardNo
 */
public Object getCardNo() {
    return CardNo;
}

/**
 *
 * @param CardNo
 * The CardNo
 */
public void setCardNo(Object CardNo) {
    this.CardNo = CardNo;
}

/**
 *
 * @return
 * The MerchantId
 */
public Integer getMerchantId() {
    return MerchantId;
}

/**
 *
 * @param MerchantId
 * The MerchantId
 */
public void setMerchantId(Integer MerchantId) {
    this.MerchantId = MerchantId;
}

}

要包含列表的片段:

public class Izvjestaji extends Fragment {

ListView list;
@Override

public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle                                                                                                 savedInstanceState) {
    // Defines the xml file for the fragment


    return inflater.inflate(R.layout.izvjestaji, parent, false);

}

// This event is triggered soon after onCreateView().
// Any view setup should occur here.  E.g., view lookups and attaching view listeners.
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    list=(ListView)view.findViewById(R.id.izvjestaji_list);
    showList();

    // Setup any handles to view objects here
    // EditText etFoo = (EditText) view.findViewById(R.id.etFoo);

}
public void showList(){
    NetworkSDK.getInstance().getTransactions(new Callback<List<Transactions>>() {
        @Override
        public void onResponse(Call<List<Transactions>> call, Response<List<Transactions>> response) {
            if(response.isSuccess()){
                Log.d("Data", String.valueOf(response.isSuccess()));
                TransactionsAdapter transactionsAdapter=new TransactionsAdapter((ArrayList<Transactions>)response.body());
                list.setAdapter(transactionsAdapter);


            }
        }

        @Override
        public void onFailure(Call<List<Transactions>> call, Throwable t) {
            Log.d("Error","Def error");

        }
    });
}
}

2个xml文件(一个来自片段,另一个是列表中的单个项目) 片段布局:

<?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" >


<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/izvjestaji_list" />
</LinearLayout>

Item_Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Datum"
    android:id="@+id/izvjestaji_datum"
    android:gravity="center"
    android:background="#a9a8a8"
    android:layout_weight="1"
    android:textSize="20dp" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/izvjestaji_partner"
    android:text="Partner"
    android:gravity="center"
    android:background="#a9a8a8"

    android:layout_weight="1"
    android:textSize="20dp" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/izvjestaji_iznos"
    android:text="Iznos"
    android:background="#a9a8a8"
    android:gravity="center"

    android:layout_weight="1"
    android:textSize="20dp" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/izvjestaji_brojbodova"
    android:text="Broj Bodova"
    android:gravity="center"
    android:background="#a9a8a8"

    android:layout_weight="1"
    android:textSize="20dp" />
    </LinearLayout>

在我的预览中,我只获得空片段

2 个答案:

答案 0 :(得分:0)

更改为

@Override
public int getCount() {
return transactions.size();
}

@Override
public Object getItem(int position) {
return transactions.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

答案 1 :(得分:0)

TransactionAdapter课程中,您尚未实施覆盖方法

使用这些

 @Override
public int getCount() {
    return transactions.size();
}

@Override
public Object getItem(int position) {
    return transactions.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}