Reputation: 399
Can someone tell me why the Listview is not showing up? I call the items from a CustomAdapter and its string from object class. It will show multiple return from web in a listview. Here's my code including the custom adapater.
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);
Voucher Class
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 = "";
}
Adapter Class
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;}}
Upvotes: 0
Views: 2099
Reputation: 11948
you need pass your list to super constructor too,
use following code:
use
super(context, textViewResourceId, myList);
when you use ArrayAdapter
you must pass your list to super constructor for handling size of your list. you can use BaseAdapter
too and override getCount()
method for returning size of your list
EDIT
you have super(context, textViewResourceId);
on your code, first line of your constructor, remove that and put my code.
so you must have:
public ClaimVoucherAdapter(Context context, int textViewResourceId, ArrayList<VoucherObj> myList) {
super(context, textViewResourceId, myList);
this.items = myList;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
you need change ArrayAdapter<String>
to ArrayAdapter<VoucherObj>
Update
as you say on comment you got NPE on tvName.setText(items.get(position).toString());
so 2 possibility is exists:
1- tvName
is null, for handling that check tvName
is exist on voucher_details
.
2- items
is null,
Update 1
you use items.get(position).toString()
and i think you have one empty object to your list.
so you have 2 way,
1- override toString
method on VoucherObj
class.
2 - change items.get(position).toString()
to one other value
Upvotes: 4
Reputation: 18923
Change this from
public ClaimVoucherAdapter(Context context, int textViewResourceId, ArrayList<VoucherObj> myList) {
super(context, textViewResourceId);
this.items = myList;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
to
public ClaimVoucherAdapter(Context context, int textViewResourceId, ArrayList<VoucherObj> myList) {
super(context, textViewResourceId,myList);
this.items = myList;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
Upvotes: 1