如何从解析到自定义对话框获取listview?

时间:2015-07-24 07:57:49

标签: android xml listview android-listview customdialog

我正在创建一个对话框,显示可供选择的配置文件列表。 我这样做的方式是使用listview,listview的内容将从解析中获取。我一直有问题让listview工作。我尝试使用适配器。使用我当前的代码,我的问题在于查询中的getListView()。还有什么我可以在那里写的吗?或者有更简单的方法吗?非常感谢你

这是启动对话框的代码:

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 {
                        }
                    }
                });
        }
    });

这是适配器:

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;
}

}

这是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>

1 个答案:

答案 0 :(得分:1)

我有另一种方式.. List<BranchListCol> branchlist是全球声明。

 branchList = new ArrayList<BranchListCol>();
            try {
                // Locate the class table named "CardList" in Parse.com
                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("BranchList");
                // Locate the column named "CardTypeId" in Parse.com and order list
                // by ascending
                query.orderByAscending("Sort");
                ob = query.find();
                for (ParseObject Sort : ob) {



                    BranchListCol map = new BranchListCol();
                    map.setObjId((String) Sort.getObjectId());
                    map.setBranchCode((String) Sort.get("BranchCode"));
                    map.setName((String) Sort.get("Name"));
                    map.setDescription((String) Sort.get("Description"));
                    map.setEnable((boolean) Sort.get("Enable"));
                    map.setSort((int) Sort.get("Sort"));

                    branchList.add(map);

                }
            } catch (ParseException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }

其中branchlist是全局varible.you获取列表并设置适配器..

  listview = (ListView) findViewById(R.id.listview);
        // Pass the results into ListViewAdapter.java
        adapter = new ListViewAdapter(Branch.this, branchList);
        // Binds the Adapter to the ListView
        listview.setAdapter(adapter);

BranchListCol.java

public class BranchListCol {
    private String BranchCode;
    private String name;
    private String object;
    private String description;
    private boolean enable;
    private int sort;
    private String image;




    public String getObjId() {
        return object;
    }

    public void setObjId(String object) {
        this.object = object;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBranchCode() {
        return BranchCode;
    }

    public void setBranchCode(String branchCode) {
        this.BranchCode = branchCode;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean getEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public int getSort() {
        return sort;
    }

    public void setSort(int sort) {
        this.sort = sort;
    }
}