1

I have pictures from my online database in my GridView and I want it in reverse order. So I want when I add a new picture to my database to be the first in the GridView.

I tried to find an answer but there is nothing about it at stackoverflow.

This is my ListViewAdapter:

public class GetMovieImagesListViewAdapter extends BaseAdapter {

private JSONArray dataArray;
private Activity activity;

private static final String baseUrlForImage = "http://google.com";

private static LayoutInflater inflater = null;

public GetMovieImagesListViewAdapter(JSONArray jsonArray, Activity a){

    this.dataArray = jsonArray;
    this.activity = a;

    inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}


@Override
public int getCount() {
    return this.dataArray.length();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final ListCell cell;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.get_images_from_movies_list_cell, null);
        cell = new ListCell();

        cell.MovieImages = (ImageView) convertView.findViewById(R.id.movie_images_id);

        convertView.setTag(cell);
    } else {
        cell = (ListCell) convertView.getTag();
    }

    try {
        JSONObject jsonObject = this.dataArray.getJSONObject(position);

        String nameOfImage = jsonObject.getString("image");

        String urlForImageInServer = baseUrlForImage + nameOfImage;

        new AsyncTask<String, Void, Bitmap>(){

            @Override
            protected Bitmap doInBackground(String... params)
            {
                String url = params[0];
                Bitmap icon = null;

                try
                {
                    InputStream in = new URL(url).openStream();
                    icon = BitmapFactory.decodeStream(in);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return icon;
            }

            @Override
            protected void onPostExecute(Bitmap result)
            {
                cell.MovieImages.setImageBitmap(result);
            }
        }.execute(urlForImageInServer);


    } catch (JSONException e) {
        e.printStackTrace();
    }
    return convertView;

}
private class ListCell{

    private ImageView MovieImages;
}}

2 Answers2

0

You could fetch each JSONObject by starting from the end of the array and working backwards to the start:

JSONObject jsonObject = this.dataArray.getJSONObject(getCount() - position - 1);
mjp66
  • 4,214
  • 6
  • 26
  • 31
  • you do realize that this instantly crashes with an index out of bounds, right? – njzk2 Jan 15 '15 at 19:49
  • Thanks njzk2, made an edit. Should work now, I think. – mjp66 Jan 15 '15 at 19:59
  • It works now. But I don't know why pictures are changing like 30 seconds (1st picture is 3rd, then it's last, second picture is last, then it's first, etc) and then they get in the correct order. – Stefan Zupanek Jan 15 '15 at 20:06
  • Was it also behaving this way before? I don't quite understand what you wrote, does this happen only while you scroll the list? You might need to set a placeholder image for each MovieImages before the actual image gets fetched remotely, but I can't be sure. – mjp66 Jan 15 '15 at 20:23
  • It happens only when I'm scrolling. – Stefan Zupanek Jan 15 '15 at 20:27
  • Try "resetting" MovieImages before executing your AsyncTask... cell.MovieImages.setImageBitmap(null); If that doesn't help then I'm afraid you'll have to post a new question, since we're getting away from the original issue here. – mjp66 Jan 15 '15 at 20:35
0

The result that you have received try to iterate through it in the reverse order. Your Bitmap result data seems to be a single data that you get. I am expecting an array, or arraylist of data that you get in onPostExecute() method parameter while you query your online database. Say for example, if you get the data in the form of onPostExecute(ArrayList result), then you can simply iterate the 'result' data in reverse order in order to populate your grid view in reverse order.

Check out Iterate arraylist in reverse order

Community
  • 1
  • 1
akash89
  • 881
  • 3
  • 12
  • 31