如何从android中的linearlayout访问子视图项?

时间:2016-04-12 19:05:14

标签: android web-services android-layout android-linearlayout

我有自定义项目的线性布局,如图片:

enter image description here

由网络服务填写,我如何获得“删除”textview和单选按钮以执行onclickonchecked听众的访问权限?

我的活动开始加载webservice的数据,并将来自webservice的每个响应的线性布局“n”子项放入,我想根据listview中子项的位置启用click和checked listeners

日Thnx。

适配器。

public class NewPaymentsAdapter extends ArrayAdapter<Payments> {

    private ViewHolder holder;

    public NewPaymentsAdapter(Context context, ArrayList<Payments> newpayments) {
        super(context, 0, newpayments);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Payments payments = getItem(position);
        if (convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.new_payments_item, parent, false);
            holder = new ViewHolder();
            holder.rbPayments = (RadioButton) convertView.findViewById(R.id.rbPayments);
            holder.tvPaymentsDelete = (MyTitleFontTextView) convertView.findViewById(R.id.tvPaymentsDelete);
            holder.tvPaymentsTitle = (MyTitleFontTextView) convertView.findViewById(R.id.tvPaymentsTitle);
            holder.tvPaymentsDesc = (MyFontTextView) convertView.findViewById(R.id.tvPaymentsDesc);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvPaymentsTitle.setText("Tarjeta");
        holder.tvPaymentsDesc.setText("**** **** **** " + payments.getLastfour());
        if (Boolean.parseBoolean(payments.getIsdefault()) == true){
            holder.rbPayments.setChecked(true);
        } else {
            holder.rbPayments.setChecked(false);
        }
        return convertView;
    }

    private class ViewHolder {
        RadioButton rbPayments;
        MyTitleFontTextView tvPaymentsDelete, tvPaymentsTitle;
        MyFontTextView tvPaymentsDesc;
    }

分析器

public ArrayList<Payments> parseNewPayments(String response, ArrayList<Payments> list) {

        try {
            JSONObject jsonObject = new JSONObject(response);
            if (jsonObject.getBoolean(KEY_SUCCESS)) {
                JSONArray jsonArray = jsonObject.getJSONArray("payments");
                if (jsonArray.length() > 0) {
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject object = jsonArray.getJSONObject(i);

                        Payments newpayments = new Payments();

                        newpayments.setId(object.getInt("id"));
                        newpayments.setLastfour(object.getString("last_four"));
                        newpayments.setIsdefault(object.getString("is_default"));
                        list.add(newpayments);
                    }
                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

模型

public class Payments {

    private int id;
    private String isdefault, lastfour;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLastfour() {
        return lastfour;
    }

    public void setLastfour(String lastfour) {
        this.lastfour = lastfour;
    }

    public String getIsdefault() {
        return isdefault;
    }

    public void setIsdefault(String isdefault) {
        this.isdefault = isdefault;
    }

   }

活动

this.pContent.parseNewPayments(response, newPaymentsList);
                    newPaymentsAdapter.notifyDataSetChanged();
                    adaptercount = Integer.valueOf(newPaymentsAdapter.getCount());
                    for (int i = 0; i < adaptercount; i++) {
                        View item = newPaymentsAdapter.getView(i, null, null);
                        layNewPayments.addView(item);
                    }

这是我的代码,活动正确显示来自WS的数据,现在我想用textview和单选按钮做一个“删除”和“选择默认”方法,传递参数

Card card = new Card(),
id = card.getid()

并执行http POST,但我无法访问子textview或单选按钮来执行此操作。

1 个答案:

答案 0 :(得分:0)

您可以使用View类的findViewById方法。

例如,

LinearLayout ll = findViewById(R.id.parentLayout);
int childcount = ll.getChildCount();
for (int i=0; i < childcount; i++){
    LinearLayout l = (LinearLayout) ll.getChildAt(i);
    TextView delete = (TextView) l.findViewById(R.id.delete);

    delete.setOnClickListener(...);
}