Listview没有显示使用自定义适配器 - android

时间:2014-03-08 08:53:08

标签: android listview adapter

有人能告诉我为什么Listview没有出现吗?我从CustomAdapter调用项目,从对象类调用它的字符串。它将在列表视图中显示来自Web的多个返回。这是我的代码,包括自定义适配器。

public class ClaimVoucherDetailsActivity extends FragmentActivity implements OnClickListener{

ArrayList<VoucherObj> items;
Button claimVoucher;
EditText etEmail,etVoucherCode;
TextView tvName,tvEmail,tvVoucherCode,tvIssueBranch,tvDateIssued,tvExpiration,tvStatus,tvVoucherSource;
String sName,sEmail,sVoucher,sIssueBranch,sDateIssued,sExpiration,sStatus,sVoucherName,sCustomerID,sType,empID,branch,brID;
ImageLoader imageLoader;
DisplayImageOptions options;
ImageView voucher;
Spinner spinEmployee;
SQLiteAdapter mysqlAdapter;
ProgressDialog progressDialog;
VoucherObj vouch;

 private ClaimVoucherAdapter m_adapter;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.voucher_details);
    imageLoader = ImageLoader.getInstance();

    ImageLoaderConfig();

    options = new DisplayImageOptions.Builder()
    .showStubImage(R.drawable.ic_launcher)
    .showImageForEmptyUri(R.drawable.ic_launcher)
    .cacheInMemory()
    .cacheOnDisc()
    .build();

    mysqlAdapter = new SQLiteAdapter(ClaimVoucherDetailsActivity.this);
    mysqlAdapter.openToRead();

    branch = mysqlAdapter.getValue("SELECT branch from branch_tb");
    brID =  mysqlAdapter.getValue("SELECT ID from branch_tb where branch = '"+branch+"'");
    TextView branchTV = (TextView)findViewById(R.id.branch);
    branchTV.setText(branch);



    Intent intent = this.getIntent();       
    sCustomerID = intent.getStringExtra("customerID").trim();
    sType = intent.getStringExtra("type").trim();
    sName = intent.getStringExtra("name").trim();
    sEmail = intent.getStringExtra("email").trim();
    sVoucher = intent.getStringExtra("voucher").trim();
    sIssueBranch = intent.getStringExtra("branch").trim();
    sDateIssued = intent.getStringExtra("issued").trim();
    sExpiration = intent.getStringExtra("expiration").trim();
    sStatus = intent.getStringExtra("status").trim();
    sVoucherName = intent.getStringExtra("vouchername").trim();
    String employ = intent.getStringExtra("employeeid").trim();

    ListView lstitems = (ListView) this.findViewById(R.id.listView1);       
    items = new ArrayList<VoucherObj>();        
    vouch = new VoucherObj();
    items.add(vouch);                         
    m_adapter = new ClaimVoucherAdapter(ClaimVoucherDetailsActivity.this,R.layout.list, items);
    lstitems.setAdapter(m_adapter);

优惠券等级

public class VoucherObj {

public String customerID="";
public String type="";
public String name="";
public String email="";
public String voucher="";
public String branch="";
public String issued = "";
public String expiration="";
public String status = "";
public String vouchername = "";
public String employeeid = "";

}

适配器类

 public class ClaimVoucherAdapter extends ArrayAdapter<String> {

 private ArrayList<VoucherObj> items;
 private LayoutInflater vi;

 TextView tvName,tvEmail,tvVoucherCode,tvIssueBranch,tvDateIssued,tvExpiration,tvStatus,tvVoucherSource;
 public ClaimVoucherAdapter(Context context, int textViewResourceId,  ArrayList<VoucherObj> myList) {
    super(context, textViewResourceId);
    this.items = myList;
    vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    final VoucherObj i = items.get(position);
    if (i != null) {

        final VoucherObj ei = (VoucherObj)i;
        v = vi.inflate(R.layout.voucher_details, null);

        tvName = (TextView) v.findViewById(R.id.tvName);
        tvEmail = (TextView)v.findViewById(R.id.tvEmail);
        tvVoucherCode = (TextView)v.findViewById(R.id.tvVoucherCode);
        tvIssueBranch = (TextView)v.findViewById(R.id.tvIssuingBranch);
        tvDateIssued = (TextView)v.findViewById(R.id.tvDateIssued);
        tvExpiration = (TextView)v.findViewById(R.id.tvExpirationDate);
        tvStatus = (TextView)v.findViewById(R.id.tvStatus);     
        tvVoucherSource = (TextView)v.findViewById(R.id.tvVoucherSource);


        tvName.setText(items.get(position).toString());
        tvEmail.setText(items.get(position).toString());
        tvVoucherCode.setText(items.get(position).toString());
        tvIssueBranch.setText(items.get(position).toString());
        tvDateIssued.setText(items.get(position).toString());
        tvExpiration.setText(items.get(position).toString());
        tvStatus.setText(items.get(position).toString());
        tvVoucherSource.setText(items.get(position).toString());





}
    return v;}}

2 个答案:

答案 0 :(得分:4)

你也需要将你的列表传递给超级构造函数,

使用以下代码:

使用

super(context, textViewResourceId, myList);

当您使用ArrayAdapter时,您必须将列表传递给超级构造函数以处理列表的大小。您也可以使用BaseAdapter并覆盖getCount()方法以返回列表大小

修改

你的代码上有super(context, textViewResourceId);,构造函数的第一行,删除它并放入我的代码。

所以你必须:

public ClaimVoucherAdapter(Context context, int textViewResourceId,  ArrayList<VoucherObj> myList) {
    super(context, textViewResourceId, myList);
    this.items = myList;
    vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 }

您需要将ArrayAdapter<String>更改为ArrayAdapter<VoucherObj>

<强>更新

正如您在评论中所说,tvName.setText(items.get(position).toString());

上有 NPE

因此存在两种可能性:

1- tvName为空,用于处理tvName上的检查voucher_details

2- items为空,

更新1

您使用items.get(position).toString(),我认为您的列表中有一个空对象。

所以你有2路,

toString类上覆盖VoucherObj方法。

2 - 将items.get(position).toString()更改为另一个值

答案 1 :(得分:1)

更改此内容
 public ClaimVoucherAdapter(Context context, int textViewResourceId,  ArrayList<VoucherObj> myList) {
 super(context, textViewResourceId);
 this.items = myList;
 vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 }

 public ClaimVoucherAdapter(Context context, int textViewResourceId,  ArrayList<VoucherObj> myList) {
 super(context, textViewResourceId,myList);
 this.items = myList;
 vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 }