如何在子列表部分添加单选按钮,并获取所有选定的单选按钮的值onClick SUBMIT按钮

时间:2015-05-10 14:25:20

标签: android

这里我有一个multiexxandable listview,我想在子部分添加单选按钮,并在点击提交按钮(如表单POST)时获取所有选定单选按钮的值。有人可以用代码帮我吗

工作代码:

MainActivity.java

package com.androidhub4you.multilevellistview;

import java.util.ArrayList;

import com.androidhub4you.multilevellistview.Product.SubCategory;
import com.androidhub4you.multilevellistview.Product.SubCategory.ItemList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
 * 
 * @author manish
 *
 */
public class MainActivity extends Activity {

    private ArrayList<Product>pProductArrayList;
    private ArrayList<SubCategory>pSubItemArrayList;
    private ArrayList<SubCategory>pSubItemArrayList2;
    private LinearLayout mLinearListView;
    boolean isFirstViewClick=false;
    boolean isSecondViewClick=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLinearListView = (LinearLayout) findViewById(R.id.linear_listview);

        /**
         * 
         */

        ArrayList<ItemList> mItemListArray=new ArrayList<ItemList>();
        mItemListArray.add(new ItemList("Red", "20"));
        mItemListArray.add(new ItemList("Blue", "50"));
        mItemListArray.add(new ItemList("Red", "20"));
        mItemListArray.add(new ItemList("Blue", "50"));

        ArrayList<ItemList> mItemListArray2=new ArrayList<ItemList>();
        mItemListArray2.add(new ItemList("Pant", "2000"));
        mItemListArray2.add(new ItemList("Shirt", "1000"));
        mItemListArray2.add(new ItemList("Pant", "2000"));
        mItemListArray2.add(new ItemList("Shirt", "1000"));
        mItemListArray2.add(new ItemList("Pant", "2000"));
        mItemListArray2.add(new ItemList("Shirt", "1000"));


        /**
         * 
         */
        pSubItemArrayList=new ArrayList<SubCategory>();
        pSubItemArrayList2=new ArrayList<SubCategory>();
        pSubItemArrayList.add(new SubCategory("Color", mItemListArray));
        pSubItemArrayList2.add(new SubCategory("Cloths", mItemListArray2));
        pSubItemArrayList.add(new SubCategory("Color", mItemListArray));
        pSubItemArrayList2.add(new SubCategory("Cloths", mItemListArray2));
        /**
         * 
         */

        pProductArrayList=new ArrayList<Product>();
        pProductArrayList.add(new Product("Emotions", pSubItemArrayList));
        pProductArrayList.add(new Product("Garments", pSubItemArrayList2));


