我对回收器视图有问题,该对象在JSONobject上没有出现。 我不明白为什么我的对象没有出现。
这是主要片段代码
private RecyclerView recyclerView;
public ListAdapterRekomendasi listAdapter;
List<DataACRekomen> data = new ArrayList<>();
private static String url_viewAC = "http://arismart.co.id/sugiklib/select_rekomendasi.php";
public static final int CONN_TIMEOUT = 10000;
public static final int CONN_READOUT = 15000;
int hasil;
public RekomendasiACFragment(){
}
public View onCreateView(@NonNull final LayoutInflater inflater,
final ViewGroup container, Bundle savedInstanceState) {
final View[] root = {inflater.inflate(R.layout.fragment_tools, container, false)};
hasil = getArguments().getInt("hasil", -1);
new viewRekomen().execute();
return root[0];
}
private class viewRekomen extends AsyncTask<String, String, String>{
HttpURLConnection conn;
URL url = null;
@Override
protected String doInBackground(String... strings) {
try{
url = new URL(url_viewAC);
} catch (MalformedURLException e) {
e.printStackTrace();
return e.toString();
}
try{
conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(CONN_READOUT);
conn.setConnectTimeout(CONN_TIMEOUT);
conn.setRequestMethod("GET");
conn.setDoOutput(true);
} catch (IOException e1) {
e1.printStackTrace();
return e1.toString();
}
try{
int response_code = conn.getResponseCode();
if(response_code == HttpURLConnection.HTTP_OK){
InputStream inputStream = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine())!=null){
result.append(line);
}
return (result.toString());
}else {
return ("tidak sukses");
}
} catch (IOException e2) {
e2.printStackTrace();
return e2.toString();
}finally {
conn.disconnect();
}
}
@Override
protected void onPostExecute(String s) {
try {
JSONObject object = new JSONObject(s);
JSONArray jsonArray = object.getJSONArray("AC");
for (int i = 0; i < jsonArray.length();i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
DataACRekomen dataACRekomen = new DataACRekomen();
dataACRekomen.Nama_ACRekomen=jsonObject.getString("nama_ac");
dataACRekomen.Merk_ACRekomen=jsonObject.getString("merk_ac");
dataACRekomen.Tipe_ACRekomen=jsonObject.getString("tipe_ac");
dataACRekomen.Harga_ACRekomen=jsonObject.getString("harga");
data.add(dataACRekomen);
}
recyclerView = (RecyclerView)getActivity().findViewById(R.id.rlListACRekomen);
listAdapter = new ListAdapterRekomendasi(getActivity(), data);
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(listAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
这是适配器类
public class ListAdapterRekomendasi extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
List<DataACRekomen> data = Collections.emptyList();
int temp;
private static LayoutInflater inflater = null;
public ListAdapterRekomendasi(Context context, List<DataACRekomen> dataACS){
this.context = context;
inflater =LayoutInflater.from(context);
this.data = dataACS;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.from(parent.getContext()).inflate(R.layout.list_acrekomen, parent, false);
ListViewHolder holder = new ListViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
//((ListViewHolder) holder ).bindView(position);
DecimalFormat decim = new DecimalFormat("#,###.##");
final ListViewHolder holder1 =(ListViewHolder) holder;
final DataACRekomen dataACRekomen = data.get(position);
holder1.tvnama.setText(dataACRekomen.Nama_ACRekomen);
holder1.tvmerk.setText(dataACRekomen.Merk_ACRekomen);
holder1.tvtipe.setText(dataACRekomen.Tipe_ACRekomen);
temp = Integer.parseInt(dataACRekomen.Harga_ACRekomen);
holder1.tvharga.setText("Rp. "+decim.format(temp));
}
@Override
public int getItemCount() {
return 0;
}
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
private class ListViewHolder extends RecyclerView.ViewHolder {
TextView tvnama, tvmerk, tvharga, tvtipe;
ImageView iv;
public ListViewHolder(@NonNull View itemView) {
super(itemView);
tvnama = itemView.findViewById(R.id.namaACre);
tvmerk = itemView.findViewById(R.id.merkACre);
tvtipe = itemView.findViewById(R.id.tipeACre);
tvharga = itemView.findViewById(R.id.hargaACre);
iv = itemView.findViewById(R.id.imgACre);
}
}
}
这里是 DataACRekomen类
public class DataACRekomen {
public String Nama_ACRekomen;
public String Merk_ACRekomen;
public String Tipe_ACRekomen;
public String Harga_ACRekomen;
public String img_ACRekomen;
}
这里是布局代码 list_acrekomen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<ImageView
android:id="@+id/imgACre"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="0.2"
android:scaleType="fitCenter"
android:src="@mipmap/ic_home" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:orientation="vertical"
android:weightSum="0.8">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/namaACre"
android:layout_weight="0.2"
android:text="Nama AC"
android:textSize="6pt"
android:layout_marginLeft="10dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/merkACre"
android:layout_weight="0.2"
android:text="Merk AC"
android:textSize="6pt"
android:layout_marginLeft="10dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tipeACre"
android:layout_weight="0.2"
android:text="Tipe AC"
android:textSize="6pt"
android:layout_marginLeft="10dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/hargaACre"
android:layout_weight="0.2"
android:text="Harga AC"
android:textSize="6pt"
android:layout_marginLeft="10dp" />
</LinearLayout>
</LinearLayout>
以及用于获取Recycler视图ID的主xml Fragment_tools.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rlListACRekomen"/>
</LinearLayout>
请帮助我,谢谢
答案 0 :(得分:1)
您在RecyclerView
适配器中将ListAdapterRekomendasi
项计数设置为0,这会导致问题。您必须从getItemCount
返回对象总数。
尝试使用
@Override
public int getItemCount() {
return data.size();
}
代替
@Override
public int getItemCount() {
return 0;
}
答案 1 :(得分:1)
问题是在适配器中,getItemCount()
返回0。
您需要返回数据集中的项目总数。
public class ListAdapterRekomendasi extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
@Override
public int getItemCount() {
return this.data.size();
}
}