从onclick获取数据

时间:2018-03-12 18:08:17

标签: android android-recyclerview

首先,我的问题与this完全相似,但我仍然无法解决我的问题。

public class PlacesAdapter extends RecyclerView.Adapter<PlacesAdapter.ViewHolder> {

  private static final String TAG = "PlaceAdapter";

  List<PlaceSaved> items;
  PlaceDatabase db;
  public PlacesAdapter(List<PlaceSaved> items, PlaceDatabase db) {
    this.items = items;
    this.db = db;
  }

    @Override
    public PlacesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext())
          .inflate(R.layout.places_list_item,parent,false);
      return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final PlacesAdapter.ViewHolder holder, final int position) {
      holder.name.setText(items.get(position).getTitle());
      holder.time.setText(items.get(position).getTime());
      holder.longi.setText(items.get(position).getLongi());
      holder.delbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
        //  removeItem(items.get(holder.getAdapterPosition()));
          AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
              AsyncTask.execute(new Runnable() {
                @Override
                public void run() {
                  db.databaseInterface().delete(items.get(position));
                  Snackbar.make(view , items.get(position).getTime()+" deleted, will be updated next time!", Snackbar.LENGTH_LONG).show();
                }
              });
            }
          });
        }
      });

      holder.cview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        Log.e("CardView", "Clicked "+items.get(position).getTitle()+", "+items.get(position).getLongi());
        Location Saved_Location_onCard = new Location("");
        Saved_Location_onCard.setLatitude(Double.parseDouble(items.get(position).getTitle()));
        Saved_Location_onCard.setLatitude(Double.parseDouble(items.get(position).getLongi()));
        latlang.Lat = (Double.parseDouble(items.get(position).getTitle()));
        latlang.Lang = (Double.parseDouble(items.get(position).getLongi()));

        Snackbar.make(view, "The new location is "+
                items.get(position).getTitle()+", "+items.get(position).getLongi(),
            Snackbar.LENGTH_LONG).show();
//        Intent intent = new Intent(, SunFragment.class)
         SecondFragment sf = new SecondFragment();
          sf.updateLocationUI();
//          sf.showMap();
        }
      });
    }

onBindViewHolder内意识到onclick不是一个好主意后,我试图将其移至ViewHolder

public class ViewHolder extends RecyclerView.ViewHolder
        implements View.OnClickListener{
      public TextView name;
      public TextView time;
      public TextView longi;
      public ImageButton delbutton;
      public CardView cview;

      public ViewHolder(View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.secondLine);
        time= itemView.findViewById(R.id.firstLine);
        longi = itemView.findViewById(R.id.longitude);
        delbutton = itemView.findViewById(R.id.delicon);
        cview = itemView.findViewById(R.id.place_view);
        itemView.setOnClickListener(this);
      }
      @Override
      public void onClick(View view){
        //Snackbar.make(this,"Hi", Snackbar.LENGTH_LONG).show();
        Log.e("onClick", "Hello"+getLayoutPosition());
      }
    }

所以,我很容易得到布局的位置,但不是存储在卡中的值,就像我在BindView中定义它时所做的那样。

我怎么能这样做?

另外,任何想法如何删除和更新删除功能?

1 个答案:

答案 0 :(得分:2)

使用getAdapterPosition()获得职位。

 @Override
    public void onClick(View view){
        // Clicked postion 
        int position =getAdapterPosition();
        PlaceSaved place=items.get(position);
        // Here is the data Do your stuff 
    }

删除具有位置调用的特定项目。

items.remove(position);
notifyItemRemoved(position);

阅读RecyclerView.Adapter Here的通知方法。