        /***
         * adding item into listview
         */
        for (int i = 0; i < pProductArrayList.size(); i++) {

            LayoutInflater inflater = null;
            inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View mLinearView = inflater.inflate(R.layout.row_first, null);

            final TextView mProductName = (TextView) mLinearView.findViewById(R.id.textViewName);
            final RelativeLayout mLinearFirstArrow=(RelativeLayout)mLinearView.findViewById(R.id.linearFirst);
            final ImageView mImageArrowFirst=(ImageView)mLinearView.findViewById(R.id.imageFirstArrow);
            final LinearLayout mLinearScrollSecond=(LinearLayout)mLinearView.findViewById(R.id.linear_scroll);

            if(isFirstViewClick==false){
            mLinearScrollSecond.setVisibility(View.GONE);
            mImageArrowFirst.setBackgroundResource(R.drawable.arw_lt);
            }
            else{
                mLinearScrollSecond.setVisibility(View.VISIBLE);
                mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
            }

            mLinearFirstArrow.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    if(isFirstViewClick==false){
                        isFirstViewClick=true;
                        mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
                        mLinearScrollSecond.setVisibility(View.VISIBLE);

                    }else{
                        isFirstViewClick=false;
                        mImageArrowFirst.setBackgroundResource(R.drawable.arw_lt);
                        mLinearScrollSecond.setVisibility(View.GONE);   
                    }
                    return false;
                } 
            });


            final String name = pProductArrayList.get(i).getpName();
            mProductName.setText(name);

            /**
             * 
             */
            for (int j = 0; j < pProductArrayList.get(i).getmSubCategoryList().size(); j++) {

                LayoutInflater inflater2 = null;
                inflater2 = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View mLinearView2 = inflater2.inflate(R.layout.row_second, null);

                TextView mSubItemName = (TextView) mLinearView2.findViewById(R.id.textViewTitle);
                final RelativeLayout mLinearSecondArrow=(RelativeLayout)mLinearView2.findViewById(R.id.linearSecond);
                final ImageView mImageArrowSecond=(ImageView)mLinearView2.findViewById(R.id.imageSecondArrow);
                final LinearLayout mLinearScrollThird=(LinearLayout)mLinearView2.findViewById(R.id.linear_scroll_third);

                if(isSecondViewClick==false){
                    mLinearScrollThird.setVisibility(View.GONE);
                    mImageArrowSecond.setBackgroundResource(R.drawable.arw_lt);
                    }
                    else{
                        mLinearScrollThird.setVisibility(View.VISIBLE);
                        mImageArrowSecond.setBackgroundResource(R.drawable.arw_down);
                    }

                mLinearSecondArrow.setOnTouchListener(new OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {

                            if(isSecondViewClick==false){
                                isSecondViewClick=true;
                                mImageArrowSecond.setBackgroundResource(R.drawable.arw_down);
                                mLinearScrollThird.setVisibility(View.VISIBLE);

                            }else{
                                isSecondViewClick=false;
                                mImageArrowSecond.setBackgroundResource(R.drawable.arw_lt);
                                mLinearScrollThird.setVisibility(View.GONE);    
                            }
                            return false;
                        } 
                    });


                final String catName = pProductArrayList.get(i).getmSubCategoryList().get(j).getpSubCatName();
                mSubItemName.setText(catName);
                /**
                 * 
                 */
                  for (int k = 0; k < pProductArrayList.get(i).getmSubCategoryList().get(j).getmItemListArray().size(); k++) {

                        LayoutInflater inflater3 = null;
                        inflater3 = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        View mLinearView3 = inflater3.inflate(R.layout.row_third, null);

                        TextView mItemName = (TextView) mLinearView3.findViewById(R.id.textViewItemName);
                        TextView mItemPrice = (TextView) mLinearView3.findViewById(R.id.textViewItemPrice);
                        final String itemName = pProductArrayList.get(i).getmSubCategoryList().get(j).getmItemListArray().get(k).getItemName();
                        final String itemPrice = pProductArrayList.get(i).getmSubCategoryList().get(j).getmItemListArray().get(k).getItemPrice();
                        mItemName.setText(itemName);
                        mItemPrice.setText(itemPrice);

                        mLinearScrollThird.addView(mLinearView3);
                  }

                mLinearScrollSecond.addView(mLinearView2);

            }

            mLinearListView.addView(mLinearView);
        }       
    }

}

Product.java

package com.androidhub4you.multilevellistview;

import java.util.ArrayList;

/**
 * 
 * first level item
 * 
 */
public class Product {

    private String pName;

    private ArrayList<SubCategory> mSubCategoryList;

    public Product(String pName, ArrayList<SubCategory> mSubCategoryList) {
        super();
        this.pName = pName;
        this.mSubCategoryList = mSubCategoryList;
    }

    public String getpName() {
        return pName;
    }

    public void setpName(String pName) {
        this.pName = pName;
    }

    public ArrayList<SubCategory> getmSubCategoryList() {
        return mSubCategoryList;
    }

    public void setmSubCategoryList(ArrayList<SubCategory> mSubCategoryList) {
        this.mSubCategoryList = mSubCategoryList;
    }

    /**
     * 
     * second level item
     * 
     */

    public static class SubCategory {

