本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)ListView和adapter配合顯示圖片和文字列表功能。分享給大家供大家參考,具體如下:
站在用戶的角度思考問題,與客戶深入溝通,找到樂都網(wǎng)站設(shè)計(jì)與樂都網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋樂都地區(qū)。
實(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="請輸入內(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ù)組長度 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來獲取真實(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 兩個私有變量
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資源操作技巧匯總》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
分享題目:Android開發(fā)實(shí)現(xiàn)ListView和adapter配合顯示圖片和文字列表功能示例
本文路徑:http://aaarwkj.com/article0/iijsio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、響應(yīng)式網(wǎng)站、App設(shè)計(jì)、網(wǎng)站制作、虛擬主機(jī)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)