单击从列表中选择的位置后,Android循环视图不会识别对象的ID

时间:2017-04-19 20:18:49

标签: android android-recyclerview recycler-adapter

我在Buttons中有一个RecycleView列表。我已在列表中的每个OnClickListener上实施了Button

 @Override
        public void onClick(View v) {
            int position = getAdapterPosition(); // gets item position
            if (position != RecyclerView.NO_POSITION) { // Check if an item was deleted, but the user clicked it before the UI removed it
                // We can access the data within the views
                Toast.makeText(context, position + " " + washLocations.get(position), Toast.LENGTH_SHORT).show();
                Log.d("myTag", "This is my message");
                Integer isFav = washLocations.get(position).getFav();
                WashLocation w = washLocations.get(position);
                Integer id = w.getId();
                List<WashLocation> washLocation = dataBaseHelper.getWashLocation();
                for(WashLocation wash : washLocation){
                    if(wash.getId().equals(w.getId())){
                        dataBaseHelper.updateWashLocation(w);
                        return;
                    }
                }
                w.setFav(isFav == 1 ? 0 : 1);
                dataBaseHelper.addWashLocationToFav(w);
                favorite.setText(w.getFav() == 1 ? "Usuń z ulubionych" : "Dodaj do ulubionych");
            }
        }

这种情况是每当我想比较已经点击的Wash对象的wash Id时,我总是得到id为null。但是当我从DB打印每个对象时,每个对象都有自己唯一的ID。

创建表格时,我添加了autoincrement

   String CREATE_TABLE = "CREATE TABLE " + TABLE_WASH_LOCATION + "(" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " + COL_WASH_NAME + " TEXT,"
                + COL_WASH_CITY + " TEXT," + COL_WASH_STREET + " TEXT," + COL_LAT + " TEXT," + COL_LNG + " TEXT," + COL_FAV + " INTEGER" + ")";
        db.execSQL(CREATE_TABLE);

实施例: enter image description here

更新

adapter中的washLocation列表是正式的:

washLocations = new ArrayList<>();
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mAdapter = new MyAdapter(washLocations, this);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

 MyAsyncTask myAsyncTask = new MyAsyncTask();

            try {
                JSONObject jsonObject =  myAsyncTask.execute(latitude, longitude, radius).get();
                String lat;
                String lng;
                String name = "";
                String city = "";

            if (jsonObject.has("results")) {
                JSONArray jsonArray = jsonObject.getJSONArray("results");
                for (int n = 0; n < jsonArray.length(); n++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(n);
                    lat = jsonObject1.getJSONObject("geometry").getJSONObject("location").get("lat").toString();
                    lng = jsonObject1.getJSONObject("geometry").getJSONObject("location").get("lng").toString();

                    JSONObject oName = jsonArray.getJSONObject(n);
                    if (oName.has("name")) {
                        name = oName.getString("name");
                    }

                    JSONObject oVicinity = jsonArray.getJSONObject(n);
                    if (oVicinity.has("vicinity")) {
                        city = oVicinity.getString("vicinity");
                    }
                    WashLocation w = new WashLocation(name, lat, lng, 0, getCity(city), null);
                    washLocations.add(w);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

1 个答案:

答案 0 :(得分:0)

我不确定我是否明白你要做什么,我稍微清理了你的代码并添加了以Q开头的评论,你能回答这些问题吗?

@Override
        public void onClick(View v) {
            int position = getAdapterPosition(); // gets item position
            if (position != RecyclerView.NO_POSITION) { // Check if an item was deleted, but the user clicked it before the UI removed it
                WashLocation w = washLocations.get(position);         
                // We can access the data within the views
                // Q: Does this show the right WashLocation ?
                Toast.makeText(context, position + " " + w, Toast.LENGTH_SHORT).show();
                Log.d("myTag", "This is my message");                
                Integer isFav = w.getFav();                
                Integer id = w.getId();
                // Q: Will this list be different from washLocations ?
                List<WashLocation> washLocation = dataBaseHelper.getWashLocation();
                for(WashLocation wash : washLocation){
                    if(wash.getId().equals(id)){
                        // Q: Why can't you execute this line immediately ? 
                        // Q: What does this line actually do ?
                        dataBaseHelper.updateWashLocation(w);
                        return;
                    }
                }
                w.setFav(isFav == 1 ? 0 : 1);
                dataBaseHelper.addWashLocationToFav(w);
                favorite.setText(w.getFav() == 1 ? "Usuń z ulubionych" : "Dodaj do ulubionych");
            }
        }