        private String pSubCatName;
        private ArrayList<ItemList> mItemListArray;

        public SubCategory(String pSubCatName,
                ArrayList<ItemList> mItemListArray) {
            super();
            this.pSubCatName = pSubCatName;
            this.mItemListArray = mItemListArray;
        }

        public String getpSubCatName() {
            return pSubCatName;
        }

        public void setpSubCatName(String pSubCatName) {
            this.pSubCatName = pSubCatName;
        }

        public ArrayList<ItemList> getmItemListArray() {
            return mItemListArray;
        }

        public void setmItemListArray(ArrayList<ItemList> mItemListArray) {
            this.mItemListArray = mItemListArray;
        }

        /**
         * 
         * third level item
         * 
         */
        public static class ItemList {

            private String itemName;
            private String itemPrice;

            public ItemList(String itemName, String itemPrice) {
                super();
                this.itemName = itemName;
                this.itemPrice = itemPrice;
            }

            public String getItemName() {
                return itemName;
            }

            public void setItemName(String itemName) {
                this.itemName = itemName;
            }

            public String getItemPrice() {
                return itemPrice;
            }

            public void setItemPrice(String itemPrice) {
                this.itemPrice = itemPrice;
            }

        }

    }

}

activity_main.xml中

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linear_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

</ScrollView>

row_first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/linearFirst"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textViewName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:text="TextView"
            android:textColor="@android:color/white"
            android:textSize="22sp" />

        <ImageView
            android:id="@+id/imageFirstArrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="5dp" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/linear_scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:background="@android:color/white"
        android:orientation="vertical" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/white" />

</LinearLayout>

row_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/linearSecond"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/background_dark"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textViewTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:text="TextView"
            android:textColor="@android:color/white"
            android:textSize="20sp" />

        <ImageView
            android:id="@+id/imageSecondArrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="5dp" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/linear_scroll_third"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:background="@android:color/white"
        android:orientation="vertical" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/white" />

</LinearLayout>

row_third.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewItemName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/background_light"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/textViewItemPrice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/background_light"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我总是使用这个解决方案,希望它对你有所帮助!

MainActivity应扩展FragmentActivity并具有以下布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/> 
</RelativeLayout>

比我以这种方式将MyFragment添加到MainActivity的容器中:

getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.container, new MyFragment())
                .commit();

MyFragment将扩展ListFragment。现在你必须创建一个这样的customListAdapter:

public class CustomListAdapter extends BaseAdapter {
private LayoutInflater inflater;
private static final int TYPE_ITEM = 0;
private static final int MAX_COUNT = 1;

private ArrayList mData = new ArrayList();

public CustomListHomeAdapter(Context context) {
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public void addItem(final HomeModel item) {
    mData.add(item);
    notifyDataSetChanged();
}


@Override
public int getItemViewType(int position) {
        return TYPE_ITEM;      
}

@Override
public int getViewTypeCount() {
    return MAX_COUNT;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = null;
    int type = getItemViewType(position);

    if(convertView == null) {
        //initialize the view Holder
        viewHolder = new ViewHolder();
        switch (type) {
            case TYPE_ITEM:
                convertView = inflater.inflate(R.layout.list_row_home, null);   //HERE YOU CAN INFLATE YOUR CUSTOM LAYOUT FOR A SINGLE ROW

                viewHolder.rb = (RadioButton) convertView.findViewById(R.id.radioButton);

                break;
        }

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    HomeModel model;
    switch (type) {
        case TYPE_ITEM:
             viewHolder.rb.setChecked(true);
        break;

    }
    return convertView;
}

private static class ViewHolder {
    RadioButton rb;
}
}

最后在你的片段中:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //initialize all home items
    mAdapter = new CustomListHomeAdapter(getActivity());

    Resources resources = getResources();

    mAdapter.addItem(new HomeModel(getString(R.string.produttori_label), resources.getDrawable(R.drawable.produttori)));


    //initialize and set list adapter
    setListAdapter(mAdapter);

}