アプリの実行中にAndroidSpinnerがクリックできない、または機能しない

ローザ

私はプログラミングに比較的慣れておらず、最近Androidを使い始めました。私はスピナーで抱えているこの問題について一日中頭を悩ませてきました。私はいくつかの同様の質問を読み、いくつかの解決策と例を見つけましたが、どれも私にはうまくいきません:(

package com.deitel.welcome;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;



public class Preferences extends Activity {

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_preferences);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.preferences, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_profile) {
        Intent intent = new Intent(Preferences.this, Profile.class);
        startActivity(intent);
    }
    else if (id == R.id.action_home) {
        Intent intent = new Intent(Preferences.this, HomeScreen.class);
        startActivity(intent);
    }
    else if (id == R.id.action_calendar) {
        Intent intent = new Intent(Preferences.this, CalendarActivity.class);
        startActivity(intent);
    }
    else if (id == R.id.action_statistics) {
        Intent intent = new Intent(Preferences.this, Statistics.class);
        startActivity(intent);
    }
    return super.onOptionsItemSelected(item);
}

public class MainActivity extends Activity {


    private String[] states;
    private Spinner spinner;


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

        states = getResources().getStringArray(R.array.countries_list);

        spinner = (Spinner) findViewById(R.id.country_spinner);

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, states);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {

            }

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

            }
        });
    }
}

}

そしてこれは私のcustomOnItemSelectedJavaファイルです:

package com.deitel.welcome;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id) {

        Toast.makeText(parent.getContext(), 
                "On Item Select : \n" +      parent.getItemAtPosition(pos).toString(),
                Toast.LENGTH_LONG).show();
    }

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

   }

}

これは私のリソースファイルです:

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <string name="spinner_prompt">Choose a country</string>

    <string-array name="country_arrays">
        <item>Malaysia</item>
        <item>United States</item>
        <item>Indonesia</item>
        <item>France</item>
        <item>Italy</item>
        <item>Singapore</item>
        <item>New Zealand</item>
        <item>India</item>
    </string-array>


</resources>

そしてこれはもちろん私のXMLファイルです:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dip" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="@string/country_label"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Spinner
        android:id="@+id/country_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/country_label" />

</LinearLayout>

誰かが私が間違ったことを理解できますか?

ご挨拶!ローザ

ローザ

みんな、ありがとう!その通りです...実際に自分のアクティビティ内に別のクラスを作成し、元のアクティビティでは何もしなかったことがわかりました。ご協力いただきありがとうございます!

私はすべてをファーストクラスにコピーして、いくつかのものを追加しました。これが最終的に私のために働いたコードです:

public class Preferences extends Activity { 

protected CharSequence[] _options = { "Monday", "Tuesday", "Wednesday",
        "Thursday", "Friday", "Saturday", "Sunday" };
protected boolean[] _selections = new boolean[_options.length];

protected Button _optionsButton;


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

    setContentView(R.layout.activity_preferences);

    _optionsButton = (Button) findViewById(R.id.button1);
    _optionsButton.setOnClickListener(new ButtonClickHandler());

}

public class ButtonClickHandler implements View.OnClickListener {
    public void onClick(View view) {
        showDialog(0);
    }
}

@Override
protected Dialog onCreateDialog (int id) {

            return new AlertDialog.Builder(this)
            .setTitle("Preferred Exercise Days")
            .setMultiChoiceItems(_options, _selections,
                    new DialogSelectionClickHandler())
            .setPositiveButton("OK", new DialogButtonClickHandler())
            .create();
}
}

public class DialogSelectionClickHandler implements
        DialogInterface.OnMultiChoiceClickListener {
    public void onClick(DialogInterface dialog, int clicked,
            boolean selected) {
        Log.i("ME", _options[clicked] + " selected: " + selected);
    }
}

public class DialogButtonClickHandler implements
        DialogInterface.OnClickListener {
    public void onClick(DialogInterface dialog, int clicked) {
        switch (clicked) {
        case DialogInterface.BUTTON_POSITIVE:
            String selected = "";
            int i = 0;
            for (boolean b : _selections) {
                if (b) {
                    selected += _options[i] + " ;";
                }
                i++;
            }
            printSelectedDays();
            Toast.makeText(Preferences.this, "Selected: " + selected,
                    Toast.LENGTH_SHORT).show();
            break;
        }
    }
}



protected void printSelectedDays() {
    for (int i = 0; i < _options.length; i++) {
        Log.i("ME", _options[i] + " selected: " + _selections[i]);
    }
}

解決策はちょっと違いますが、そもそも実現したかったので、やっぱり誰かに使ってもらいたいです。

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

iOS; アプリが実行されていないとき、またはバックグラウンドでプッシュ通知が機能しない

分類Dev

アプリが開いているときに解析を使用したCordovaプッシュ通知が機能しないアプリが閉じているとき、またはバックグラウンドでWindowsPhoneで機能している

分類Dev

Vue.jsのボタンをクリックしたときにアラート機能が機能しないのはなぜですか

分類Dev

関数で実行しているときに部分的に閉じたジェネリックが機能するのに、ServiceCollectionを直接呼び出しているときに機能しないのはなぜですか?

分類Dev

最初のクリックで機能が実行されない

分類Dev

