如何动态添加带有图像和文本的ListView?

时间:2015-12-09 16:52:13

标签: android image listview android-listview

我尝试将{{1>}和captured image活动B 返回到活动A text。我遵循此tutorial 1tutorial 2,但遗憾的是,列表编号已修复。 由于我想动态添加listView但未修复,因此我陷入了某些困境。有人可以帮助我吗?感谢

活动B (将文字和图片返回A)

listView

活动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();
              }
        });

PicCustomBaseAdapter (OBJ)

@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);

                }


            }
        }
    }

最后 ImageAndText

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

我的问题:

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

1 个答案:

答案 0 :(得分:1)

我最近制作了这个节目。它包括ListView与图像和文本跳它帮助

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>