0

I am using the volley library for the networking side. I am trying to get the image to be loaded onto the next screen when I click on an item on the list. In the XML file, I am using the "NetworkImageView" to hold the image. I am using intent to pass the views across the description page.

Thanks in Advance

Here is the code:

List Activity This activity shows the list of items that is stored in the database

public class HallListActivity extends AppCompatActivity
    implements SearchView.OnQueryTextListener, AdapterView.OnItemClickListener {

// Log tag
private static final String TAG = HallListActivity.class.getSimpleName();

// Movies json url
private static final String url = "http://10.0.2.2/wedding1/halls.php";
private ProgressDialog progressDialog;
private List<Hall> hallList = new ArrayList<Hall>();
private ListView listView;
private HallAdapter adapter;
private Hall h;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hall_list);
    getSupportActionBar().setTitle("List of Halls");

    listView = (ListView) findViewById(R.id.list);
    adapter = new HallAdapter(this, hallList);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(this);

    // Create a new progress dialog
    progressDialog = new ProgressDialog(this);
    // Showing progress dialog before making http request
    progressDialog.setMessage("Loading...");
    progressDialog.show();


    // Creating volley request obj
    //String url = "http://10.0.2.2/wedding1/";
    JsonArrayRequest hallReq = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            // Parsing json
            for (int i = 0; i < response.length(); i++) {
                try {
                    JSONObject obj = response.getJSONObject(i);
                    Hall hall = new Hall();
                    hall.setThumbnailUrl(obj.getString("image"));
                    hall.setTitle(obj.getString("name"));
                    hall.setDescription(obj.getString("description"));
                    hall.setLocation(obj.getString("location"));
                    hall.setPrice(obj.getString("price"));

                    // adding hall to movies array
                    hallList.add(hall);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            progressDialog.dismiss(); // progress bar disappears once the halls are loaded on screen

            /* notifying list adapter about data changes so that it renders the list view with updated data */
            adapter.notifyDataSetChanged();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            hideProgressDialog();
        }
    });

    // Adding request to request queue
    MySingleton.getInstance().addToRequestQueue(hallReq);

}

@Override
public void onDestroy() {
    super.onDestroy();
    hideProgressDialog();
}

private void hideProgressDialog() {
    if (progressDialog != null) {
        progressDialog.dismiss();
        progressDialog = null;
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.refresh:
            startActivity(new Intent(getApplicationContext(), HallListActivity.class));
            break;
    }
    return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    MenuItem menuItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
    searchView.setOnQueryTextListener(this); // The onQueryTextChange method will invoke
    return true;
}

@Override
public boolean onQueryTextSubmit(String query) {

    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    newText = newText.toLowerCase();
    ArrayList<Hall> newList = new ArrayList<>();
    for (Hall hall : hallList) {
        String name = hall.getTitle().toLowerCase();
        if (name.contains(newText))
            newList.add(hall);
    }
    return true;
}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    h = hallList.get(position);
    // shows the hall name at the bottom once the hall is clicked
    Toast.makeText(HallListActivity.this, h.getTitle(), Toast.LENGTH_SHORT).show();
    Intent intentDescription = new Intent(HallListActivity.this, HallDescriptionActivity.class);

    Log.d(TAG, "onItemClick: title " + h.getTitle());

    intentDescription.putExtra("title", h.getTitle());
    intentDescription.putExtra("image", h.getThumbnailUrl());
    intentDescription.putExtra("description", h.getDescription());
    intentDescription.putExtra("location", h.getLocation());
    intentDescription.putExtra("price", String.valueOf(h.getPrice()));

    intentDescription.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intentDescription);

}
}

Description Activity This activity shows the details of the item selected in the previous activity

