循环浏览android中的视图

时间:2014-12-11 15:25:46

标签: android

我正在迭代我动态创建的视图

int childCnt=dynAddrId.getChildCount();

for(int i=0;i<childCnt;i++){
    Spinner ref=(Spinner) dynAddrId.findViewWithTag(i).findViewWithTag("spinnerTag");
    ref.getSelectedItem().toString();       
}

ref.getSelectedItem().toString()的值为{id=1, proof=driving licence}

注意:证明的下一循环涉及sincedynamic内容我可能会PAN CARD

如何获取值driving licence并将其存储在字符串


修改

用于播放SPINNER的代码

/* Create a Spinner .*/
        spinnerView=new Spinner(getActivity());
        spinnerView.setTag("spinnerTag");
        spinnerView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,1f));
        AdptAddressProof adapter=new AdptAddressProof(addressProofSpinnerData, getActivity());
        spinnerView.setAdapter(adapter);
        //SET THE SPINNER ID FOR DYNAMICALLY CREATED OBJECT

public class AdptAddressProof extends BaseAdapter {
    ArrayList<HashMap<String, String>> categorySpinnerData;
    Context context;
    //ArrayList<ListObject> objects;

    public AdptAddressProof(ArrayList<HashMap<String, String>> _categorySpinnerData, Context _context) {
        super();
        context=_context;
        categorySpinnerData=_categorySpinnerData;
    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        HashMap<String, String> mapData=categorySpinnerData.get(position);
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.adpt_categories_spinner, null);
        }

        TextView txtCategoryNameId = (TextView) convertView.findViewById(R.id.txtCategoryNameId);

        txtCategoryNameId.setText(mapData.get("proof"));
        txtCategoryNameId.setTag(mapData.get("id"));

        return convertView;
    }
}

1 个答案:

答案 0 :(得分:2)

在您的情况下getSelectedItem会返回HashMap<String, String>。要检索证明内容,您只需执行以下操作:

HashMap<String, String> item = (HashMap<String, String>) ref.getSelectedItem(); 
String value = item.get("proof");