/*
* Copyright 2014 sonaive.com. All rights reserved.
*
* 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.sonaive.v2ex.ui.adapter;
import android.content.Context;
import android.database.Cursor;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.sonaive.v2ex.R;
import com.sonaive.v2ex.provider.V2exContract;
import com.sonaive.v2ex.widget.PaginationCursorAdapter;
import de.greenrobot.event.EventBus;
/**
* Created by liutao on 1/5/15.
*/
public class SearchCursorAdapter extends PaginationCursorAdapter<SearchCursorAdapter.ViewHolder> {
/**
* Recommended constructor.
*
* @param context The context
* @param c The cursor from which to get the data.
* @param flags Flags used to determine the behavior of the adapter;
* Currently it accept {@link #FLAG_REGISTER_CONTENT_OBSERVER}.
*/
public SearchCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
@Override
public void onBindViewHolder(ViewHolder holder, Cursor cursor) {
if (cursor != null) {
final String keyword = cursor.getString(cursor.getColumnIndex(V2exContract.Search.SEARCH_KEYWORD));
holder.keyword.setText(keyword);
holder.container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(keyword);
}
});
}
}
@Override
protected void onContentChanged() {
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_history, parent, false);
// set the view's size, margins, paddings and layout parameters
return new ViewHolder(v);
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView keyword;
public LinearLayout container;
public ViewHolder(View view) {
super(view);
keyword = (TextView) view.findViewById(R.id.keyword);
container = (LinearLayout) view.findViewById(R.id.container);
}
}
}