本文實例講述了Android開發(fā)實現(xiàn)ListView和adapter配合顯示圖片和文字列表功能。分享給大家供大家參考,具體如下:
站在用戶的角度思考問題,與客戶深入溝通,找到樂都網(wǎng)站設(shè)計與樂都網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋樂都地區(qū)。
實際效果:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
代碼實現(xiàn)部分:
public class MainActivity extends AppCompatActivity { //list表 private ListinformationsList01 = new ArrayList<>(); //當(dāng)前消息列表 ListView list01 ; //消息發(fā)送欄 EditText editText01 ; //存放圖片 ImageView imageView01; //消息發(fā)送按鈕 Button button01_send ; //記錄數(shù)組長度 int arr_num = 0; //定義一個數(shù)組 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,定義延時執(zhí)行的行為*/ 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); //在相冊里面選擇好相片之后調(diào)回到現(xiàn)在的這個activity中 switch (requestCode) { case IMAGE_REQUEST_CODE://這里的requestCode是我自己設(shè)置的,就是確定返回到那個Activity的標(biāo)志 if (resultCode == RESULT_OK) {//resultcode是setResult里面設(shè)置的code值 try { Uri selectedImage = data.getData(); //獲取系統(tǒng)返回的照片的Uri String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);//從系統(tǒng)表中查詢指定Uri對應(yīng)的照片 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];//解析出數(shù)字格式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; } }
權(quán)限:
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設(shè)計有所幫助。