将字符串getIntent传递给另一个Activity中的Listview

时间:2014-03-07 08:29:01

标签: android adapter

我想传递此代码标记生成器以在下一个活动中填充列表视图。我使用getintent()和自定义适配器进行下一个活动。有人告诉我,我应该定制适配器。

Intent intent = new Intent(ClaimVoucherActivity.this,ClaimVoucherDetailsActivity.class);
                        intent.putExtra("customerID", customerID);
                        intent.putExtra("type", type);
                        intent.putExtra("name", name);
                        intent.putExtra("email", email);
                        intent.putExtra("voucher", voucher);
                        intent.putExtra("branch", branch);
                        intent.putExtra("issued", issued);
                        intent.putExtra("expiration", expiration);
                        intent.putExtra("status", status);
                        intent.putExtra("vouchername", vouchername);
                        intent.putExtra("employeeid", employeeid);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);

这是Custome适配器

public class ClaimVoucherAdapter extends ArrayAdapter<ClaimVoucherActivity> {

private ArrayList<ClaimVoucherActivity> items;
private LayoutInflater vi;

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

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

        ClaimVoucherActivity o = items.get(position);
        if (o != 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);


        }
        return v;
}
}

ClaimVoucherActivity.java

      Intent intent = this.getIntent();     
    String selected = 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();
        // Configure the listview

    ListView lstitems = (ListView) this.findViewById(R.id.listView1);       
    setListAdapter(this.m_adapter);

2 个答案:

答案 0 :(得分:1)

您只需添加列表

中的所有数据即可
List<String> myList = new ArrayList<String>();
myList.add(selected);
myList.add(sType);
myList.add(sName);
myList.add(sEmail);
myList.add(sVoucher); //Add same for other which is remaining.

现在将此列表传递给您的适配器

ClaimVoucherAdapter m_adapter = new ClaimVoucherAdapter(YourActivityName.this , R.layout.urlayoutName ,myList);

现在在这里

 public class ClaimVoucherAdapter extends ArrayAdapter<ClaimVoucherActivity> {

 private ArrayList<String> items;
 private LayoutInflater vi;

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

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

    //ClaimVoucherActivity o = items.get(position);
   // if (o != 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);

        tvEmail.setText(items.get(position).toString());
        // Now same do for all others Strings.
    //}
    return v;
}
}

答案 1 :(得分:1)

您可以在下一个活动中获取这些数据。

Intent i = getIntent();
customerID= i.getStringExtra("customerID");
type= i.getStringExtra("type");
name= i.getStringExtra("name");
...............
...............

然后创建列表并将数据添加到列表中。在创建适配器并将列表设置为适配器之后。

添加到列表

ArrayList<String> list = new ArrayList<String>();
list.add(customerID);
list.add(type);
..................
..........

然后添加到适配器

ArrayAdapter<String> adapter = new ArrayAdapter<String>(YourActivity.this,android.R.layout.simple_list_item_1, list);
list.setAdapter(adapter);

此处列表是您的ListView