网站建设资讯

NEWS

网站建设资讯

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

本文实例讲述了Android开发实现ListView和adapter配合显示图片和文字列表功能。分享给大家供大家参考,具体如下:

站在用户的角度思考问题,与客户深入沟通,找到乐都网站设计与乐都网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计、网站建设、企业官网、英文网站、手机端网站、网站推广、空间域名、网络空间、企业邮箱。业务覆盖乐都地区。

实际效果:

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

布局文件:

<?xml version="1.0" encoding="utf-8"?>

    
    
    
    
    
        
            
            
            
            
        
        
            
            

代码实现部分:

public class MainActivity extends AppCompatActivity {
  //list表
  private List informationsList01 = new ArrayList<>();
  //当前消息列表
  ListView list01 ;
  //消息发送栏
  EditText editText01 ;
  //存放图片
  ImageView imageView01;
  //消息发送按钮
  Button button01_send ;
  //记录数组长度
  int arr_num = 0;
  //定义一个数组
  String[] arr1 = new String[arr_num];
  //从相册获得图片
  Bitmap bitmap;
  //判断返回到的Activity
  private static final int IMAGE_REQUEST_CODE = 0;
  //图片路径
  private String path ;
  private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
      if((Integer)msg.obj==0){
        imageView01.setImageBitmap(bitmap);
      }
      super.handleMessage(msg);
    }
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list01 = (ListView) findViewById(R.id.list1);
    editText01 = (EditText) findViewById(R.id.ifo_edit);
    imageView01 = (ImageView) findViewById(R.id.ifo_image);
    button01_send = (Button) findViewById(R.id.send);
    imageView01.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if(ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
          ActivityCompat.requestPermissions(MainActivity.this,new String[]{
              Manifest.permission.WRITE_EXTERNAL_STORAGE
          },1);
        }
        Intent intent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, IMAGE_REQUEST_CODE);
      }
    });
    button01_send.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if(((BitmapDrawable) ((ImageView) imageView01).getDrawable()).getBitmap() != null
            || editText01.getText().toString() != null){
          Informations xiaochouyu = new Informations(
              ((BitmapDrawable) ((ImageView) imageView01).getDrawable()).getBitmap(),
              editText01.getText().toString());
          informationsList01.add(xiaochouyu);
          EssayAdapter adapter = new EssayAdapter(MainActivity.this,
              R.layout.array_list,informationsList01);
          list01.setAdapter(adapter);
          editText01.setText("");
          imageView01.setImageBitmap(null);
          imageView01.setImageResource(R.drawable.addphoto);
        }
      }
    });
  }
  /*定义一个Handler,定义延时执行的行为*/
  public void chnage(){
    new Thread(){
      @Override
      public void run() {
        while ( bitmap == null ){
          bitmap = BitmapFactory.decodeFile(path);
          Log.v("qwe","123");
        }
        Message message = handler.obtainMessage();
        message.obj = 0;
        handler.sendMessage(message);
      }
    }.start();
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //在相册里面选择好相片之后调回到现在的这个activity中
    switch (requestCode) {
      case IMAGE_REQUEST_CODE://这里的requestCode是我自己设置的,就是确定返回到那个Activity的标志
        if (resultCode == RESULT_OK) {//resultcode是setResult里面设置的code值
          try {
            Uri selectedImage = data.getData(); //获取系统返回的照片的Uri
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            path = cursor.getString(columnIndex); //获取照片路径
            cursor.close();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 1;
            bitmap = BitmapFactory.decodeFile(path,options);
            imageView01.setImageBitmap(bitmap);
            chnage();
            Toast.makeText(MainActivity.this,path,Toast.LENGTH_SHORT).show();
          } catch (Exception e) {
            // TODO Auto-generatedcatch block
            e.printStackTrace();
          }
        }
        break;
    }
  }
  @TargetApi(19)
  private void handleImageOmKitKat(Intent data){
    String imagePath = null;
    Uri uri = data.getData();
    if (DocumentsContract.isDocumentUri(this,uri)){
      //如果document类型是U日,则通过document id处理
      String docId = DocumentsContract.getDocumentId(uri);
      if ("com.android.providers.media.documents".equals(uri.getAuthority())){
        String id = docId.split(":")[1];//解析出数字格式id
        String selection = MediaStore.Images.Media._ID + "=" + id;
        imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
      }else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())){
        Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),Long.valueOf(docId));
        imagePath = getImagePath(contentUri,null);
      }
    }else if ("content".equalsIgnoreCase(uri.getScheme())){
      //如果是普通类型 用普通方法处理
      imagePath = getImagePath(uri,null);
    }else if ("file".equalsIgnoreCase(uri.getScheme())){
      //如果file类型位uri直街获取图片路径即可
      imagePath = uri.getPath();
    }
    displayImage(imagePath);
  }
  private void handleImageBeforeKitKat(Intent data){
    Uri uri = data.getData();
    String imagePath = getImagePath(uri,null);
    displayImage(imagePath);
  }
  private String getImagePath(Uri uri, String selection){
    String path = null;
    //通过Uri和selection来获取真实图片路径
    Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
    if (cursor != null){
      if (cursor.moveToFirst()){
        path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
      }
      cursor.close();
    }
    return path;
  }
  private void displayImage(String imagePath){
    if (imagePath != null){
      Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
      imageView01.setImageBitmap(bitmap);
    }else {
      Toast.makeText(MainActivity.this,"fail to get image",Toast.LENGTH_SHORT).show();
    }
  }
}

Adapter 配试器:

public class EssayAdapter extends ArrayAdapter {
  private int resourceId;
  public EssayAdapter(Context context, int textViewResourceId,
            List objects){
    super(context, textViewResourceId, objects);
    resourceId = textViewResourceId;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    Informations informations = getItem(position);
    View view = LayoutInflater.from(getContext()).inflate(resourceId,parent, false);
    ImageView informationImage = (ImageView) view.findViewById(R.id.image);
    TextView informationEssay = (TextView) view.findViewById(R.id.essay);
    informationImage.setImageBitmap(informations.getImageBitmap());
    informationEssay.setText(informations.getEssay());
    return view;
  }
}

Information类:

包括 Bitmap 和 Essay 两个私有变量

public class Informations {
  //文体
  private String essay;
  //图片
  private Bitmap imageId;
  Informations(Bitmap imageId, String essay){
    this.imageId = imageId;
    this.essay = essay;
  }
  Informations(){
    this.essay = null;
    this.imageId = null;
  }
  public String getEssay() {
    return essay;
  }
  public void setEssay(String essay) {
    this.essay = essay;
  }
  public Bitmap getImageBitmap() {
    return imageId;
  }
  public void setImageBitmap(Bitmap imageId) {
    this.imageId = imageId;
  }
}

权限:





更多关于Android相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》、《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android数据库操作技巧总结》及《Android资源操作技巧汇总》

希望本文所述对大家Android程序设计有所帮助。


名称栏目:Android开发实现ListView和adapter配合显示图片和文字列表功能示例
网站URL:http://cdweb.net/article/iijsio.html