Spinner setOnItemSelectedListener不调用onItemSelected

时间:2015-02-01 12:49:58

标签: android spinner onitemselectedlistener

在过去的几天里,我尝试在表格的某些单元格中插入一些微调器,其中顶部和左侧的标题都是固定的,只有其他单元格可以作为一个单元滚动。为了实现这一点,我找到InQBarna/TableFixHeaders并更改了SampleTableAdapter.java以在某些单元格中显示微调器(不在标题内)。

为了处理微调器的选择,我像往常一样为每个微调器添加一个OnItemSelectedListener,代码如下:

    final Spinner aufgabe = (Spinner) view.findViewById(android.R.id.custom);
    ArrayAdapter<CharSequence> adapter = new ArrayAdapter(getContext(), android.R.layout.simple_spinner_item, ArbeitAnzeigen.allePunkte.get(column - 2));
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    aufgabe.setAdapter(adapter);

    aufgabe.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView parent, View view, int position, long id) {
            Log.d(TAG,"onItemSelected()");
        }

        @Override
        public void onNothingSelected(AdapterView parent) {
            Log.d(TAG,"onNothingSelected()");
        }
    });

但是所有的OnItemSelectedListener都不起作用。一旦创建了视图,就会调用它们,但是当选择一个项目(不仅是预先选择的)时,没有反应。

以下是我的应用程序上下文的一些信息:带有微调器的表放在一个片段中。此片段是tabhost的另一个片段的一部分。我的应用程序有一个导航抽屉来选择想要的片段。主要活动属于ActionBarActivity类型。

我检查了上面代码的许多变体,但我找不到解决方案。有人可以帮助我吗?我会很高兴有一个提示,我可以搜索并尝试。

是否有可能另一位听众正在接收该事件?如果可以的话,我该如何找出哪一个?

以下是整个代码:

MainActivity.java

public class MainActivity extends Activity {

public static String[] tabellenueberschrift;
public static String[][] tabellenzeilen;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView ueberschrift = (TextView) findViewById(R.id.ueberschrift);
    ueberschrift.setText("Beispieltabelle");

    tabellenueberschrift = new String[6];
    for (int i = 0; i < 6; i++) {
        tabellenueberschrift[i] = "Header " + Integer.toString(i);
    }

    tabellenzeilen = new String[3][6];
    for (int i = 0; i < 3; i++) {
        tabellenzeilen[i][0] = "Zeile " + Integer.toString(i);
        for (int j = 1; j < 6; j++) {
            tabellenzeilen[i][j] = "3";
        }
    }

    TableFixHeaders tableFixHeaders = (TableFixHeaders) findViewById(R.id.table);
    tableFixHeaders.setAdapter(new TableTestAdapter(this));
}

TableTestAdapter.java:

public class TableTestAdapter extends SampleTableAdapter {

private static final String TAG = TableTestAdapter.class.getSimpleName();

private final static int WIDTH_DIP = 110;
private final static int HEIGHT_DIP = 48;

private final int width;
private final int height;
public TableTestAdapter(Context context) {
    super(context);
    Resources resources = context.getResources();
    width = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, WIDTH_DIP, resources.getDisplayMetrics()));
    height = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, HEIGHT_DIP, resources.getDisplayMetrics()));
}
@Override
public int getRowCount() {
    return 3;
}
@Override
public int getColumnCount() {
    return 5;
}
@Override
public int getWidth(int column) {
    return width;
}
@Override
public int getHeight(int row) {
    return height;
}
@Override
public String getCellString(int row, int column) {
    if (row == -1) {
        return MainActivity.tabellenueberschrift[column + 1];
    }
    else {
        return MainActivity.tabellenzeilen[row][column + 1];
    }
}
@Override
public int getLayoutResource(int row, int column) {
    final int layoutResource;
    switch (getItemViewType(row, column)) {
        case 0:
            layoutResource = R.layout.tabelle_header;
            break;
        case 1:
            layoutResource = R.layout.tabelle;
            break;
        case 2:
            layoutResource = R.layout.tabelle_spinner;
            break;
        default:
            throw new RuntimeException("wtf?");
    }
    return layoutResource;
}
@Override
public int getItemViewType(int row, int column) {
    if (row < 0) {
        return 0;
    } else {
        if (column >= 0) {
            return 2;
        }
        else {
            return 1;
        }
    }
}
@Override
public int getViewTypeCount() {
    return 3;
}

SampleTableAdapter.java:

public abstract class SampleTableAdapter extends BaseTableAdapter {
private static final String TAG = SampleTableAdapter.class.getSimpleName();
private final Context context;
private final LayoutInflater inflater;
/**
 * Constructor
 *
 * @param context
 * The current context.
 */
public SampleTableAdapter(Context context) {
    this.context = context;
    inflater = LayoutInflater.from(context);
}
/**
 * Returns the context associated with this array adapter. The context is
 * used to create views from the resource passed to the constructor.
 *
 * @return The Context associated with this adapter.
 */
public Context getContext() {
    return context;
}
/**
 * Quick access to the LayoutInflater instance that this Adapter retreived
 * from its Context.
 *
 * @return The shared LayoutInflater.
 */
public LayoutInflater getInflater() {
    return inflater;
}
@Override
public View getView(int row, int column, View converView, ViewGroup parent) {
    if (converView == null) {
        converView = inflater.inflate(getLayoutResource(row, column), parent, false);
    }
    if (getItemViewType(row, column) == 2) {
        final Spinner spinner = (Spinner) converView.findViewById(android.R.id.custom);

        ArrayAdapter<CharSequence> adapter = new ArrayAdapter(getContext(), android.R.layout.simple_spinner_item, getItems(Integer.valueOf(MainActivity.tabellenzeilen[row][column + 1])));
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                Log.d(TAG, "onItemSelected()");
            }

            @Override
            public void onNothingSelected(AdapterView parent) {
                Log.d(TAG, "onNothingSelected()");
            }
        });

    }
    else {
        setText(converView, getCellString(row, column));
    }
    return converView;
}
/**
 * Sets the text to the view.
 *
 * @param view
 * @param text
 */
private void setText(View view, String text) {
    ((TextView) view.findViewById(android.R.id.text1)).setText(text);
}
/**
 * @param row
 * the title of the row of this header. If the column is -1
 * returns the title of the row header.
 * @param column
 * the title of the column of this header. If the column is -1
 * returns the title of the column header.
 * @return the string for the cell [row, column]
 */
public abstract String getCellString(int row, int column);
public abstract int getLayoutResource(int row, int column);

private ArrayList<String> getItems(int anzahl) {
    ArrayList<String> rueckgabe = new ArrayList<String>();

    for (int i = 0; i < anzahl; i++) {
        rueckgabe.add("Auswahl " + Integer.toString(i+1));
    }

    return rueckgabe;
}

1 个答案:

答案 0 :(得分:0)

你不应该覆盖getView。相反,您应该在onResume中运行您的代码,以确保每次活动到达时都能正确初始化侦听器。