セットアップモックが正常に機能しない(実行されたリポジトリメソッドのnull値を返す)

分類Dev

jQueryのクリックアンドトグルがクラスまたはIDで機能しない

分類Dev

アプリが実行されていない場合、PendingIntent内のサービスによる通知アクションは機能しません

分類Dev

CSPの実行時に外部リンクが機能しない

分類Dev

sudo で実行しないと Python スクリプトが機能しない

分類Dev

ポップアップスクリプトがIE9で機能しないのはなぜですか?

分類Dev

ItunesアプリのリンクがWkWebviewで機能しない

分類Dev

ItunesアプリのリンクがWkWebviewで機能しない

分類Dev

Android-アプリがロック状態またはスリープ状態のときにタイマーが定期的に実行されない

分類Dev

アプリが終了したとき、または実行されていないとき、iOSのAppStoreからアプリが更新されたときにコールバックはありますか?

分類Dev

画像をアップロードするときにリンク解除機能が古い画像を削除しないのはなぜですか

分類Dev

ボタン実行クリックが機能しない

分類Dev

クリックするたびにこの増分がjQueryで機能しないのはなぜですか?

分類Dev

フォークしたときにD2XXアプリケーションが機能しないのはなぜですか?

分類Dev

クリック機能が機能しないのはなぜですか?

分類Dev

jQueryクリック機能が機能しないのはなぜですか?

分類Dev

JavaFXがリリースされました:アプレットとJavaデスクトップは正式に機能しなくなっていますか?

分類Dev

あるスクリプトでは機能するが別のスクリプトでは機能しないサインアップ機能

分類Dev

クラス要素をクリックしたときにjQueryの「最も近い」が機能しないのはなぜですか

分類Dev

アプリがバックグラウンドにあるか実行されていない場合、プッシュ通知が正しく機能しない

分類Dev

アプリがバックグラウンドにあるか実行されていない場合、プッシュ通知が正しく機能しない

分類Dev

アプリがバックグラウンドにあるか実行されていない場合、プッシュ通知が正しく機能しない

分類Dev

Kotlin 1.3.30にアップグレードしたときに、KotlinJSR223スクリプトが機能しなくなったのはなぜですか

分類Dev

アプリを開いたままにすると自動ロックが機能しない-iOS

Related 関連記事

  1. 1

    iOS; アプリが実行されていないとき、またはバックグラウンドでプッシュ通知が機能しない

  2. 2

    アプリが開いているときに解析を使用したCordovaプッシュ通知が機能しないアプリが閉じているとき、またはバックグラウンドでWindowsPhoneで機能している

  3. 3

    Vue.jsのボタンをクリックしたときにアラート機能が機能しないのはなぜですか

  4. 4

    関数で実行しているときに部分的に閉じたジェネリックが機能するのに、ServiceCollectionを直接呼び出しているときに機能しないのはなぜですか?

  5. 5

    最初のクリックで機能が実行されない

  6. 6

    セットアップモックが正常に機能しない(実行されたリポジトリメソッドのnull値を返す)

  7. 7

    jQueryのクリックアンドトグルがクラスまたはIDで機能しない

  8. 8

    アプリが実行されていない場合、PendingIntent内のサービスによる通知アクションは機能しません

  9. 9

    CSPの実行時に外部リンクが機能しない

  10. 10

    sudo で実行しないと Python スクリプトが機能しない

  11. 11

    ポップアップスクリプトがIE9で機能しないのはなぜですか?

  12. 12

    ItunesアプリのリンクがWkWebviewで機能しない

  13. 13

    ItunesアプリのリンクがWkWebviewで機能しない

  14. 14

    Android-アプリがロック状態またはスリープ状態のときにタイマーが定期的に実行されない

  15. 15

    アプリが終了したとき、または実行されていないとき、iOSのAppStoreからアプリが更新されたときにコールバックはありますか?

  16. 16

    画像をアップロードするときにリンク解除機能が古い画像を削除しないのはなぜですか

  17. 17

    ボタン実行クリックが機能しない

  18. 18

    クリックするたびにこの増分がjQueryで機能しないのはなぜですか?

  19. 19

    フォークしたときにD2XXアプリケーションが機能しないのはなぜですか?

  20. 20

    クリック機能が機能しないのはなぜですか?

  21. 21

    jQueryクリック機能が機能しないのはなぜですか?

  22. 22

    JavaFXがリリースされました:アプレットとJavaデスクトップは正式に機能しなくなっていますか?

  23. 23

    あるスクリプトでは機能するが別のスクリプトでは機能しないサインアップ機能

  24. 24

    クラス要素をクリックしたときにjQueryの「最も近い」が機能しないのはなぜですか

  25. 25

    アプリがバックグラウンドにあるか実行されていない場合、プッシュ通知が正しく機能しない

  26. 26

    アプリがバックグラウンドにあるか実行されていない場合、プッシュ通知が正しく機能しない

  27. 27

    アプリがバックグラウンドにあるか実行されていない場合、プッシュ通知が正しく機能しない

  28. 28

    Kotlin 1.3.30にアップグレードしたときに、KotlinJSR223スクリプトが機能しなくなったのはなぜですか

  29. 29

    アプリを開いたままにすると自動ロックが機能しない-iOS

ホットタグ

アーカイブ