D
D
Denis Sokolov2021-11-18 20:22:02
Java
Denis Sokolov, 2021-11-18 20:22:02

Why doesn't anything appear in the list?

I get all the data, but for some reason they are not displayed for me

public class MainActivity extends AppCompatActivity {
    private VideotapeRecordingsAdapter mAdapter;
    private List<VideotapeRecordings> mVideotapeRecordings = new ArrayList<>();

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

        ListView lvVideotapeRecordings = findViewById(R.id.listViewVideotapes);
        mAdapter = new VideotapeRecordingsAdapter(MainActivity.this, mVideotapeRecordings);
        lvVideotapeRecordings.setAdapter(mAdapter);

        new GetVideotapeRecordings().execute();
    }

    private class GetVideotapeRecordings extends AsyncTask<Void, Void, String>
    {
        @Override
        protected String doInBackground(Void... voids) {
            try
            {
                URL url = new URL("http://10.0.2.2:63599/api/VideotapesRecordings");
                HttpURLConnection connection = (HttpURLConnection)url.openConnection();

                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

                StringBuilder result = new StringBuilder();
                String line = "";

                while ((line = reader.readLine()) != null)
                {
                    result.append(line);
                }

                return result.toString();
            } catch (Exception ex) {
                return null;
            }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            try {
                JSONArray tempArray = new JSONArray(s);

                for (int i = 0; i < tempArray.length(); i++)
                {
                    JSONObject recordingJson = tempArray.getJSONObject(i);
                    VideotapeRecordings tempVideotapeRecording = new VideotapeRecordings(
                            recordingJson.getInt("id"),
                            recordingJson.getString("name"),
                            recordingJson.getString("category"),
                            recordingJson.getInt("year"),
                            recordingJson.getString("manufacturer"),
                            recordingJson.getInt("price"),
                            recordingJson.getString("image")
                    );

                    mVideotapeRecordings.add(tempVideotapeRecording);

                    mAdapter.notifyDataSetChanged();
                }
            } catch (Exception ex) {

            }
        }
    }
}

Adapter
public class VideotapeRecordingsAdapter extends BaseAdapter {
    private Context mContext;
    private List<VideotapeRecordings> mVideotapeRecordingsList;

    public VideotapeRecordingsAdapter(Context mContext, List<VideotapeRecordings> mVideotapeRecordingsList) {
        this.mContext = mContext;
        this.mVideotapeRecordingsList = mVideotapeRecordingsList;
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v = View.inflate(mContext, R.layout.item_videotape, null);

        ImageView imgSource = v.findViewById(R.id.imageViewItemVideotapeRecording);
        TextView textName = v.findViewById(R.id.textViewItemVideotapeRecordingName);
        TextView textYear = v.findViewById(R.id.textViewItemVideotapeRecordingYear);

        VideotapeRecordings currentVideotapeRecording = mVideotapeRecordingsList.get(i);

        imgSource.setImageBitmap(currentVideotapeRecording.getBitmapSource());
        textName.setText(currentVideotapeRecording.getName());
        textYear.setText(currentVideotapeRecording.getYear());

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentDetails = new Intent(mContext, DetailsActivity.class);
                intentDetails.putExtra("videotapeRecording", currentVideotapeRecording);

                mContext.startActivity(intentDetails);
            }
        });

        return v;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jacen11, 2021-11-19
@Jacen11

oh, how many questions I have.
What does java do in android development?
why is AsyncTask used? why is not a recycler used?
why does the method that should return the number of elements always return 0?
why does a method that should return an element always return cash?
why does the method that should return the ID always return 0?
and most importantly, why did you decide that when you always return nothing, something should work for you?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question