【问题标题】:How to get listview from parse to a custom dialog?如何从解析获取列表视图到自定义对话框?
【发布时间】:2015-10-14 19:21:55
【问题描述】:

我正在创建一个对话框,其中显示了可供选择的配置文件列表。 我这样做的方式是使用列表视图,列表视图的内容将从解析中获取。我在让 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>

【问题讨论】:

    标签: android xml listview android-listview customdialog


    【解决方案1】:

    我有另一种方式..List&lt;BranchListCol&gt; 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 是全局变量。你得到列表并设置适配器..

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

    【讨论】:

    • 有趣。那么我在哪里放置分支列表?它是在 onCreate 之外作为单独的方法吗?还是它与对话框一起在 onClick 内?什么是分支列表?我在使用它时遇到错误。这是我第一次看到分支列表
    • BranchListCol 我的班级名称..List 分支列表;是全局声明...还有 BranchListCol 我的类名就是这样
    • 你能告诉我整个代码吗?我有点困惑。所以我需要创建一个名为 branchlist 的新类?
    • 所以分支列表只是一个名字?在我的情况下,它就像 MyProfile?
    • 那我需要新建一个java类吗?
    猜你喜欢
    • 2016-06-16
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    相关资源
    最近更新 更多