-2

I created custom listview to display three blocks of textviews and an imageview. All of which are fetched from Parse.com. The string data is fetched properly but only one image is getting fetched in a session. Sometimes, no images are fetched. But the images show up only in the last position of the listview. I am unable to sort it out.

This is the snippet in my main2activity.java which fetches all the data from parse.com.

    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Students");
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> parseObjects, ParseException e) {
            if(e==null) {
                studentsList= new ArrayList<Students>();
                for(ParseObject ob: parseObjects) {

                    s= new Students();

                    s.setName(ob.getString("Name").toString());


                    s.setUSN(ob.getString("USN").toString());


                    s.setSemester(ob.getString("Semester").toString());
                    ParseFile file = (ParseFile) ob.getParseFile("Image");
                    file.getDataInBackground(new GetDataCallback() {
                        @Override
                        public void done(byte[] bytes, ParseException e) {
                            if(e == null) {
                                Bitmap bmp = BitmapFactory.decodeByteArray( bytes, 0, bytes.length);
                                s.setPic(bmp);
                            } else {
                              Log.e(TAG,"Error while file fetching");
                            }
                        }
                    });
                    studentsList.add(s);
                  }
                adapter = new CustomAdapter(Main2Activity.this, studentsList);
                lv.setAdapter(adapter);

            } else {
                Toast.makeText(Main2Activity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
            }

        }
    });

This is the custom adapter.

public class CustomAdapter extends BaseAdapter{
private final static String TAG = CustomAdapter.class.getSimpleName();

private Context activity;
private LayoutInflater inflater = null;
private List<Students> student;
int layout;


   public CustomAdapter(Context activity, List<Students> Student) {

    this.activity = activity;
    this.student = Student;
    inflater = LayoutInflater.from(activity);
    }

    @Override
    public int getCount() {
    return student.size();
    }

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

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

    public  class ViewHolder {
    //ImageView imageView ;
    TextView name  ;
    TextView usn ;
    ImageView imageView;
    TextView semester  ;
    }

   @Override
   public View getView(int position, View view, ViewGroup parent) {
    View v =view;
    ViewHolder holder = new ViewHolder();

    if (view == null) {
    LayoutInflater li = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.list_item_layout,null);
        holder.name = (TextView)v.findViewById(R.id.name);
        holder.usn = (TextView)v.findViewById(R.id.usn);
        holder.semester = (TextView)v.findViewById(R.id.semester);
        holder.imageView = (ImageView)v.findViewById(R.id.imageView);
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }

      Students s = new Students();

      s = student.get(position);
      String a =  s.Name;
      Log.d(TAG,a);
      holder.name.setText(s.getName());
      holder.usn.setText(s.getUSN());
      holder.semester.setText(s.getSemester());
      holder.imageView.setImageBitmap(s.getPic());

      Log.d("CustomAdapter.class", "CustomAdapter");

   // imageView.setImageDrawable(s.getPic());
      return v;
    }


 } 

And this is list_item_layout.xml.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.redux.kumardivyarajat.parsedemo.Main2Activity"
android:background="#8003cbff">


  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="New Text"
      android:id="@+id/name"
      android:textColor="#80000000"
      android:textSize="25sp"
      android:textStyle="bold"
      android:layout_alignParentTop="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true" />

  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="New Text"
      android:id="@+id/usn"
      android:textSize="35sp"
      android:textColor="#9bffa068"
      android:textStyle="bold|italic"
      android:layout_alignTop="@+id/imageView"
      android:layout_alignRight="@+id/checkBox"
      android:layout_alignEnd="@+id/checkBox" />

  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="New Text"
      android:id="@+id/semester"
      android:textSize="15sp"
      android:textColor="#c8ff653a"
      android:layout_below="@+id/name"
      android:layout_toRightOf="@+id/imageView"
      android:layout_toEndOf="@+id/imageView" />

  <ImageView
      android:layout_width="100dp"
      android:layout_height="100dp"
      android:id="@+id/imageView"
      android:layout_below="@+id/semester"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_marginTop="42dp" />

  <CheckBox
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Absent"
      android:id="@+id/checkBox"
      android:textStyle="bold"
      android:textSize="20sp"
      android:layout_above="@+id/usn"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:layout_marginRight="42dp"
      android:layout_marginEnd="42dp" />

  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Semester-"
      android:id="@+id/textView"
      android:layout_below="@+id/name"
      android:layout_toLeftOf="@+id/semester"
      android:layout_toStartOf="@+id/semester"
      android:textSize="15sp" />
</RelativeLayout>
  • this is not a debugging service. do not just dump all your code and expect it to be debugged. – Scary Wombat Apr 16 '15 at 00:52
  • Bro. I am unable to sort out what is going wrong in the code. I just joined StackOverflow a week or so ago and started developing android apps 10 days ago. If asking someone what exactly is going wrong with the code is called "SPAMMING" as such, then what do the other 80% people on StackOverflow do. I thought all of them asked about their issues. So now for once please help me with this. I have to submit the project tomorrow. I have been up all night today. – Kumar Divya Rajat Apr 16 '15 at 01:04
  • hint: include some `System.out.println` statements in your code so that you can figure where you are going wrong. When you figure this out, and then don't know why it is happening then ask about it. BTW I am not your `Bro` – Scary Wombat Apr 16 '15 at 01:07

1 Answers1

-1
ParseFile file = (ParseFile) ob.getParseFile("Image");
                    file.getDataInBackground(new GetDataCallback() {
                        @Override
                        public void done(byte[] bytes, ParseException e) {
                            if(e == null) {
                                Bitmap bmp = BitmapFactory.decodeByteArray( bytes, 0, bytes.length);
                                s.setPic(bmp);
                            } else {
                              Log.e(TAG,"Error while file fetching");
                            }
                        }
                    });
                    studentsList.add(s);

File is downloading as background job, before it gets download the loop will be finished and the bitmap wont store in student object.so, try to change the code and solve the issue.

May be u can try as follows In Students class, create a url string.

Don't download the image from ParseFile, just get the file.getUrl() and add to the student object.

In adapter class, Use image caching libraries and finish your job. libraries like Picasso, Glide, Volley, UIL or your own mechanism.

Hope this will solve your problem.

Harsha Vardhan
  • 3,324
  • 2
  • 17
  • 22