当我使用arraylist tospinner时,我无法从微调器中获取所选数据

时间:2017-02-18 21:01:24

标签: java android arraylist spinner

当我使用arraylist到spinner时,我无法从微调器中获取所选数据。

这里当我使用固定长度的数组时,它会将微调器的选定数据设置为textview,但是当我使用arraylist时它就没有被设置。

public class GetData extends AsyncTask<String,String,String> {
    int i=0;
    String data;
    String[] array;

    @Override
    protected String doInBackground(String... strings) {
        // data = strings[0];
        arrayList.clear();
        Query query = FirebaseDatabase.getInstance().getReference().child(strings[0]);
        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                long c=dataSnapshot.getChildrenCount();
                int count = (int)c;
                array= new String[count];

                Iterator iterator = dataSnapshot.getChildren().iterator();
                while (iterator.hasNext()) {
                    DataSnapshot dataSnapshot1 = (DataSnapshot) iterator.next();
                    // array[i] = dataSnapshot1.getValue().toString();
                    // i++;
                    arrayList.add(dataSnapshot1.getValue().toString());
                }
                Toast.makeText(Register_Activity.this,"Now Select : "/*+array[0]*/, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        // super.onPostExecute(s);

        switch(n){
            case 0:
                adapter[0] = new ArrayAdapter(Register_Activity.this, android.R.layout.simple_spinner_item, array);
                spinner[0].setAdapter(adapter[0]);
                break;
            case 1:
                adapter[1] = new ArrayAdapter(Register_Activity.this, android.R.layout.simple_spinner_item, array);
                spinner[1].setAdapter(adapter[1]);
                break;
            case 2:
                adapter[2] = new ArrayAdapter(Register_Activity.this, android.R.layout.simple_spinner_item, array);
                spinner[2].setAdapter(adapter[2]);
                break;
            case 3:
                adapter[3] = new ArrayAdapter(Register_Activity.this, android.R.layout.simple_spinner_item, array);
                spinner[3].setAdapter(adapter[3]);
                break;
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                spinner[n].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                        // data = spinner[n].getSelectedItem().toString();
                        data = spinner[n].getItemAtPosition(i).toString();
                        Toast.makeText(Register_Activity.this, "hey"+data, Toast.LENGTH_SHORT).show();
                        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Register_Activity.this);
                        alertDialogBuilder.setMessage("Are you sure: "+data);
                        alertDialogBuilder.setPositiveButton("yes",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {
                                **textViews[n].setText(data);
                                spinner[n].setEnabled(false);**
                                n++;
                                if (n < 4) {
                                    new GetData().execute(data);
                                }
                            }
                        });

                        alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                return;
                            }
                        });

                        AlertDialog alertDialog = alertDialogBuilder.create();
                        alertDialog.show();
                    }

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

                    }
                });
            }
        }, 300);
    }
}

0 个答案:

没有答案