public class HallDescriptionActivity extends AppCompatActivity implements View.OnClickListener {

private static final String TAG = HallDescriptionActivity.class.getSimpleName();

TextView tvTitle, tvDesc, tvLocation, tvPrice;
NetworkImageView ivImage;
Button btnReserve;
ImageLoader imageLoader;
Hall h;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hall_description);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    imageLoader = MySingleton.getInstance().getImageLoader();
    // Initialize the views
    tvTitle = (TextView) findViewById(R.id.tvTitle);
    ivImage = (NetworkImageView) findViewById(R.id.ivImage);
    tvDesc = (TextView) findViewById(R.id.tvDesc);
    tvLocation = (TextView) findViewById(R.id.tvLocation);
    tvPrice = (TextView) findViewById(R.id.tvPrice);

    btnReserve = (Button) findViewById(R.id.btnReserve);
    btnReserve.setOnClickListener(this);

    try {
        Intent iDescription = getIntent();

        h = new Hall();
        Log.d(TAG, "onCreate: hall " + h);
        // The text and image from the list page is passed onto this page using intents
        h.setTitle(iDescription.getStringExtra("title"));
        //Get the result of image
        h.setThumbnailUrl(iDescription.getStringExtra("image"));
        //Get the result of description
        h.setDescription(iDescription.getStringExtra("description"));
        //Get the result of location
        h.setLocation(iDescription.getStringExtra("location"));
        //Get the result of price
        h.setPrice(iDescription.getStringExtra("price"));

        // The views then set to their appropriate views
        tvTitle.setText(h.getTitle());
        ivImage.setImageUrl(h.getThumbnailUrl(), imageLoader);
        tvDesc.setText(h.getDescription());
        tvLocation.setText(h.getLocation());
        tvPrice.setText("£" + h.getPrice());

    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}

@Override
public void onClick(View v) {
    if(v == btnReserve) {
        startActivity(new Intent(getApplicationContext(), OrderActivity.class));
    }
}
}

Adapter class

public class HallAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Hall> hallItems;
ImageLoader imageLoader = MySingleton.getInstance().getImageLoader();

public HallAdapter(Activity activity, List<Hall> hallItems) {
    this.activity = activity;
    this.hallItems = hallItems;
}

// Lets tge ListView know how many items to display (returns the size of the data source
@Override
public int getCount() {
    return hallItems.size();
}

// returns an item to be placed in a given position of the data source
@Override
public Object getItem(int position) {
    return hallItems.get(position);
}

// defines a unique ID for each row in the list
// Use the position of the item as its ID for simplicity
@Override
public long getItemId(int position) {
    return position;
}

// Creates the view to be used as a row in the list.
// Defining the information and where it is placed in the ListView.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (inflater == null)
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_item, null);

    // image loader is used to check if the permissions are granted or not
    if (imageLoader == null)
        imageLoader = MySingleton.getInstance().getImageLoader();

    // Thumbnail
    NetworkImageView thumbnail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
    // Hall name
    TextView title = (TextView) convertView.findViewById(R.id.title);
    // Rating
    TextView description = (TextView) convertView.findViewById(R.id.description);
    // genre
    TextView location = (TextView) convertView.findViewById(R.id.location);
    // year
    TextView price = (TextView) convertView.findViewById(R.id.price);

    // getting hall data for each row
    //Hall h = hallItems.get(position);
    Hall h = (Hall) getItem(position);
    // Thumbnail image
    String url = "http://10.0.2.2/wedding1/";
    thumbnail.setImageUrl(url + h.getThumbnailUrl(), imageLoader);
    // Title
    title.setText(h.getTitle());
    // Description
    description.setText(String.valueOf(h.getDescription()));
    // Location
    location.setText(String.valueOf(h.getLocation()));
    // Price
    price.setText("£" + String.valueOf(h.getPrice()));

    return convertView;
}
}
hhh
  • 23
  • 1
  • 1
  • 4
  • I suspect that an exception is thrown in the try...catch block of the `onCreate()` method of your detail activity. Since you print the stacktrace manually, your app does not crash. You need to be careful when doing this as an exception can leave your program in an unstable state. In such a case, crashing is much better than continuing to run your code with bad results. You need to think more carefully about your exception handling. With that said, please post the stacktrace here so that we can help you further. – Code-Apprentice Apr 25 '17 at 23:38

1 Answers1

0

https://www.simplifiedcoding.net/picasso-android-tutorial-picasso-image-loader-library/

try this - i think you are getting image as url if so save it in an arraylist. - make the arraylist static so you can call same arraylist in any activity - use picasso image loader in activity u like.

Jins Lukose
  • 699
  • 6
  • 19