在微调器中选择项目时,在文本视图中显示值

时间:2017-05-15 16:41:26

标签: java android android-spinner

我找不到位置来显示我的项目的确切值。

data.json

 {
    "interactions":
    [
 {
   "MDT1": "Un",
   "MDT2": "One",
   "CI": "",
   "AD": "",
   "PE": "",
   "PC": "OnePc"
 },
 {
   "MDT1": "Un",
   "MDT2": "One2",
   "CI": "",
   "AD": "One2AD",
   "PE": "",
   "PC": ""
 },
 {
   "MDT1": "Trois",
   "MDT2": "Three",
   "CI": "",
   "AD": "",
   "PE": "ThreePE",
   "PC": ""
 },

]
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    JSONObject jsonobject;
    JSONArray jsonarray;
    private SearchableSpinner mySpinner1;
    private SearchableSpinner mySpinner2;
    private TextView txtMDT1;
    private TextView txtMDT2;
    private TextView txtT;

    private SimpleListAdapter myAdapter1;
    private SimpleListAdapter myAdapter2;

    private final ArrayList<Interactions> world = new ArrayList<>();
    private final ArrayList<String> mStrings = new ArrayList<>();
    private final ArrayList<String> m1Strings = new ArrayList<>();

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        new DownloadJSON().execute();

         mySpinner1 = (SearchableSpinner) findViewById(R.id.mySpinner1);
        myAdapter1 = new SimpleListAdapter(this, mStrings);
        mySpinner1.setOnItemSelectedListener(mOnItemSelectedListener1);
        mySpinner1.setAdapter(myAdapter1);

         mySpinner2 = (SearchableSpinner) findViewById(R.id.mySpinner2);
        myAdapter2 = new SimpleListAdapter(this, m1Strings);
        mySpinner2.setOnItemSelectedListener(mOnItemSelectedListener2);
        mySpinner2.setAdapter(myAdapter2);


        txtMDT1  = (TextView) findViewById(R.id.MDT1);
        txtMDT2  = (TextView) findViewById(R.id.MDT2);
        txtT  = (TextView) findViewById(R.id.T);

    }


    private class DownloadJSON extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {

            jsonobject = JSONfunctions
                    .getJSONfromURL("https://rabah.000webhostapp.com/data.txt");
            try {

                jsonarray = jsonobject.getJSONArray("interactions");

                for (int j = 0; j < jsonarray.length(); j++ ) {
                    jsonobject = jsonarray.getJSONObject(j);
                    Interactions worldpop = new Interactions();

                    worldpop.setMDT1(jsonobject.getString("MDT1"));
                    worldpop.setMDT2(jsonobject.getString("MDT2"));
                    worldpop.setPE(jsonobject.getString("PE"));
                    worldpop.setCI(jsonobject.getString("CI"));
                    worldpop.setAD(jsonobject.getString("AD"));
                    worldpop.setPC(jsonobject.getString("PC"));

                    world.add(worldpop);

                    mStrings.add(jsonobject.getString("MDT1"));
                    m1Strings.add(jsonobject.getString("MDT2"));
                }

            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
        }
    }

    private OnItemSelectedListener mOnItemSelectedListener1 = new OnItemSelectedListener() {
        @Override
        public void onItemSelected(View view, int position, long id) {

            txtMDT1.setText(String.format("MDT1 : %s", myAdapter1.getItem(position)));
            txtMDT2.setText("");
            txtT.setText("");

            String mdt2 = world.get(position).getMDT2();

            for (int j = 0; j < mStrings.size(); j++ ) {

                    m1Strings.clear();
                    m1Strings.add(mdt2);
                    myAdapter2.notifyDataSetChanged();

            }

            Toast.makeText(MainActivity.this, "Item on position " + position + " : "
                    + myAdapter1.getItem(position) + " Selected", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected() {
            Toast.makeText(MainActivity.this, "Nothing Selected", Toast.LENGTH_SHORT).show();
        }
    };

    private OnItemSelectedListener mOnItemSelectedListener2 = new OnItemSelectedListener() {
        @Override
        public void onItemSelected(View view, int position, long id) {

            txtMDT2.setText(String.format("MDT2 : %s", myAdapter2.getItem(position)));

            String PC = world.get(position).getPC();
            String AD = world.get(position).getAD();
            String PE = world.get(position).getPE();


            for (int j = 0; j < m1Strings.size(); j++ ) {

            if (!Objects.equals(PC, "")) {
                txtT.setText(String.format("PC : %s", PC));

            } else if (!Objects.equals(AD, "")) {
                txtT.setText(String.format("AD : %s", AD));

            } else if (!Objects.equals(PE, "")) {
                txtT.setText(String.format("PE : %s", PE));
            }
            Toast.makeText(MainActivity.this, "Item on position " + position + " : "
                    + myAdapter2.getItem(position) + " Selected", Toast.LENGTH_SHORT).show();
        }
        }

        @Override
        public void onNothingSelected() {
            Toast.makeText(MainActivity.this, "Nothing Selected", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_reset) {
            mySpinner1.setSelectedItem("");
            mySpinner2.setSelectedItem("");
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

content_main.xml

<gr.escsoft.michaelprimez.searchablespinner.SearchableSpinner
        android:id="@+id/mySpinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginEnd="24dp"
        android:gravity="center_horizontal"
        app:StartSearchTintColor="@android:color/white"
        app:DoneSearchTintColor="@android:color/holo_purple"
        app:RevealViewBackgroundColor="@android:color/holo_purple"
        app:SearchViewBackgroundColor="@android:color/secondary_text_dark"
        app:ShowBorders="false"
        app:RevealEmptyText="Touch to select"
        app:SpinnerExpandHeight="300dp"/>

    <gr.escsoft.michaelprimez.searchablespinner.SearchableSpinner
        android:id="@+id/mySpinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginEnd="24dp"
        android:gravity="center_horizontal"
        app:StartSearchTintColor="@android:color/white"
        app:DoneSearchTintColor="@android:color/holo_purple"
        app:RevealViewBackgroundColor="@android:color/holo_purple"
        app:SearchViewBackgroundColor="@android:color/secondary_text_dark"
        app:ShowBorders="false"
        app:RevealEmptyText="Touch to select"
        app:SpinnerExpandHeight="300dp"
        android:layout_below="@+id/mySpinner1"
        />

    <TextView
        android:id="@+id/MDT1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/mySpinner2" />
    <TextView
        android:id="@+id/MDT2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/MDT1" />

    <TextView
        android:id="@+id/T"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/MDT2" />

</RelativeLayout>

结果mOnItemSelectedListener2:

img

必须如此:

  • MDT1:Trois
  • MDT2:三
  • PE:ThreePE ......位置值Trois。

而不是:

  • MDT1:Trois
  • MDT2:三
  • PC:OnePC ......位置Un。
  • 的值

0 个答案:

没有答案