0

I trying to return the captured image and text from Activity B to Activity A listView. I follow this tutorial 1 and tutorial 2, but sadly, the list number was fixed. I get stucked on some part since I wanted to add the listView dynamically but not fixed. Can someone help me ? Thanks

Activity B (return text and image to A)

 private void activeTakePhoto() { // open camera
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }


 @Override
  protected void onActivityResult(int requestCode, int resultCode,
                                              Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case RESULT_LOAD_IMAGE:
                if (requestCode == RESULT_LOAD_IMAGE &&
                        resultCode == RESULT_OK && null != data) {
                  ......// selecte from gallery
                }
            case REQUEST_IMAGE_CAPTURE:
                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

                //to generate random file name
                String fileName = "tempimg.jpg";

                try {
                     photo = (Bitmap) data.getExtras().get("data");
                     imageView.setImageBitmap(photo); // image can shown here
                    } catch (Exception e) {
                      e.printStackTrace();
                    }
                }
              }

  submit.setOnClickListener(new View.OnClickListener() { // return image and text to A
            @Override
            public void onClick(View v) {
                Intent returnIntent=new Intent();
                amount=Amount.getText().toString();
                returnIntent.putExtra("amount", amount);
                returnIntent.putExtra("photo",photo);
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
              }
        });

Activity A

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
        if (resultCode == RESULT_OK) {
            if (requestCode == PROJECT_REQUEST_CODE) {
                ReceiveAmount = data.getStringExtra("amount");
                ReceiveImage=data.getStringExtra("photo");

                if (mClickedPosition == -1) {  // if icon clicked
                    if (obj != null)
                        obj.addNewItem( ReceiveAmount,ReceiveImage);

                } else {
                    if (obj != null)
                        obj.changeItem(mClickedPosition,ReceiveAmount, ReceiveImage);

                }


            }
        }
    }

PicCustomBaseAdapter(obj)

  public void addNewItem(String amount, Bitmap imageFromClaims)
        {
            ImageAndText image = new ImageAndText();
            image.setAmount(" Amount : " + amount);
            image.setImage(imageFromClaims);
            imgAndText.add(image);
            this. notifyDataSetChanged();
            addOrRemoveFooter();
        }

            public void changeItem(int m,String amount, Bitmap imageFromClaims)
        {
            ImageAndText image = new ImageAndText();
            image.setAmount(" Amount : " + amount);
            image.setImage(imageFromClaims);
            imgAndText.set(image);
            this. notifyDataSetChanged();
        }


        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.custom_image_and_text, null);
                holder = new ViewHolder();
                holder.txtAmount = (TextView) convertView.findViewById(R.id.ListAmount);
               holder.picture=(ImageView)convertView.findViewById(R.id.photo);
               convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
       holder.txtAmount.setText(imgAndText.get(position).getAmount());
                holder.picture.setImageResource(imgid[imgAndText.get(position).getImageNumber() - 1]);

    return convertView;
        }

        static class ViewHolder {
            TextView txtAmount;
            ImageView picture;
        }
    }

Finally ImageAndText

 private String amount = "";
    Bitmap image;
 public Bitmap getImage() {
        return image;
    }

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

    public String getAmount() {
        return amount;
    }

My problem :

1.ReceiveImage in onActivityResult Activity A wrong 2nd Argument Type
2. In PicCustomBaseAdapter, public view get view , how should I put imgid  `holder.picture.setImageResource(imgid[imgAndText.get(position).getImageNumber() - 1]);` since image is added dynamically ?
3.  imgAndText.set(image); in PicCustomBaseAdapter, changeItem() error
12
  • Would you add your logcat output? Commented Dec 9, 2015 at 16:53
  • @JordanSeanor I didn't run my app yet, but you can see My problem as I have stated in my post. Thanks Commented Dec 9, 2015 at 16:54
  • @JordanSeanor do you know how to solve my 1st issue ? Commented Dec 9, 2015 at 17:17
  • 1
    but past that the answer given below is exactly what you need. You can do the list dynamically as well. Commented Dec 9, 2015 at 17:25
  • Your code is inconsistent, once again. In the getView() method, you use a getImageNumber() method not shown in the ImageAndText class. I'm assuming that's a Resource ID. You need to be consistent with how you're handling the image in that class. I would change the ImageAndText to remove any use of a Resource ID; i.e. have it keep only a Bitmap. When you're creating the initial data list, load the Bitmap from Resources, and pass that to the ImageAndText object, instead of an ID. Then change the getView() line to: holder.picture.setImageBitmap(imgAndText.get(position).getImage());. Commented Dec 9, 2015 at 17:26

1 Answer 1

1

I recently made this program. It include ListView with Image and Text hop it helps

MainActivity.java

  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.ListAdapter;
  import android.widget.ListView;
  import android.widget.Toast;

  public class MainActivity extends AppCompatActivity {

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

          String[] favouriteTvShows = {"Pushing Daisies", "Better Off Ted",
                 "Twin Peaks", "Freaks and Geeks", "Orphan Black", "Walking Dead",
                  "Breaking Bad", "The 400", "Alphas", "Life on Mars"};

          ListAdapter theAdapter = new myAdapter(this, favouriteTvShows);

          ListView listView = (ListView) findViewById(R.id.listView);

          listView.setAdapter(theAdapter);

          listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

                  String tvShowPicked = "You Selected " + String.valueOf(adapterView.getItemAtPosition(position));

                  Toast.makeText(MainActivity.this, tvShowPicked, Toast.LENGTH_LONG).show();

              }
          });
      }

  }

myAdapter.java

class myAdapter extends ArrayAdapter<String>{

    public myAdapter(Context context, String[] values) {
        super(context, R.layout.row_layout_2, values);
    }

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

        LayoutInflater inflater = LayoutInflater.from(getContext());

        View theView = inflater.inflate(R.layout.row_layout_2, parent, false);

        String tvShows = getItem(position);

        TextView textView = (TextView) theView.findViewById(R.id.textView1);

        textView.setText(tvShows);

        ImageView imageView = (ImageView) theView.findViewById(R.id.imageView1);

        imageView.setImageResource(R.drawable.imgg);

        return theView;
    }
}

activity_main.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:orientation="vertical"
  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

  <ListView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/listView"
      android:layout_centerVertical="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true" />

  </LinearLayout>

row_layout.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
      android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:orientation="vertical"
      android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/textView1"
          android:textSize="30sp"
          android:textStyle="bold"
          android:padding="15dp"/>

  </LinearLayout>

row_layout_2.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
      android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:orientation="horizontal"
      android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

      <ImageView
          android:layout_width="25dp"
          android:layout_height="25dp"
          android:layout_marginLeft="10dp"
          android:layout_marginTop="25dp"
          android:src="@drawable/imgg"
          android:id="@+id/imageView1" />


      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/textView1"
          android:textSize="30sp"
          android:textStyle="bold"
          android:padding="15dp"/>

  </LinearLayout>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your answer, but I don't want the list number fixed. Is there a way to achieve this ?
Can you explain what you mean by "I don't want the list number fixed"
I want add the list dynamically.
Did your list can add dynamically?
Sorry I only know this way of adding image and text in a ListView

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.