OnClick操作在回收站网格布局中出错

时间:2016-10-14 11:12:48

标签: android android-recyclerview

这是我的MainActivity Code.In这我们在setOnItemClickListener中得到错误作为空指针异常并且它停止了我的app.give我一些建议我错了。我正在尝试使用顶部的图像和旁边的标题来实现自定义网格。

public class MainActivity extends AppCompatActivity  {

//RecyclerView recyclerView;
private static final String TAG = "MainActivity";

    private final String android_version_names[] = {
            "Donut",
            "Eclair",
            "Froyo",
            "Gingerbread",
            "Honeycomb",
            "Ice Cream Sandwich",
            "Jelly Bean",
            "KitKat",
            "Lollipop",
            "Marshmallow"
    };

    private final String android_image_urls[] = {
            "https://api.learn2crack.com/android/images/donut.png",
            "https://api.learn2crack.com/android/images/eclair.png",
            "https://api.learn2crack.com/android/images/froyo.png",
            "https://api.learn2crack.com/android/images/ginger.png",
            "https://api.learn2crack.com/android/images/honey.png",
            "https://api.learn2crack.com/android/images/icecream.png",
            "https://api.learn2crack.com/android/images/jellybean.png",
            "https://api.learn2crack.com/android/images/kitkat.png",
            "https://api.learn2crack.com/android/images/lollipop.png",
            "https://api.learn2crack.com/android/images/marshmallow.png"
    };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    initViews();


}

private void initViews(){
    RecyclerView recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),2);
    recyclerView.setLayoutManager(layoutManager);

    ArrayList<AndroidVersion> androidVersions = prepareData();
    DataAdapter adapter = new DataAdapter(getApplicationContext(),androidVersions);
    recyclerView.setAdapter(adapter);

    DataAdapter mAdapter = null ;

    mAdapter.setOnItemClickListener(new DataAdapter.ClickListener() {
        @Override
        public void onItemClick(int position, View v) {

            Log.d(TAG, "onItemClick position: " + position);
        }

    });


}
private ArrayList<AndroidVersion> prepareData(){

    ArrayList<AndroidVersion> android_version = new ArrayList<>();
    for(int i=0;i<android_version_names.length;i++){
    AndroidVersion androidVersion = new AndroidVersion();
    androidVersion.setAndroid_version_name(android_version_names[i]);
    androidVersion.setAndroid_image_url(android_image_urls[i]);
    android_version.add(androidVersion);
    }
    return android_version;
    }

    }

这是我的适配器代码。

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<AndroidVersion> android;
private Context context;
private static ClickListener clickListener;

public DataAdapter(Context context,ArrayList<AndroidVersion> android) {
    this.android = android;
    this.context = context;
}

@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {

    viewHolder.tv_android.setText(android.get(i).getAndroid_version_name());
    Picasso.with(context).load(android.get(i).getAndroid_image_url()).resize(240, 120).into(viewHolder.img_android);
}

@Override
public int getItemCount() {
    return android.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    private TextView tv_android;
    private ImageView img_android;
    public ViewHolder(View view) {
        super(view);
        view.setOnClickListener(this);




        tv_android = (TextView)view.findViewById(R.id.tv_android);
        img_android = (ImageView) view.findViewById(R.id.img_android);
    }
    @Override
    public void onClick(View v) {
        clickListener.onItemClick(getAdapterPosition(), v);
    }
}
public void setOnItemClickListener(ClickListener clickListener) {
    DataAdapter.clickListener = clickListener;
}

public interface ClickListener {
    void onItemClick(int position, View v);

}

}

2 个答案:

答案 0 :(得分:1)

您在设置mAdapter = null之前设置setOnItemClickListener,将其删除

DataAdapter mAdapter = null ; //remove this line

adapter.setOnItemClickListener(new DataAdapter.ClickListener() {
    @Override
    public void onItemClick(int position, View v) {

        Log.d(TAG, "onItemClick position: " + position);
    }

});

您正在调用错误的适配器,您的实际变量为adapter,并且您使用mAdapter调用它,并将其值设置为ull

答案 1 :(得分:0)

试试这个

DataAdapter adapter = new DataAdapter(getApplicationContext(),androidVersions);
recyclerView.setAdapter(adapter);


adapter.setOnItemClickListener(new DataAdapter.ClickListener() {
    @Override
    public void onItemClick(int position, View v) {

        Log.d(TAG, "onItemClick position: " + position);
    }

});