Android開發(fā)實(shí)現(xiàn)ListView和adapter配合顯示圖片和文字列表功能示例
本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)ListView和adapter配合顯示圖片和文字列表功能。分享給大家供大家參考,具體如下:
實(shí)際效果:
布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <!--使用紅色得分割條--> <ListView android:id="@+id/list1" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#f00" android:dividerHeight="2px" android:headerDividersEnabled="false"> </ListView> <!--用于存放和發(fā)送新的信息--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="0dp" android:background="#66ffffff" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!--設(shè)置最大行數(shù)--> <EditText android:id="@+id/ifo_edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請(qǐng)輸入內(nèi)容" android:maxLines="6" android:textColorHint="#c0c0c0" /> <!--存放新的圖片--> <ImageView android:id="@+id/ifo_image" android:layout_width="130dp" android:layout_height="110dp" android:src="@drawable/addphoto" /> </LinearLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <!--點(diǎn)擊取消發(fā)送消息--> <Button android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_gravity="left" android:text="取消" android:textSize="16sp" /> <!--點(diǎn)擊發(fā)送消息--> <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="發(fā)送" android:textSize="16sp" /> </RelativeLayout> </LinearLayout> </RelativeLayout>
代碼實(shí)現(xiàn)部分:
public class MainActivity extends AppCompatActivity { //list表 private List<Informations> informationsList01 = new ArrayList<>(); //當(dāng)前消息列表 ListView list01 ; //消息發(fā)送欄 EditText editText01 ; //存放圖片 ImageView imageView01; //消息發(fā)送按鈕 Button button01_send ; //記錄數(shù)組長(zhǎng)度 int arr_num = 0; //定義一個(gè)數(shù)組 String[] arr1 = new String[arr_num]; //從相冊(cè)獲得圖片 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); } } }); } /*定義一個(gè)Handler,定義延時(shí)執(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); //在相冊(cè)里面選擇好相片之后調(diào)回到現(xiàn)在的這個(gè)activity中 switch (requestCode) { case IMAGE_REQUEST_CODE://這里的requestCode是我自己設(shè)置的,就是確定返回到那個(gè)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對(duì)應(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來獲取真實(shí)圖片路徑 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<Informations> { private int resourceId; public EssayAdapter(Context context, int textViewResourceId, List<Informations> 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 兩個(gè)私有變量
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)限:
<!--獲取照片權(quán)限--> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android權(quán)限如何禁止以及友好提示用戶開通必要權(quán)限詳解
- Android開發(fā)中TextView各種常見使用方法小結(jié)
- 史上最全Android build.gradle配置詳解(小結(jié))
- Android開發(fā)實(shí)現(xiàn)的圓角按鈕、文字陰影按鈕效果示例
- Android開發(fā)之獲取單選與復(fù)選框的值操作示例
- Android開發(fā)實(shí)現(xiàn)Switch控件修改樣式功能示例【附源碼下載】
- Android開發(fā)實(shí)現(xiàn)的計(jì)時(shí)器功能示例
- Android開發(fā)之ListView的簡(jiǎn)單用法及定制ListView界面操作示例
- Android文字基線Baseline算法的使用講解
- android分享純圖片到QQ空間實(shí)現(xiàn)方式
相關(guān)文章
Android之聯(lián)系人PinnedHeaderListView使用介紹
Android聯(lián)系人中的ListView是做得比較獨(dú)特的,這幾天,我把他提取出來了,寫成一個(gè)簡(jiǎn)單的例子,留著備用,感興趣的朋友可以參考下哈2013-06-06RxJava加Retrofit文件分段上傳實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了RxJava加Retrofit文件分段上傳實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android實(shí)現(xiàn)指定時(shí)間定時(shí)觸發(fā)方法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)指定時(shí)間定時(shí)觸發(fā)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05詳解android 視頻圖片混合輪播實(shí)現(xiàn)
這篇文章主要介紹了android 視頻圖片混合輪播實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Android使用ViewPager實(shí)現(xiàn)翻頁效果
這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實(shí)現(xiàn)翻頁效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05詳解Flutter如何在單個(gè)屏幕上實(shí)現(xiàn)多個(gè)列表
這篇文章主要為大家詳細(xì)介紹了Flutter如何在單個(gè)屏幕上實(shí)現(xiàn)多個(gè)列表,這些列表可以水平排列、網(wǎng)格格式、垂直排列,甚至是這些常用布局的組合,感興趣的小伙伴可以了解下2023-11-11Android開發(fā)之DatePickerDialog、TimePickerDialog時(shí)間日期對(duì)話框用法示例
這篇文章主要介紹了Android開發(fā)之DatePickerDialog、TimePickerDialog時(shí)間日期對(duì)話框用法,結(jié)合實(shí)例形式分析了Android使用DatePickerDialog、TimePickerDialog顯示日期時(shí)間相關(guān)操作技巧,需要的朋友可以參考下2019-03-03Android 數(shù)據(jù)存儲(chǔ)方式有哪幾種
android為數(shù)據(jù)存儲(chǔ)提供了五種方式,有SharedPreferences、文件存儲(chǔ)、SQLite數(shù)據(jù)庫、ContentProvider、網(wǎng)絡(luò)存儲(chǔ),對(duì)android數(shù)據(jù)存儲(chǔ)方式感興趣的朋友可以通過本文學(xué)習(xí)一下2015-11-11Android開發(fā)中下拉刷新如何實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)中下拉刷新的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-07-07