我正在处理使用排球获取数据并使用RecyclerView
在Feed中显示的应用。我在每张卡片中都有一个Button
,点击后会计算点击次数并将其显示在{/ 1}}在该特定卡片中**(如Instagram按钮)**。
当我运行它时,应用程序崩溃了。
我究竟做错了什么?
这是我的代码
TextView
答案 0 :(得分:1)
package net.simplifiedcoding.myfeed;
import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Belal on 11/9/2015.
*/
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
//Imageloader to load image
private ImageLoader imageLoader;
private Context context;
//List to store all superheroes
List<SuperHero> superHeroes;
//Constructor of this class
public CardAdapter(List<SuperHero> superHeroes, Context context) {
super();
//Getting all superheroes
this.superHeroes = superHeroes;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.superheroes_list, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder( final ViewHolder holder, final int position) {
//Getting the particular item from the list
SuperHero superHero = superHeroes.get(position);
//Loading image from url
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView, R.drawable.image, android.R.drawable.ic_dialog_alert));
//Showing data on the views
holder.imageView.setImageUrl(superHero.getImageUrl(), imageLoader);
holder.textViewName.setText(superHero.getName());
holder.textViewPublisher.setText(superHero.getPublisher());
holder.textViewLikes.setText(superHero.getLikes());
holder.custom_button.setOnClickListener(new PlusListener(position,superHero,holder.txtCount));
holder.txtCount.setText(superHeroes.get(position).getCount());
}
@Override
public int getItemCount() {
return superHeroes.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
//Views
public NetworkImageView imageView;
public TextView textViewName;
public TextView textViewPublisher;
public TextView textViewLikes;
public TextView txtCount;
public ImageButton custom_button;
//Initializing Views
public ViewHolder(View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
textViewName = (TextView) itemView.findViewById(R.id.textViewName);
textViewPublisher = (TextView) itemView.findViewById(R.id.textViewPublisher);
textViewLikes = (TextView) itemView.findViewById(R.id.textViewlikes);
txtCount = (TextView) itemView.findViewById(R.id.txtCount);
custom_button = (ImageButton) itemView.findViewById(R.id.custom_button);
}
}
}
class PlusListener implements View.OnClickListener
{
SuperHero Data;
int position;
TextView txtCount;
Listener(int position,SuperHero Data,TextView txtCount)
{
this.Data = Data;
this.position = position;
this.txtCount= txtCount
}
@Override
public void onClick(View view) {
int count = data.getCount();
count = count+1;
data.setCount(count);
txtCount.setText(""+count);
}
}
class MinusListener implements View.OnClickListener
{
SuperHero Data;
int position;
TextView txtCount;
Listener(int position,SuperHero Data,TextView txtCount)
{
this.Data = Data;
this.position = position;
this.txtCount= txtCount
}
@Override
public void onClick(View view) {
int count = data.getCount();
if(count>0)
{
count = count-1;
data.setCount(count);
txtCount.setText(""+count);
}
}
}
public class SuperHero {
//Data Variables
private String imageUrl;
private String name;
private String publisher;
private String likes;
private int count;
//Getters and Setters
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getLikes() {
return likes;
}
public void setLikes(String likes) {
this.likes = likes;
}
public int getCount() {
return count ;
}
public void setCount(int count) {
this.count = count;
}
}
答案 1 :(得分:1)
只是简单地这样做
替换此行
holder.txtCount.setText(superHeroes.get(position).getCount());
用这个
holder.txtCount.setText(String.valueOf(superHeroes.get(position).getCount()));
你的应用程序崩溃了,因为textView从不设置Integer值。