在适配器中实现Listview和Imageview

时间:2015-09-18 16:33:50

标签: android listview android-studio android-imageview

我的ImageView位于Listview内,用户可以从图库中选择一张图片并将其放入ImageView!我遇到的问题是图像在图像视图中出现一秒钟,然后立即消失!

我担心我没有以正确的方式设置适配器,这就是图像消失的原因!问题还在于我对Android很新,并且不能很好地按照正确的方式设置适配器来显示图像!

有关信息:在主xml中是ListView,在customlayout xml中是ImageView,其中包含TextView。一个用于用户名,另一个用于用户发送的评论!它们以正确的方式显示,但问题在于ListViewImageView

在这种情况下我需要指导和建议......

我的适配器来了:

public class RecipesAdapter extends ArrayAdapter<ParseObject>  {

    protected List<ParseObject> mRecipes;
    public Context mContext;

    public RecipesAdapter(Context context, List<ParseObject> comment) {

        super(context, R.layout.recipes_customlayout, comment);
        mContext = context;
        mRecipes = comment;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.recipes_customlayout, null);

            holder = new ViewHolder();

            holder.imgPic = (ParseImageView) convertView.findViewById(R.id.img_recipes);
            holder.username_recipesChat = (TextView) convertView.findViewById(R.id.row_username_recipes);
            holder.message_recipesChat = (TextView) convertView.findViewById(R.id.row_message_recipes);

            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();

        }

        ParseObject recipesObject = mRecipes.get(position);

        String username = recipesObject.getString("user");
        holder.username_recipesChat.setText(username);

        String message = recipesObject.getString("commentStatus");
        holder.message_recipesChat.setText(message);

        return convertView;
    }

    public static class ViewHolder {

        TextView username_recipesChat;
        TextView message_recipesChat;
        ParseImageView imgPic;

    }
}

以下是ImageView的主要活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recipes_ideas);

    imagePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");

    imgPic = (ParseImageView) findViewById(R.id.img_recipes);
    mRecipesChat = (EditText) findViewById(R.id.add_text);
    mSendPicBtn = (Button) findViewById(R.id.btn_pic);
    mSendPicBtn.setOnClickListener(this);


    listview = (ListView) findViewById(android.R.id.list);
    listview.setAdapter(adapter);




    mSendRecipesBtn = (Button) findViewById(R.id.btn_comment);
    mSendRecipesBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ParseUser currentUser = ParseUser.getCurrentUser();

            String currentUserUsername = currentUser.getUsername();

            String commentStatus = mRecipesChat.getText().toString();

            if (commentStatus.isEmpty()) {

                AlertDialog.Builder builder = new AlertDialog.Builder(RecipesIdeas.this);
                builder.setMessage("Please type in a Message!");
                builder.setTitle("Oops!");
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }

                });
                AlertDialog dialog = builder.create();
                dialog.show();

            }

            byte[] image = null;
            final ParseFile file = new ParseFile("CommentPic.png", image);
            // Upload the image into Parse Cloud
            file.saveInBackground();

            ParseObject commentObject = new ParseObject("Comment");
            commentObject.put("commentStatus", commentStatus);
            commentObject.put("user", currentUserUsername);
            commentObject.put("Image", "CommentPic.png");
            commentObject.put("CommentImageFile", file);
            commentObject.saveInBackground(new SaveCallback() {
                @Override
                public void done(ParseException e) {

                    Toast.makeText(getBaseContext(), "Your Pic is Saved!", Toast.LENGTH_LONG).show();

                    if (e == null) {
                        Toast.makeText(RecipesIdeas.this, "Send!", Toast.LENGTH_LONG).show();
                        mRecipesChat.setText("");

                        queryAndPopulateMsgs();

                    } else {
                        Toast.makeText(RecipesIdeas.this, e.getMessage(), Toast.LENGTH_LONG).show();

                        AlertDialog.Builder builder = new AlertDialog.Builder(RecipesIdeas.this);
                        builder.setMessage(e.getMessage());
                        builder.setTitle("Sorry!");
                        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }

                        });
                        AlertDialog dialog = builder.create();
                        dialog.show();



                    }

                }


            });

        }


    });


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Here we need to check if the activity that was triggers was the Image Gallery.
    // If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
    // If the resultCode is RESULT_OK and there is some data we know that an image was picked.
    if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
        // Let's read picked image data - its URI
        Uri pickedImage = data.getData();
        // Let's read picked image path using content resolver
        String[] filePath = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
        cursor.moveToFirst();
        String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
        PreferenceManager.getDefaultSharedPreferences(this).edit().putString("picturePath", imagePath).commit();
        cursor.close();
        imgPic = (ParseImageView) findViewById(R.id.img_recipes);
        // Now we need to set the GUI ImageView data with data read from the picked file.
        imgPic.setImageBitmap(BitmapFactory.decodeFile(imagePath));


        // At the end remember to close the cursor or you will end with the RuntimeException!

    }


}

2 个答案:

答案 0 :(得分:1)

您是否介意发布您的活动或片段代码,以便我们了解您如何调用和构建适配器?快速通过,看起来好像只是设置了convertView == null的图像。请记住,当您滚动并更改方向时,列表视图行(您的convertView)会不断被销毁和重建。

答案 1 :(得分:0)

Chero Beam你应该把你的项目发给我Google+,这样我就可以帮你修复它。如果你没事的话