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>