我是android的新手,我从db获取数据并在recycleler视图中显示。如何获取项目的价值。我能够获得这个位置,但我不确定如何获得这个价值。
import android.content.Context;
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.TextView;
import android.widget.Toast;
import java.util.Collections;
import java.util.List;
public class AdapterSearch extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LayoutInflater inflater;
List<DataSearch> data= Collections.emptyList();
DataSearch current;
// create constructor to initialize context and data sent from MainActivity
public AdapterSearch(Context context, List<DataSearch> data){
this.context=context;
inflater= LayoutInflater.from(context);
this.data=data;
}
// Inflate the layout when ViewHolder created
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.container_search, parent,false);
MyHolder holder=new MyHolder(view);
return holder;
}
// Bind data
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// Get current position of item in RecyclerView to bind data and assign values from list
MyHolder myHolder= (MyHolder) holder;
DataSearch current=data.get(position);
myHolder.textsanctname.setText(current.sanctname);
myHolder.textstate.setText("State " + current.state);
myHolder.textType.setText("Category " + current.catagory);
}
// return total item from List
@Override
public int getItemCount() {
return data.size();
}
class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView textsanctname;
TextView textstate;
TextView textType;
// create constructor to get widget reference
public MyHolder(View itemView) {
super(itemView);
textsanctname= (TextView) itemView.findViewById(R.id.textsanctname);
textstate = (TextView) itemView.findViewById(R.id.textstate);
textType = (TextView) itemView.findViewById(R.id.textType);
itemView.setOnClickListener(this);
}
// Click event for all items
@Override
public void onClick(View v) {
Toast.makeText(context, "You clicked "+getPosition(),Toast.LENGTH_SHORT).show();
}
}
}
提前致谢。
答案 0 :(得分:1)
您需要使用getter和setter
创建Model类Modelclass.java
public class FeaturedContentModel {
String featuredContent;
public String getFeaturedContent() {
return featuredContent;
}
public void setFeaturedContent(String featuredContent) {
this.featuredContent = featuredContent;
}
}
在您的适配器实现中单击侦听器
MyAdapter.java
public class FeaturedContentAdapter extends RecyclerView.Adapter<FeaturedContentAdapter.ViewHolder> {
ArrayList<FeaturedContentModel> featuredContentModelArrayList;
FeaturedContentModel featuredContentModel;
Context mContext;
FeaturedContentAdapter.OnClickInterface onClickInterface;
public FeaturedContentAdapter(ArrayList<FeaturedContentModel> featuredContentModelArrayList, Context mContext, OnClickInterface onClickInterface) {
this.featuredContentModelArrayList = featuredContentModelArrayList;
this.mContext = mContext;
this.onClickInterface = onClickInterface;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.featured_content_single_row, parent, false);
return new FeaturedContentAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
featuredContentModel = featuredContentModelArrayList.get(position);
holder.tv_featured_content_name.setText(featuredContentModel.getFeaturedContent());
}
@Override
public int getItemCount() {
return featuredContentModelArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tv_featured_content_name;
public ViewHolder(View itemView) {
super(itemView);
tv_featured_content_name = (TextView) itemView.findViewById(R.id.tv_featured_contents_name);
itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// get position
int pos = getAdapterPosition();
FeaturedContentModel featuredContentModel=featuredContentModelArrayList.get(pos);
onClickInterface.setOnClick(featuredContentModel);
// check if item still exists
if(pos != RecyclerView.NO_POSITION){
FeaturedContentModel clickedDataItem = featuredContentModelArrayList.get(pos);
Toast.makeText(v.getContext(), "Clicked ", Toast.LENGTH_SHORT).show();
}
}
});
}
}
public interface OnClickInterface{
void setOnClick(FeaturedContentModel featuredContentModel);
}
}
主要Activity.java
RecyclerView.Adapter adapter = new FeaturedContentAdapter(featuredContentModelArrayList, FeaturedContent.this, new FeaturedContentAdapter.OnClickInterface() {
@Override
public void setOnClick(FeaturedContentModel featuredContentModel) {
startActivity(new Intent(FeaturedContent.this,DetailsContentActivity.class).putExtra("title",featuredContentModel.getFeaturedContent()));
}
});
featuredContentsRecyclerView.setAdapter(adapter);
答案 1 :(得分:0)
// Click event for all items
@Override
public void onClick(View v) {
DataSearch clickedData = data.get(getPosition());
Toast.makeText(context, "You clicked "+clickedData.getXXX(),Toast.LENGTH_SHORT).show();
}
将值分配给变量,您可以在Toast中显示该值。
答案 2 :(得分:0)
找到解决方案
写下这门课程
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
public void onLongItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && mListener != null) {
mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
}
}
});
}
@Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return true;
}
return false;
}
@Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
@Override
public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
}
在您的活动中写
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Log.d("position",position);
}
@Override
public void onLongItemClick(View view, int position) {
// do whatever
}
})
);
答案 3 :(得分:0)
将您的代码更改为此。在这里,如果您点击“textsanctname”视图,则值将以toast
显示onActivityResult()