Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

java - 没有出现RecyclerView对象(RecyclerView object doesn't appear)

I have problem with recycler view which my object on JSONobject doesn't appear.

(我在Recycler视图中遇到问题,该问题在JSONobject上没有出现。)

i dont understand why my object doesn't appear.

(我不明白为什么我的对象没有出现。)

我想与我的对象一起显示recylerview

And here's the Main Fragment code

(这是主要片段代码)

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

Here the Adapter Class

(这里是适配器类)

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

       }
    }
 }

Here the DataACRekomen Class

(这里是DataACRekomen类)

public class DataACRekomen {
public String Nama_ACRekomen;
public String Merk_ACRekomen;
public String Tipe_ACRekomen;
public String Harga_ACRekomen;
public String img_ACRekomen;
}

And here the Layout code list_acrekomen.xml

(这里是布局代码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>

And the main xml to get the Recycler view id Fragment_tools.xml

(以及获取Recycler视图ID Fragment_tools.xml的主要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>

Please help me, thanks

(请帮我,谢谢)

  ask by Sugi Almantara translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You set RecyclerView item count to 0 in your ListAdapterRekomendasi adapter which cause the problem.

(您将导致问题的ListAdapterRekomendasi适配器中的RecyclerView项目计数设置为0。)

You have to return total no of object from your getItemCount .

(您必须从getItemCount返回对象总数。)

Try using

(尝试使用)

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

instead of

(代替)

@Override
public int getItemCount() {
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...