Android RecylerView添加按钮给我错误

时间:2016-08-01 19:58:43

标签: java android button android-recyclerview onclicklistener

我正在尝试添加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);

}

}

3 个答案:

答案 0 :(得分:0)

我没有看到您的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();
        }
    });

}

答案 1 :(得分:0)

这是因为您从未夸大ViewHolder类中的按钮。记住应该如何&#34;找到视图&#34;在java中使用视图时lol?那样做。

答案 2 :(得分:0)

希望这有助于您

        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();
        }

        //Step 1:  Implement View.OnClickListener as below
        public static class urunlistem extends RecyclerView.ViewHolder implements View.OnClickListener{
            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);

                //Step 2:  Register Your Button here below
                sepete_ekle=(Button)itemView.findViewById(R.id.button);                     
                sepete_ekle.setOnClickListener(this);

            }

            @Override
            public void onClick(View v) {
                    // Step 3: Redirect to next Activity From here 
                    Intent intent = new Intent(contexto, NextActivity.class);
                    contexto.startActivity(intent);
            }

        }

        }