我正在尝试添加一个Button
,单击该按钮时应打开一个新的Activity
。尽管我为此花费了很长时间,但我的努力却没有成功。如果我添加 OnClickListener
并单击 Button
,则会出现错误。感谢您的帮助。
Listele.Java
package com.example.hp.myapplication;
import android.view.View;
import android.widget.Button;
public class Listele {
private String adi;
private String fiyat;
private String aciklama;
private String stok;
private String resim;
public Listele(String adi, String fiyat, String aciklama, String stok,String resim) {
this.adi = adi;
this.fiyat = fiyat;
this.aciklama = aciklama;
this.stok = stok;
this.resim = resim;
}
//getters and setters
@Override
public String toString() {
return "Listele{" +
"adi='" + adi + '\'' +
", fiyat='" + fiyat + '\'' +
", aciklama='" + aciklama + '\'' +
", stok='" + stok + '\'' +
", resim='" + resim + '\'' +
'}';
}
}
ListeleAdapter.Java
package com.example.hp.myapplication;
//imports
public class ListeleAdapter extends RecyclerView.Adapter<ListeleAdapter.urunlistem> {
private Context contexto;
private ArrayList<Listele>tortica;
public ListeleAdapter(ArrayList<Listele> tortica, Context contexto ) {
this.contexto = contexto;
this.tortica = tortica;
}
@Override
public urunlistem onCreateViewHolder(ViewGroup parent, int viewType) {
return new urunlistem(LayoutInflater.from(parent.getContext()).inflate(R.layout.list,null));
}
@Override
public void onBindViewHolder(urunlistem holder, int position) {
holder.adi.setText(tortica.get(position).getAdi());
holder.aciklama.setText(tortica.get(position).getAciklama());
holder.stok.setText(tortica.get(position).getStok());
holder.fiyat.setText(tortica.get(position).getFiyat());
Glide.with(contexto).load("http://192.168.1.33:8080/urunler/"+tortica.get(position).getResim()).into(holder.tresim);
}
@Override
public int getItemCount() {
return tortica.size();
}
public static class urunlistem extends RecyclerView.ViewHolder{
ImageView tresim;
TextView adi,fiyat,stok,aciklama;
Button sepete_ekle;
ArrayList<Listele> listeles = new ArrayList<Listele>();
public urunlistem(View itemView) {
super(itemView);
tresim=(ImageView)itemView.findViewById(R.id.resim);
adi=(TextView)itemView.findViewById(R.id.adi_urun);
fiyat=(TextView)itemView.findViewById(R.id.fiyat);
stok=(TextView)itemView.findViewById(R.id.stok);
aciklama=(TextView)itemView.findViewById(R.id.aciklama);
}
}
}
MainActivity.Java
package com.example.hp.myapplication;
//imports
public class MainActivity extends AppCompatActivity {
private static final String url="http://192.168.1.33:8080/urunler/goruntule.php";
private RecyclerView recyclerView;
private ListeleAdapter adapter;
private ArrayList<Listele>listeles = new ArrayList<>();
ProgressDialog progressDialog;
RequestQueue requestQu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestQu = Volley.newRequestQueue(getApplicationContext());
goster();
}
private void goster() {
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Bilgiler alınıyor");
recyclerView=(RecyclerView) findViewById(R.id.listado);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.HORIZONTAL,false));
recyclerView.setHasFixedSize(true);
adapter = new ListeleAdapter(listeles,this);
recyclerView.setAdapter(adapter);
JsonArrayRequest request = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for(int i=0;i<response.length();i++)
{
try {
JSONObject object = response.getJSONObject(i);
String resimurl=object.getString("resim");
String adi=object.getString("adi");
String stok=object.getString("stok");
String fiyat=object.getString("fiyat");
String aciklama=object.getString("aciklama");
listeles.add(new Listele(adi,fiyat,aciklama,stok,resimurl));
} catch (JSONException e) {
Toast.makeText(MainActivity.this,""+e,Toast.LENGTH_LONG).show();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQu.add(request);
}
}
最佳答案
我没有看到您的 logcat,但首先您将 OnClickListener
添加到 Button
的错误位置。将其添加到 onBindViewHolder
而不是 ViewHolder
构造函数中。在你的代码中这样:
@Override
public void onBindViewHolder(urunlistem holder, int position) {
holder.adi.setText(tortica.get(position).getAdi());
holder.aciklama.setText(tortica.get(position).getAciklama());
holder.stok.setText(tortica.get(position).getStok());
holder.fiyat.setText(tortica.get(position).getFiyat());
Glide.with(contexto).load("http://192.168.1.33:8080/urunler/"+
tortica.get(position).getResim()).into(holder.tresim);
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String salutation="Merhaba | Hello | Здравствуйте | Hallo | Salut";
Toast.makeText(mContext,salutation,Toast.LENGTH_SHORT).show();
}
});
}
关于java - Android RecyclerView 添加按钮给我错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38707120/