在微调器中选择后触发方法

时间:2017-01-01 22:46:13

标签: android listview spinner

首先祝大家新年快乐!!其次,我想知道是否有人可以帮助我解决这个问题。我有一个ListActivity,其中每一行都有一个微调器来选择一个批号。

<?xml version="1.0" encoding="utf-8"?>
<com.picking.utils.RelativeLayout
android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayoutPickingRow"                                                                      
android:layout_width="match_parent"
android:layout_height="70dp"                                  
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
>

<LinearLayout
    android:id="@+id/linearLayoutPickingRow"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:paddingLeft="10dp"
    >

 //TextViews and other things

    <Spinner
        android:id="@+id/spinnerLote"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/spinner_bg"
        android:focusable="false"
        android:spinnerMode="dialog"
        android:visibility="visible"

        />

</LinearLayout>

我想要实现的目标是&#34;模拟&#34;选择bacthnumber时的onListItemClick方法

protected void onListItemClick(ListView l, View view, int position, long id)

我还有一个自定义微调器适配器和一个自定义适配器,用于具有固定器模式的列表

package com.picking.adapters;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import  com.picking.POJOS.LotesAbiertos;
import  com.pickingR;
import java.util.ArrayList;

public class SpinnerCustomAdapter<L> extends ArrayAdapter<LotesAbiertos> {
    private ArrayList<LotesAbiertos> list;
    private Activity context;
    Resources resources;
    public SpinnerCustomAdapter(Activity activity, int resource, int textViewResourceId, ArrayList<LotesAbiertos> list) {
        super(activity, resource, textViewResourceId, list);
        this.list = list;
        this.context = activity;
        resources = activity.getResources();
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View row = inflater.inflate(R.layout.custom_spinner, parent, false);
        LotesAbiertos item = list.get(position);
        TextView tvLote = (TextView) row.findViewById(android.R.id.text1);
        if (item != null) {   // Parse the data from each object and set it.
            if (item.status.equalsIgnoreCase("2")) {
                tvLote.setText("SIN LOTES");
            } else {
                tvLote.setText(item.loteFormateado);
            }
        }
        return row;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {   // This view starts when we click the spinner.
        final int positionRow = position;
        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            row = inflater.inflate(R.layout.custom_spinner_drop, parent, false);
        }
        TextView tvLote = (TextView) row.findViewById(android.R.id.text1);
        TextView tvAcceso = (TextView) row.findViewById(android.R.id.text2);
        LotesAbiertos item = list.get(position);
        if (item != null) {   // Parse the data from each object and set it.
            if (position == 0) {
                tvLote.setText("Lotes");
                row.setBackgroundColor(resources.getColor(R.color.green_ligth));
            } else {
                if (item.status.equalsIgnoreCase("2")) {
                    tvLote.setText("SIN LOTES");
                } else {
                    tvLote.setText(item.loteFormateado.substring(0, 5));
                    Spannable secondWord = new SpannableString(item.loteFormateado.substring(item.loteFormateado.length() - 3));
                    secondWord.setSpan(new StyleSpan(Typeface.BOLD), 0, secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    tvLote.append(secondWord);
                }
                row.setBackgroundColor(resources.getColor(R.color.white));
            }
            if (item.status.equalsIgnoreCase("0") || item.status.equalsIgnoreCase("2")) {
                tvLote.setTextColor(resources.getColor(R.color.green_dark));
                tvAcceso.setVisibility(View.INVISIBLE);
            } else {
                tvLote.setTextColor(resources.getColor(R.color.dark_grey));
                tvAcceso.setVisibility(View.VISIBLE);
            }
        }
        return row;
    }
}

考虑到我在不同的包中有适配器和活动,我不能调用该方法,因为它受到保护,但我可以有这样的方法。

public void fireOnListItemClick(View view, int position, long id) {
    onListItemClick(getListView(), view, position, id);
}

我可以从spinnner适配器调用此方法,但问题是如何获得

view, position, id

listview行的参数将它们传递给方法? Shoud我在spinner适配器中有这样的setOnClickListener吗?

 @Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {   

    /*..........*/

    row.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            /*Call fireOnListItemClick*/
        }
    });
    return row;
}

提前感谢您的时间

1 个答案:

答案 0 :(得分:1)

查看OnItemSelectedListener

Example usage

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            tv.setText("Spinner selected : ");
            tv.setText(tv.getText() + parent.getItemAtPosition(position).toString());
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            //Another interface callback
        }
    });