按下微调器时显示微调器列表但列表项未选择且未在Toast中显示

时间:2017-01-12 10:48:40

标签: android

这是我的代码我非常想在微调器中显示项目,但一切都是徒劳的 列表显示按下微调器箭头但没有选择并且没有显示在吐司中所以请帮助我并指导我在哪里错了

 public class MakeComplain extends AppCompatActivity implements    
 OnItemSelectedListener {
 private Spinner spinner,spinner1,spinner3;
 private TextView txtCategory;

 private ArrayList<String> Station;



private JSONArray result;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_make_complain);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

     spinner1 = (Spinner) findViewById(R.id.station);
    spinner3 = (Spinner) findViewById(R.id.generator);
    spinner = (Spinner) findViewById(R.id.complain);

    spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            //Get data from the list which you had set to the adapter of spinner1
            Toast.makeText(MakeComplain.this, "Spinner1 Item Clicked: "+position, Toast.LENGTH_SHORT).show();
        }

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

        }
    });

    spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            //Get data from the list which you had set to the adapter of spinner3
            Toast.makeText(MakeComplain.this, "Spinner3 Item Clicked: "+position, Toast.LENGTH_SHORT).show();
        }

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

        }
    });

    ArrayAdapter arrayAdapter = ArrayAdapter.createFromResource(this, R.array.Complain, android.R.layout.simple_spinner_item);
    spinner.setAdapter(arrayAdapter);


    spinner1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, this.fetchTwitterPublicTimeline()));
    spinner3.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, this.fetchGeneratorList()));





}
public ArrayList<String> fetchTwitterPublicTimeline()
{
    final  ArrayList<String> listItems = new ArrayList<String>();

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                URL url = new URL("http://bigbi.com/cs/Station.php");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                //urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0");
                InputStream stream = urlConnection.getInputStream();
                InputStreamReader reader = new InputStreamReader(stream);
                BufferedReader bufferedReader = new BufferedReader(reader);
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    JSONArray ja = new JSONArray(line);

                    for (int i = 0; i < ja.length(); i++) {
                        JSONObject jo = (JSONObject) ja.get(i);
                        listItems.add(jo.getString("name"));
                    }
                }
                bufferedReader.close();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }).start();

    return listItems;
}
public ArrayList<String> fetchGeneratorList()
{
    final  ArrayList<String> listItems = new ArrayList<String>();

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                URL url = new URL("http://biges.com/12/get.php");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                //urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0");
                InputStream stream = urlConnection.getInputStream();
                InputStreamReader reader = new InputStreamReader(stream);
                BufferedReader bufferedReader = new BufferedReader(reader);
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    JSONArray ja = new JSONArray(line);

                    for (int i = 0; i < ja.length(); i++) {
                        JSONObject jo = (JSONObject) ja.get(i);
                        listItems.add(jo.getString("gen_no"));
                    }
                }
                bufferedReader.close();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }).start();

    return listItems;
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
                           long id) {


}


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

}

3 个答案:

答案 0 :(得分:0)

请尝试这个,

spinner1.setAdapter(new ArrayAdapter<String>(this, R.layout.spinner_item, fetchTwitterPublicTimeline()));
spinner3.setAdapter(new ArrayAdapter<String>(this, R.layout.spinner_item,fetchGeneratorList()));

点击项目可以添加。

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

Toast.makeText(getActivity(), "Item clicked",
   Toast.LENGTH_LONG).show();


}

答案 1 :(得分:0)

你错过了两件事。

  1. 您尚未添加用于选择项目的侦听器。添加以下内容以添加侦听器。

    spinner.setOnItemSelectedListener(this);
    

    如果需要,可以为其他微调器添加相同的颜色。

  2. onItemSelected()方法中添加用于显示toast的代码。像这样的东西

    Toast.makeText(this, "Item Clicked", Toast.LENGTH_SHORT).show();
    

答案 2 :(得分:0)

你为spinner实现了onItemSelection的监听器,但你没有将onItemSelection设置为微调器,所以像这样更新你的onCreate:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_make_complain);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        spinner1 = (Spinner) findViewById(R.id.station);
        spinner3 = (Spinner) findViewById(R.id.generator);
        spinner = (Spinner) findViewById(R.id.complain);

        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //Get data from the list which you had set to the adapter of spinner1
                Toast.makeText(this, "Spinner1 Item Clicked: "+position, Toast.LENGTH_SHORT).show();
            }

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

            }
        });

        spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //Get data from the list which you had set to the adapter of spinner3
                Toast.makeText(this, "Spinner3 Item Clicked: "+position, Toast.LENGTH_SHORT).show();
            }

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

            }
        });

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //Get data from the list which you had set to the adapter of spinner
                Toast.makeText(this, "Spinner Item Clicked: "+position, Toast.LENGTH_SHORT).show();
            }

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

            }
        });

        ArrayAdapter arrayAdapter = ArrayAdapter.createFromResource(this, R.array.Complain, android.R.layout.simple_spinner_item);
        spinner.setAdapter(arrayAdapter);

        Station=this.fetchTwitterPublicTimeline();

        spinner1.setAdapter(new ArrayAdapter<String>(this, R.layout.spinner_item,R.id.txt, this.fetchTwitterPublicTimeline()));
        spinner3.setAdapter(new ArrayAdapter<String>(this, R.layout.spinner_item,R.id.txt, this.fetchGeneratorList()));

    }

我个人将setOnItemSelectedListener设置为spinners,因为如果我们覆盖一个方法,那么你将无法找出哪个spinners项目选择是回调。

因此,删除工具的实现  来自您的Activity的OnItemSelectedListener,相对而言它也是覆盖的方法,并按照我的回答做。