我无法点击我的recyclerview上的项目,我认为我的代码很好
Mainactivity
public class Appetizer extends AppCompatActivity {
public static final int CONNECTION_TIMEOUT=10000;
public static final int READ_TIMEOUT=15000;
private RecyclerView recyclerView;
private AdapterFood mAdapter;
List<DataFood> data=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_appetizer);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
new AsyncFetch().execute();
}
private class AsyncFetch extends AsyncTask<String,String,String>{
ProgressDialog pdLoading = new ProgressDialog(Appetizer.this);
HttpURLConnection conn;
URL url = null;
@Override
protected void onPreExecute(){
super.onPreExecute();
pdLoading.setMessage("\tLoading...");
pdLoading.setCancelable(false);
pdLoading.show();
}
@Override
protected String doInBackground(String... params) {
try{
url = new URL("http://kelompokdua.hol.es/private_html/shAppetizer.php");
} catch (MalformedURLException e) {
e.printStackTrace();
return e.toString();
}
try {
conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("GET");
conn.setDoOutput(true);
} catch (IOException e1) {
e1.printStackTrace();
return e1.toString();
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return (result.toString());
} else {
return ("unsuccessful");
}
} catch (IOException e) {
e.printStackTrace();
return e.toString();
} finally {
conn.disconnect();
}
}
@Override
protected void onPostExecute(String result) {
//this method will be running on UI thread
pdLoading.dismiss();
List<DataFood> data=new ArrayList<>();
pdLoading.dismiss();
try {
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONArray innerArray = jArray.getJSONArray(i);
for (int j = 0; j < innerArray.length(); j++){
JSONObject json_data = innerArray.getJSONObject(j);
// JSONArray foodArray= json_data.getJSONArray("");
// for (int j=0;j<foodArray.length();j++){
DataFood foodData = new DataFood();
//JSONObject gambar = json_data.getJSONObject("gambar");
foodData.foodImage= json_data.getString("gambar");
foodData.foodName= json_data.getString("nama");
foodData.foodId= json_data.getInt("id");
foodData.price= json_data.getInt("harga");
data.add(foodData);
}}
// }
recyclerView = (RecyclerView)findViewById(R.id.lvaptzr);
LinearLayoutManager layoutManager=new LinearLayoutManager(Appetizer.this);
recyclerView.setLayoutManager(layoutManager);
//recyclerView.setLayoutManager(new LinearLayoutManager(Appetizer.this));
mAdapter = new AdapterFood(Appetizer.this,data);
recyclerView.setAdapter(mAdapter);
} catch (JSONException e) {
Toast.makeText(Appetizer.this,e.toString(),Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}}
}
这是我的适配器
public class AdapterFood extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private Context context;
private LayoutInflater inflater;
List<DataFood> data = Collections.emptyList();
DataFood current;
int currentPos=0;
public AdapterFood(Context context, List<DataFood> data){
this.context=context;
inflater=LayoutInflater.from(context);
this.data=data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.list_item,parent,false);
Myholder holder=new Myholder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final Myholder myholder=(Myholder) holder;
DataFood current=data.get(position);
myholder.textFoodname.setText(current.foodName);
myholder.textPrice.setText("Rp. " + current.price);
myholder.textId.setText(String.valueOf(current.foodId));
// Log.d("IMAGE_URL", "http://kelompokdua.hol.es/pic/" + current.foodImage);
// Picasso.with(context).load("http://kelompokdua.hol.es/pic/" + current.foodImage).into(myholder.ivFood);
Glide.with(context).load(current.foodImage).asBitmap()
// .placeholder(R.mipmap.ic_launcher).dontAnimate()
// .error(R.mipmap.minus)
.into(myholder.ivFood);
}
@Override
public int getItemCount() {
return data.size();
}
class Myholder extends RecyclerView.ViewHolder{
TextView textFoodname,textPrice,textId;
ImageView ivFood;
public Myholder(View view) {
super(view);
textFoodname=(TextView) view.findViewById(R.id.textFoodname);
ivFood=(ImageView)view.findViewById(R.id.ivFood);
textId=(TextView)view.findViewById(R.id.textid);
textPrice=(TextView) view.findViewById(R.id.textPrice);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
if (pos != RecyclerView.NO_POSITION){
DataFood clickedDataItem = data.get(pos);
Intent intent = new Intent(context, Order.class);
intent.putExtra("nama", data.get(pos).foodName);
intent.putExtra("harga", data.get(pos).price);
intent.putExtra("gambar", data.get(pos).foodImage);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Toast.makeText(v.getContext(),"clicked" + clickedDataItem.foodName, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(v.getContext(),"ga",Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
AdapterLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="390dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:clickable="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical">
<ImageView
android:id="@+id/ivFood"
android:layout_width="match_parent"
android:layout_height="320dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:clickable="true"
android:scaleType="fitXY"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toEndOf="@id/ivFood"
android:layout_toRightOf="@id/ivFood"
android:clickable="true"
android:text="id"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#666" />
<TextView
android:id="@+id/textFoodname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toLeftOf="@+id/textPrice"
android:layout_toRightOf="@id/ivFood"
android:clickable="true"
android:text="food name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:clickable="true"
android:text="price"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/colorAccent" />
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
MainActivity布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kelompok3.restaurantapp.restoranfixx.Isimenu.Appetizer">
<android.support.v7.widget.RecyclerView
android:id="@+id/lvaptzr"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:scrollbars="vertical"
android:visibility="visible" />
</RelativeLayout>
我不知道为什么,我无法点击该项目。当我点击该项目时,即使是吐司也没有显示出来。我做错了什么?
答案 0 :(得分:0)
setOnClickListener应该位于RecyclerView的onBindViewHolder方法中。
@Override
public void onBindViewHolder(RecyclerViewHolder holder, final int position) {
myholder.textFoodname.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DataFood clickedDataItem = data.get(position);
Intent intent = new Intent(context, Order.class);
intent.putExtra("nama", data.get(position).foodName);
intent.putExtra("harga", data.get(position).price);
intent.putExtra("gambar", data.get(position).foodImage);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Toast.makeText(v.getContext(),"clicked" + clickedDataItem.foodName, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(v.getContext(),"ga",Toast.LENGTH_SHORT).show();
}
});
}
答案 1 :(得分:0)
尝试以下
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final Myholder myholder = (Myholder) holder;
DataFood current = data.get(position);
myholder.textFoodname.setText(current.foodName);
myholder.textPrice.setText("Rp. " + current.price);
myholder.textId.setText(String.valueOf(current.foodId));
Glide.with(context).load(current.foodImage).asBitmap()
.into(myholder.ivFood);
myholder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DataFood clickedDataItem = data.get(position);
Intent intent = new Intent(context, Order.class);
intent.putExtra("nama", data.get(position).foodName);
intent.putExtra("harga", data.get(position).price);
intent.putExtra("gambar", data.get(position).foodImage);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Toast.makeText(v.getContext(), "clicked" + clickedDataItem.foodName, Toast.LENGTH_LONG).show();
}
});
}
并在Myholder类
上更新class Myholder extends RecyclerView.ViewHolder {
TextView textFoodname, textPrice, textId;
ImageView ivFood;
CardView cardView;
public Myholder(View view) {
super(view);
textFoodname = (TextView) view.findViewById(R.id.textFoodname);
ivFood = (ImageView) view.findViewById(R.id.ivFood);
textId = (TextView) view.findViewById(R.id.textid);
textPrice = (TextView) view.findViewById(R.id.textPrice);
cardView = (CardView) itemView.findViewById(R.id.cv);
}
}
答案 2 :(得分:0)
您已点击2,因此无法点击该项目。
^(\d+|\w+-\d+)$
注意:在你的情况下删除所有
答案 3 :(得分:0)
如果您使用recyclerView,您应该尝试&#34;回调接口&#34; ;
<强> AdapterFood.class 强>
public interface OnItemClickLitener {
void onItemClick(View view, int position);
}
private OnItemClickLitener mOnItemClickLitener;
public void setOnItemClickListener(OnItemClickLitener mOnItemClickLitener) {
this.mOnItemClickLitener = mOnItemClickLitener;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickLitener. onItemClick();
}
}
<强> Myholder 强>
class Myholder extends RecyclerView.ViewHolder {
TextView textFoodname, textPrice, textId;
ImageView ivFood;
CardView cardView;
public Myholder(View view) {
super(view);
textFoodname = (TextView) view.findViewById(R.id.textFoodname);
ivFood = (ImageView) view.findViewById(R.id.ivFood);
textId = (TextView) view.findViewById(R.id.textid);
textPrice = (TextView) view.findViewById(R.id.textPrice);
cardView = (CardView) itemView.findViewById(R.id.cv);
}
}
<强>菜强>
mAdapter.setOnItemClickListener(new CourseListAdp.OnItemClickLitener() {
@Override
public void onItemClick(View view, int position) {
//your work
int pos = position;
if (pos != RecyclerView.NO_POSITION){
**********
**********
}
}
}
希望能帮到你
答案 4 :(得分:0)
Change Youyr Layout File(remove clickable from yourlayout):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:background="#CCC"
>
<android.support.v7.widget.CardView
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_gravity="center"
android:id="@+id/ivFood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toEndOf="@id/ivFood"
android:layout_toRightOf="@id/ivFood"
android:clickable="true"
android:text="id"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#666" />
<TextView
android:id="@+id/textFoodname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toLeftOf="@+id/textPrice"
android:layout_toRightOf="@id/ivFood"
android:text="food name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="price"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/colorAccent" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Change Your Adapter :
public class AdapterFood extends RecyclerView.Adapter<AdapterFood.CustomViewHolder> {
private Context context;
private LayoutInflater inflater;
List<DataFood> data = Collections.emptyList();
public AdapterFood(Context context, List<DataFood> data) {
this.context = context;
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public AdapterFood.CustomViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
DataFood current = data.get(position);
holder.textFoodname.setText(current.foodName);
holder.textPrice.setText("Rp. " + current.price);
holder.textId.setText(String.valueOf(current.foodId));
// Log.d("IMAGE_URL", "http://kelompokdua.hol.es/pic/" + current.foodImage);
Picasso.with(context).load("http://kelompokdua.hol.es/pic/" + current.foodImage).into(myholder.ivFood);
Glide.with(context).load(current.foodImage).asBitmap()
.placeholder(R.mipmap.ic_launcher).dontAnimate()
.error(R.mipmap.minus)
.into(myholder.ivFood);
}
@Override
public int getItemCount() {
return data.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView textFoodname, textPrice, textId;
ImageView ivFood;
public CustomViewHolder(View itemView) {
super(itemView);
textFoodname = (TextView) itemView.findViewById(R.id.textFoodname);
ivFood = (ImageView) itemView.findViewById(R.id.ivFood);
textId = (TextView) itemView.findViewById(R.id.textid);
textPrice = (TextView) itemView.findViewById(R.id.textPrice);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Toast.makeText(context, "ga" + getAdapterPosition(), Toast.LENGTH_SHORT).show();
int pos = getAdapterPosition();
if (pos != RecyclerView.NO_POSITION) {
DataFood clickedDataItem = data.get(pos);
Intent intent = new Intent(context, Order.class);
intent.putExtra("nama", data.get(pos).foodName);
intent.putExtra("harga", data.get(pos).price);
intent.putExtra("gambar", data.get(pos).foodImage);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Toast.makeText(v.getContext(), "clicked" + clickedDataItem.foodName, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(v.getContext(), "ga", Toast.LENGTH_SHORT).show();
}
}
}