Skip to content

Instantly share code, notes, and snippets.

@410063005
Created June 26, 2018 08:09
Show Gist options
  • Save 410063005/1f72005b9c57ce9cd93807c73d3365fb to your computer and use it in GitHub Desktop.
Save 410063005/1f72005b9c57ce9cd93807c73d3365fb to your computer and use it in GitHub Desktop.
/*
* Copyright (C) 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.example.wordlistsql;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.util.List;
/**
* Implements a simple Adapter for a RecyclerView.
* Demonstrates how to add a click handler for each item in the ViewHolder.
*/
public class WordListAdapterAsync extends RecyclerView.Adapter<WordListAdapterAsync.WordViewHolder> {
/**
* Custom view holder with a text view and two buttons.
*/
class WordViewHolder extends RecyclerView.ViewHolder {
public final TextView wordItemView;
Button delete_button;
Button edit_button;
public WordViewHolder(View itemView) {
super(itemView);
wordItemView = (TextView) itemView.findViewById(R.id.word);
delete_button = (Button)itemView.findViewById(R.id.delete_button);
edit_button = (Button)itemView.findViewById(R.id.edit_button);
}
}
private static final String TAG = WordListAdapterAsync.class.getSimpleName();
public static final String EXTRA_ID = "ID";
public static final String EXTRA_WORD = "WORD";
public static final String EXTRA_POSITION = "POSITION";
private final LayoutInflater mInflater;
// WordListOpenHelper mDB;
Context mContext;
List<WordItem> mList;
public WordListAdapterAsync(Context context, List<WordItem> list) {
mInflater = LayoutInflater.from(context);
mContext = context;
mList = list;
}
@Override
public WordViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = mInflater.inflate(R.layout.wordlist_item, parent, false);
return new WordViewHolder(itemView);
}
@Override
public void onBindViewHolder(WordViewHolder holder, int position) {
WordItem current = mList.get(position);
holder.wordItemView.setText(current.getWord());
// Keep a reference to the view holder for the click listener
final WordViewHolder h = holder; // needs to be final for use in callback
// Attach a click listener to the DELETE button.
holder.delete_button.setOnClickListener(new MyButtonOnClickListener(
current.getId(), null) {
@Override
public void onClick(View v ) {
// You have to get the position like this, you can't hold a reference
Log.d (TAG + "onClick", "VHPos " + h.getAdapterPosition() + " ID " + id);
int deleted = -1; //mDB.delete(id);
if (deleted >= 0)
notifyItemRemoved(h.getAdapterPosition());
}
});
// Attach a click listener to the EDIT button.
holder.edit_button.setOnClickListener(new MyButtonOnClickListener(
current.getId(), current.getWord()) {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, EditWordActivity.class);
intent.putExtra(EXTRA_ID, id);
intent.putExtra(EXTRA_POSITION, h.getAdapterPosition());
intent.putExtra(EXTRA_WORD, word);
// Start an empty edit activity.
((Activity) mContext).startActivityForResult(intent, MainActivity.WORD_EDIT);
}
});
}
@Override
public int getItemCount() {
return mList.size();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment