Chero Beam
Chero Beam

Reputation: 3

Implement Listview and Imageview in Adapter

My ImageView is inside a Listview and the user can choose a image from the gallery and put it into the ImageView! The problem I have is that the image appears for a second in the imageview and then dissapears immediately!

I am afraid of that I don't set up the adapter in the right way and thats why the image dissappears! The problem also is that I am pretty new to Android and dont get it good the way how i set up the adapter in the right way to show the image!

For info: in the main xml is the ListView and in the customlayout xml is the ImageView with 2 TextView. One for the username and the other for the comment the user sends! They appear in the right way but the issue is with the ListView and the ImageView!

I need guidance and advice in this case...

Here comes my adapter:

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;

    }
}

Here is the main activity with the 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!

    }


}

Upvotes: 0

Views: 2120

Answers (3)

McLeroy Ibe
McLeroy Ibe

Reputation: 66

Chero Beam you should probably send me your project on Google+ so I can help you fix it. If that's okay by you

Upvotes: 0

McLeroy Ibe
McLeroy Ibe

Reputation: 66

I really can't see where you set the image in the adapter. Setting of the image should be done in the adapter and not in

.....
onActivityResult

i.e

@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);
imgPic.setImageBitmap(BitmapFactory.decodeFile(imagePath));

return convertView;

}

but then Why are you showing a single image in a ListView. it will lead to duplication of the same image. I don't see any use for that

Upvotes: 0

androidtitan
androidtitan

Reputation: 671

Would you mind posting your activity or fragment code so we can see how you call and construct your adapter? With a quick pass it looks as though you are only setting your image if your convertView == null. Keep in mind that your listview rows(your convertView) are constantly being destroyed and rebuilt as you scroll and change the orientation.

Upvotes: 1

Related Questions