从WatchKit 2中的选定选择行获取值

时间:2015-08-14 19:40:30

标签: swift watchkit xcode7 watch-os-2

我正在使用Xcode 7,当我按下按钮时,我正试图将选择器中的值传递给另一个函数。是否可以获取当前在选择器中选择的行的值?或者我必须以某种方式使用选择器作为按钮?我尝试实现didSelectRow,但是当我尝试设置它时,我收到一个错误:使用未声明的类型'UIPicker View'。我猜我必须以某种方式检索源数组的索引。

1 个答案:

答案 0 :(得分:2)

您正在尝试使用UIKit类。

将watchkit类与watch os 2一起使用:

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

        // Toolbabr settings
        getSupportActionBar().setDisplayShowHomeEnabled(true);   
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        getSupportActionBar().setLogo(R.drawable.ic_horor_filmovi_ikonica);

        Intent newActivity2=new Intent();
        setResult(RESULT_OK, newActivity2);

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
          @Override
          public void run() {
              noInternet();
          }
        }, 100);

        listView = (ListView) findViewById(R.id.list);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
        adapter = new CustomListAdapter(this, movieList);
        listView.setAdapter(adapter);

        pDialog = new ProgressDialog(this);
        // Showing progress dialog before making http request
        pDialog.setMessage("Učitavanje...");
        pDialog.setCancelable(false);
        pDialog.show();      

        swipeRefreshLayout.setOnRefreshListener(this);

        /**
         * Showing Swipe Refresh animation on activity create
         * As animation won't start on onCreate, post runnable is used
         */
        swipeRefreshLayout.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        swipeRefreshLayout.setRefreshing(true);

                                        fetchMovies();
                                    }
                                }
        );


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) { 
                String name = ((TextView) view.findViewById(R.id.title))
                        .getText().toString();

                String opisFilma = ((TextView) view.findViewById(R.id.opis))
                        .getText().toString();

                String urlFilm = ((TextView) view.findViewById(R.id.url))
                        .getText().toString();

                String ocena = String.valueOf(movieList.get(position).getRating());

                String godina = String.valueOf(movieList.get(position).getYear());

                bitmap = ((Movie)movieList.get(position)).getThumbnailUrl();

                Intent intent = new Intent(ListaPreporuka.this, MoviesSingleActivity.class);    
                intent.putExtra(Title, name);
                intent.putExtra(opis, opisFilma);
                intent.putExtra("images", bitmap);
                intent.putExtra(Rating, ocena);
                intent.putExtra(Year, godina);
                intent.putExtra(urlMovie, urlFilm);
                startActivity(intent);
                overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
                }
            });

    }

    @Override
    public void onRefresh() {
        fetchMovies();

    }

    private void fetchMovies(){

        // showing refresh animation before making http call
        swipeRefreshLayout.setRefreshing(true);

        // Creating volley request obj
        JsonArrayRequest movieReq = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                Movie movie = new Movie();
                                movie.setTitle(obj.getString("title"));
                                movie.setOpis(obj.getString("opis"));
                                movie.setThumbnailUrl(obj.getString("image"));
                                movie.setRating(((Number) obj.get("rating"))
                                        .doubleValue());
                                movie.setYear(obj.getInt("releaseYear"));
                                movie.setUrl(obj.getString("url"));

                                // Genre is json array
                                final JSONArray genreArry = obj.getJSONArray("genre");
                                ArrayList<String> genre = new ArrayList<String>();
                                for (int j = 0; j < genreArry.length(); j++) {
                                    genre.add((String) genreArry.get(j));
                                }
                                movie.setGenre(genre);

                                // adding movie to movies array
                                movieList.add(movie);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }                        

                        }
                                adapter.notifyDataSetChanged();  

                             // stopping swipe refresh
                                swipeRefreshLayout.setRefreshing(false);
                    }


                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        hidePDialog();

                        // stopping swipe refresh
                        swipeRefreshLayout.setRefreshing(false);
                    }
                });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(movieReq);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }

    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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.
        if (item.getItemId() == android.R.id.home) {
            finish();
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            return true;
        }
        return false;
    }

    @Override
    public void onBackPressed() {

        super.onBackPressed();
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }

    private void noInternet() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info != null) {
            if (!info.isConnected()) {
            }
        }
        else {
            Toast.makeText(this, "No Internet Connection", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(ListaPreporuka.this, NoConnection.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
        }
    }

}