package com.bawei.fenlei.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.bawei.fenlei.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
/**
-
@Author:苏羡c
-
@E-mail:
-
@Date:2019/3/23 14:10
-
@Description:描述信息
*/
public class LeftRecyAdapter extends RecyclerView.Adapter<LeftRecyAdapter.ViewHolder> {
Context context;
String result;
private HashMap<String,String> hashMap= new HashMap<>();public LeftRecyAdapter(Context context, String result) {
this.context = context;
this.result = result;
}@NonNull
@Override
public LeftRecyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.left_view, null);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}@Override
public void onBindViewHolder(@NonNull LeftRecyAdapter.ViewHolder viewHolder, final int i) {try { JSONObject jsonObject = new JSONObject(result); JSONArray result = jsonObject.getJSONArray("result"); final String id = result.getJSONObject(i).getString("id"); String name = result.getJSONObject(i).getString("name"); viewHolder.left_text.setText(id); viewHolder.left_text.setText(name); viewHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (recyclerViewListener!=null){ recyclerViewListener.onclick(id); } } }); } catch (JSONException e) { e.printStackTrace(); }
}
@Override
public int getItemCount() {
return result.length();
}public class ViewHolder extends RecyclerView.ViewHolder {
private TextView left_text; public ViewHolder(@NonNull View itemView) { super(itemView); left_text = itemView.findViewById(R.id.left_text); }
}
public interface RecyclerViewListener{
void onclick(String id);
}
RecyclerViewListener recyclerViewListener;public void setRecyclerViewListener(RecyclerViewListener recyclerViewListener) {
this.recyclerViewListener = recyclerViewListener;
}
}