I am creating a dialog that shows a list of profiles to pick from. The way I'm doing this is using a listview and the content of the listview will be taken from parse. I have been having problem getting the listview to work. I tried using an adapter. and with my current code, my proble is with the getListView() in the query. Is there something else that I can write there? or is there an easier way to do this? Thank you very much
This is the code that launches the dialog:
mSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
ParseUser currentUser = ParseUser.getCurrentUser();
//get current user username and turn it to string
final String currentUserUsername = currentUser.getUsername();
// bring user to homepage and do stuff with the user
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("MyProfiles");
query.whereEqualTo("user", currentUserUsername);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> profileObject, ParseException e) {
if (e == null) {
mProfile = profileObject;
//SelectProfileAdapter adapter = new SelectProfileAdapter(getListView().getContext(), mProfile);
//setListAdapter(adapter);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(AddSocialActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.selectprofilecustomlayout, null);
alertDialog.setView(convertView);
alertDialog.setTitle("List");
ListView lv = (ListView) convertView.findViewById(R.id.selectProfile);
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.list_content,names);
//lv.setAdapter(adapter);
alertDialog.show();
} else {
}
}
});
}
});
This is the adapter:
public class SelectProfileAdapter extends ArrayAdapter<ParseObject> {
protected Context mContext;
protected List<ParseObject> mProfile;
public SelectProfileAdapter (Context context, List<ParseObject> MyProfile){
super(context, R.layout.selectpprofilecustomlayout, MyProfile);
mContext = context;
mProfile = MyProfile;
}
@Override
public View getView (final int position, View convertView, ViewGroup Parent){
final ViewHolder holder;
if(convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.selectprofilecustomlayout, null);
holder = new ViewHolder();
holder.ProfileNameMain = (TextView) convertView.findViewById(R.id.text);
holder.ProfileImageMain = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
final ParseObject profileObject = mProfile.get(position);
//profile name
String profilename = profileObject.getString("profileName");
holder.ProfileNameMain.setText(profilename);
//profile Image
//********** (showing profile image) **********//
ParseFile image = (ParseFile) profileObject.get("profileImage");
image.getDataInBackground(new GetDataCallback() {
public void done(byte[] data, ParseException e) {
if (e == null) {
// Decode the Byte[] into bitmap
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
// Set the Bitmap into the imageView
holder.ProfileImageMain.setImageBitmap(bmp);
//circle img
int wbmp = bmp.getWidth();
int hbmp = bmp.getHeight();
int diameter;
if(wbmp > hbmp){
diameter = hbmp;
}else{
diameter = wbmp;
}
Bitmap resized = Bitmap.createScaledBitmap(bmp, wbmp, hbmp, true);
Bitmap conv_bm = ImageHelper.getRoundedRectBitmap(resized, diameter );
holder.ProfileImageMain.setImageBitmap(conv_bm);
// TODO Auto-generated method stub
//circle img ends
} else {
Log.d("test", "There was a problem downloading the data.");
}
}
});
//********** (showing profile image) **********//
return convertView;
}
public static class ViewHolder {
ImageView ProfileImageMain;
TextView ProfileNameMain;
}
}
This is the selectprofilecustomlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:id="@+id/selectProfile"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="5dp" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="profileName"
android:textStyle="bold" />
</LinearLayout>