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="請輸入內(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,定義延時(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);
//在相冊里面選擇好相片之后調(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權(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的簡單用法及定制ListView界面操作示例
- Android文字基線Baseline算法的使用講解
- android分享純圖片到QQ空間實(shí)現(xiàn)方式
相關(guān)文章
Android自定義控件實(shí)現(xiàn)邊緣凹凸的卡劵效果
這篇文章主要介紹了Android自定義控件實(shí)現(xiàn)邊緣凹凸的卡劵效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
Android使用RecyclerView實(shí)現(xiàn)列表數(shù)據(jù)選擇操作
這篇文章主要為大家詳細(xì)介紹了Android使用RecyclerView結(jié)合CheckBox實(shí)現(xiàn)列表數(shù)據(jù)選擇操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
anndroid使用ViewPager實(shí)現(xiàn)三個fragment切換
這篇文章主要介紹了anndroid使用ViewPager實(shí)現(xiàn)三個fragment切換,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
Android筆記之:App自動化之使用Ant編譯項(xiàng)目多渠道打包的使用詳解
本篇文章介紹了,Android筆記之:App自動化之使用Ant編譯項(xiàng)目多渠道打包的使用詳解。需要的朋友參考下2013-04-04
Android?Studio實(shí)現(xiàn)簡單計(jì)算器開發(fā)
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)簡單計(jì)算器開發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android藍(lán)牙的開啟和搜索設(shè)備功能開發(fā)實(shí)例
這篇文章主要介紹了Android藍(lán)牙服務(wù)啟動搜索流程,了解內(nèi)部原理是為了幫助我們做擴(kuò)展,同時(shí)也是驗(yàn)證了一個人的學(xué)習(xí)能力,如果你想讓自己的職業(yè)道路更上一層樓,這些底層的東西你是必須要會的2023-04-04
使用RadioButton+Fragment實(shí)現(xiàn)底部導(dǎo)航欄效果
這篇文章主要為大家詳細(xì)介紹了使用RadioButton+Fragment實(shí)現(xiàn)底部導(dǎo)航欄效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android之聯(lián)系人PinnedHeaderListView使用介紹
Android聯(lián)系人中的ListView是做得比較獨(dú)特的,這幾天,我把他提取出來了,寫成一個簡單的例子,留著備用,感興趣的朋友可以參考下哈2013-06-06

