Android如何在listview中使用单选按钮管理微调器?

时间:2014-03-24 06:04:13

标签: android listview radio-button spinner

我的问题是,当我点击单选按钮时,它会在任何位置随机改变微调器值。这里的帮助是我的适配器类..

public class ManageAdapter extends BaseAdapter {
    ArrayList<String> _manageArrayList = null;
    private Context _Context = null;
    private Integer selectedPosition = 0;
    String List1[]=new String[]{"In use","Lost" ,"Stolen" , "For sale"};
    private int flag = 0;


    public ManageAdapter(ArrayList<String> _manageArrayList, Context _Context) {
        super();
        this._manageArrayList = _manageArrayList;
        this._Context = _Context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return _manageArrayList != null? _manageArrayList.size():0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder
    {
        public TextView itemTextView;
        public RadioButton myitemButton;
        public Spinner inUseSpinner;
        public RelativeLayout managelayout;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        final ViewHolder holder;
        LayoutInflater inflater = (LayoutInflater)_Context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(v == null)
        {
            holder = new ViewHolder();
            v = inflater.inflate(R.layout.manage_single_row, null,false);
            holder.managelayout = (RelativeLayout)v.findViewById(R.id.single_row_layout);
            holder.itemTextView = (TextView)v.findViewById(R.id.item_name_txt);
            holder.myitemButton = (RadioButton)v.findViewById(R.id.manage_radio_buton);
            holder.inUseSpinner = (Spinner)v.findViewById(R.id.Status_spinner);
            v.setTag(holder);

            ArrayAdapter<String> Adapter = new ArrayAdapter<String>(_Context,R.layout.spinner_color,List1);
            Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
            //Setting the ArrayAdapter data on the Spinner  
            holder.inUseSpinner.setAdapter(Adapter);
        }else {
            holder = (ViewHolder) v.getTag();
        }
        holder.itemTextView.setText(_manageArrayList.get(position));

        holder.inUseSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                // int getPosition = (Integer) arg0.getTag();
                //  Toast.makeText(_Context,List1[position] ,Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

        if(flag  == 0)
        {
            flag = 1;
            holder.myitemButton.setChecked(false);
            holder.myitemButton.setTag(position);
        }else {
            holder.myitemButton.setChecked(position == selectedPosition);
            holder.myitemButton.setTag(position);
        }
        holder.myitemButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                selectedPosition = (Integer)v.getTag();
                notifyDataSetInvalidated();
            }
        });

        return v;
    }

}

提前致谢

super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage_item);

        _backButton = (Button)findViewById(R.id.back);
        _backButton.setOnClickListener(this);

        _headerTextView = (TextView)findViewById(R.id.header_TextView);
        _headerTextView.setText("My Items");

        _uploadButton = (ImageButton)findViewById(R.id.Upload_file_button);
        _uploadButton.setOnClickListener(this);

        _addmanullyButton = (ImageButton)findViewById(R.id.add_item_manually);
        _addmanullyButton.setOnClickListener(this);

        _addphoneButton = (ImageButton)findViewById(R.id.auto_add_phone);
        _addphoneButton.setOnClickListener(this);

        _modifyButton = (ImageButton)findViewById(R.id.modify_button);
        _modifyButton.setOnClickListener(this);

        _transferButton = (ImageButton)findViewById(R.id.transfer_ownership);
        _transferButton.setOnClickListener(this);

        _hideenButton = (Button)findViewById(R.id.hideen_button);
        _hideenButton.setOnClickListener(this);


        _manageListView = (ListView)findViewById(R.id.manage_listview);
        prepareList();
        _adapter = new ManageAdapter(listItem, this);
        _manageListView.setAdapter(_adapter);
    }
    public void prepareList() {
        listItem = new ArrayList<String>();

        listItem.add("Android smart");
        listItem.add("sony");
        listItem.add("samsung");
        listItem.add("micromax");
        listItem.add("tablet");
    }
    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
        case R.id.Upload_file_button:
            intent = new Intent(ManageItems.this, UploadFileActivity.class);
            startActivity(intent);
            break;
        case R.id.modify_button:
            intent = new Intent(ManageItems.this, ModifyActivity.class);
            startActivity(intent);
            break;
        case R.id.transfer_ownership:
            intent = new Intent(ManageItems.this, TransferOwnerShip.class);
            startActivity(intent);
            break;
        case R.id.add_item_manually:
            intent = new Intent(ManageItems.this, AddManuallyActivity.class);
            startActivity(intent);
            break;
        case R.id.auto_add_phone:
            intent = new Intent(ManageItems.this, AutoAddActivity.class);
            startActivity(intent);
            break;
        case R.id.back:
            intent = new Intent(ManageItems.this, MenuActivtiy.class);
            startActivity(intent);
            break;
        case R.id.hideen_button:
            intent = new Intent(ManageItems.this, MenuActivtiy.class);
            startActivity(intent);
            break;
        }   

1 个答案:

答案 0 :(得分:0)

谢谢大家,但我得到了我的问题的解决方案..

 @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v = convertView;
            final ViewHolder holder;
            MyItemListDataHolder myItemListDataHolder = _manageArrayList.get(position);
            LayoutInflater inflater = (LayoutInflater)_Context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if(v == null)
            {
                holder = new ViewHolder();
                v = inflater.inflate(R.layout.manage_single_row, null,false);
                holder.managelayout = (RelativeLayout)v.findViewById(R.id.single_row_layout);
                holder.itemTextView = (TextView)v.findViewById(R.id.item_name_txt);
                holder.myitemButton = (RadioButton)v.findViewById(R.id.manage_radio_buton);
                holder.inUseSpinner = (Spinner)v.findViewById(R.id.Status_spinner);
                v.setTag(holder);

            }else {
                holder = (ViewHolder) v.getTag();
            }

            holder.itemTextView.setText(myItemListDataHolder.get_itemName());

            Adapter = new ArrayAdapter<String>(_Context,R.layout.spinner_color,List1);
            Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
            //Setting the ArrayAdapter data on the Spinner  
            holder.inUseSpinner.setAdapter(Adapter);
            if(myItemListDataHolder.get_selectedItem() > 0){
                holder.inUseSpinner.setSelection(myItemListDataHolder.get_selectedItem());
            }else{
                holder.inUseSpinner.setSelection(0);
            }
            holder.inUseSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                private Intent intent;

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int position1, long arg3) {
                    _manageArrayList.get(position).set_selectedItem(position1);
                    //  notifyDataSetChanged();
                    String status=holder.inUseSpinner.getSelectedItem().toString();
                    if(status.equalsIgnoreCase("Lost"))
                    {
                        intent = new Intent (_Context, LostItemActivity.class);
                        startActivity(intent);
                    }else if (status.equalsIgnoreCase("Stolen")) {
                        intent = new Intent (_Context, StolenItemActivity.class);
                        startActivity(intent);
                    }else if (status.equalsIgnoreCase("Stolen")) {
                        intent = new Intent (_Context, LostFoundActivity.class);
                        startActivity(intent);
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {

                }
            });

            if(flag  == 0)
            {
                flag = 1;
                holder.myitemButton.setChecked(false);
            }else {
                holder.myitemButton.setChecked(position == selectedPosition);
            }
            holder.myitemButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    selectedPosition = position;
                    notifyDataSetChanged();
                    //_adapter.notifyDataSetChanged();
                }
            });

            return v;
        }
    }