AndriodStudio使用listview實(shí)現(xiàn)簡(jiǎn)單圖書管理
本文實(shí)例為大家分享了使用listview實(shí)現(xiàn)簡(jiǎn)單圖書管理的具體代碼,供大家參考,具體內(nèi)容如下
在主類布局文件中只需要一個(gè)listview即可
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/list_view" ? ? ? ? /> ? </LinearLayout>
在listview的布局文件中給listview定義樣式
<?xml version="1.0" encoding="utf-8"?> <LinearLayout ? ? xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="horizontal"> ? ? ? <ImageView ? ? ? ? android:id="@+id/book_img" ? ? ? ? android:layout_width="103dp" ? ? ? ? android:layout_height="113dp" /> ? ? ? <LinearLayout ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:orientation="vertical"> ? ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:id="@+id/book_name" ? ? ? ? ? ? /> ? ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:id="@+id/book_author" ? ? ? ? ? ? /> ? ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:id="@+id/book_info1" ? ? ? ? ? ? /> ? ? </LinearLayout> </LinearLayout>
接下來是類方法:首先定義一個(gè)Book類用來接收和返回值
package com.example.tow; ? public class Book { ? ? String name; ? ? int imageId; ? ? String author; ? ? String info1; ? ? public Book(String name,int imageId,String author,String info1){ ? ? ? ? this.name = name; ? ? ? ? this.imageId = imageId; ? ? ? ? this.author = author; ? ? ? ? this.info1 = info1; ? ? } ? ? ? public Book(String name,int imageId){ ? ? ? ? this.name = name; ? ? ? ? this.imageId = imageId; ? ? } ? ? public String getName(){ ? ? ? ? return name; ? ? } ? ? public int getImageId(){ ? ? ? ? return imageId; ? ? } ? ? public String getAuthor(){ ? ? ? ? return author; ? ? } ? ? public String getInfo1(){ ? ? ? ? return info1; ? ? } }
接著給listview添加一個(gè)Adapter這里使用了ArrayAdapter沒有使用BaseAdapter是因?yàn)橥瓿珊?jiǎn)單問題使用ArrayAdapter更簡(jiǎn)單
package com.example.tow; ? import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; ? import androidx.annotation.NonNull; ? import java.util.List; ? public class BookAdapter extends ArrayAdapter<Book> { ? ? private int resourceId; ? ? public BookAdapter(@NonNull Context context, int resource, @NonNull List<Book> objects){ ? ? ? ? super(context,resource,objects); ? ? ? ? resourceId = resource; ? ? } ? ? @Override ? ? public View getView(int position, View convertView, ViewGroup parent){ ? ? ? ? Book book = getItem(position); ? ? ? ? //LayoutInflater動(dòng)態(tài)加載子視圖的布局 ? ? ? ? View view = LayoutInflater.from(getContext()).inflate(resourceId, ? ? ? ? ? ? ? ? parent,false); ? ? ? ? ImageView bookImg = view.findViewById(R.id.book_img); ? ? ? ? TextView bookName = view.findViewById(R.id.book_name); ? ? ? ? TextView bookAuthor = view.findViewById(R.id.book_author); ? ? ? ? TextView bookInfo1 = view.findViewById(R.id.book_info1); ? ? ? ? bookImg.setImageResource(book.getImageId()); ? ? ? ? bookName.setText(book.getName()); ? ? ? ? bookAuthor.setText(book.getAuthor()); ? ? ? ? bookInfo1.setText(book.getInfo1()); ? ? ? ? return view; ? ? } }
接下來就是書寫主類:
package com.example.tow; ? import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; ? import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; ? import java.util.ArrayList; import java.util.List; ? public class MainActivity extends AppCompatActivity { ? ? private List<Book> bookList = new ArrayList<>(); ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? initBook(); ? ? ? ? BookAdapter adapter = new BookAdapter(MainActivity.this,R.layout.book_item,bookList); ? ? ? ? ListView listView = findViewById(R.id.list_view); ? ? ? ? listView.setAdapter(adapter); ? ? ? ? listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onItemClick(AdapterView<?> parent, View view, int i, long id) { ? ? ? ? ? ? ? ? Book book = bookList.get(i); ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,book.getName(),Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? ? listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public boolean onItemLongClick(AdapterView<?> parent, View view, int i, long l) { ? ? ? ? ? ? ? ? Book book = bookList.get(i); ? ? ? ? ? ? ? ? AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); ? ? ? ? ? ? ? ? builder.setTitle("確認(rèn)刪除").setMessage("是否確認(rèn)刪除書籍"+book.getName()+"?"); ? ? ? ? ? ? ? ? builder.setPositiveButton("是", new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int ij) { ? ? ? ? ? ? ? ? ? ? ? ? bookList.remove(i); ? ? ? ? ? ? ? ? ? ? ? ? adapter.notifyDataSetChanged(); ? ? ? ? ? ? ? ? ? ? ? ? listView.invalidate(); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? ? ? builder.setNegativeButton("否", new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) { ? ? ? ? ? ? ? ? ? ? ? ? dialogInterface.dismiss(); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? ? ? builder.show(); ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? public void initBook(){ ? ? ? ? Book book1 = new Book("火影忍者疾風(fēng)",R.drawable.book1,"牛仔|可以了","看人kiss"); ? ? ? ? bookList.add(book1); ? ? ? ? Book book2 = new Book("火影劇場(chǎng)版",R.drawable.book2,"可愛卡卡西|寫不下去了","學(xué)習(xí)kiss"); ? ? ? ? bookList.add(book2); ? ? ? ? Book book3 = new Book("hello",R.drawable.book,"雷切|我說真的","自己去kiss"); ? ? ? ? bookList.add(book3); ? ? ? ? Book book4 = new Book("我是卡卡西",R.drawable.book3,"小澤瑪利亞|真寫不下去了","巨好看的kiss"); ? ? ? ? bookList.add(book4); ? ? ? ? Book book5 = new Book("我是鳴人",R.drawable.book4,"廣末涼子|詞窮了","我燃起來了"); ? ? ? ? bookList.add(book5); ? ? ? ? Book book6 = new Book("你是個(gè)好人",R.drawable.book5,"鳴人|但是還是得寫","我炸了"); ? ? ? ? bookList.add(book6); ? ? ? ? Book book7 = new Book("我不想辜負(fù)你",R.drawable.book6,"自來也|沒辦法","開始戰(zhàn)斗吧"); ? ? ? ? bookList.add(book7); ? ? ? ? Book book8 = new Book("謝謝你",R.drawable.book,"羅密歐|我得交作業(yè)","謝謝你泰羅"); ? ? ? ? bookList.add(book8); ? ? ? ? Book book9 = new Book("泰羅",R.drawable.book9,"朱麗葉|就這吧","真美"); ? ? ? ? bookList.add(book9); ? ? } }
這里只定義了長(zhǎng)按刪除的操作,點(diǎn)擊操作未完善,可以書寫頁(yè)面跳轉(zhuǎn)自行更改
運(yùn)行效果:
長(zhǎng)按效果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 圖書管理系統(tǒng)java代碼實(shí)現(xiàn)
- 圖書管理系統(tǒng)java版
- 一個(gè)簡(jiǎn)陋的java圖書管理系統(tǒng)
- java實(shí)現(xiàn)圖書館管理系統(tǒng)
- Java+MySQL實(shí)現(xiàn)圖書管理系統(tǒng)(完整代碼)
- java GUI實(shí)現(xiàn)學(xué)生圖書管理簡(jiǎn)單實(shí)例
- 圖書信息管理java實(shí)現(xiàn)代碼
- JAVA初級(jí)項(xiàng)目——實(shí)現(xiàn)圖書管理系統(tǒng)
- java控制臺(tái)輸出圖書館管理系統(tǒng)
- java+mysql實(shí)現(xiàn)圖書館管理系統(tǒng)實(shí)戰(zhàn)
相關(guān)文章
Android中SharedPreferences簡(jiǎn)單使用實(shí)例
這篇文章主要介紹了Android中SharedPreferences簡(jiǎn)單使用案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Flutter?DateTime日期轉(zhuǎn)換的詳細(xì)使用
本文主要介紹了Flutter?DateTime日期轉(zhuǎn)換的詳細(xì)使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05Android網(wǎng)絡(luò)編程之UDP通信模型實(shí)例
這篇文章主要介紹了Android網(wǎng)絡(luò)編程之UDP通信模型實(shí)例,本文給出了服務(wù)端代碼和客戶端代碼,需要的朋友可以參考下2014-10-10Android手機(jī)衛(wèi)士之綁定sim卡序列號(hào)
這篇文章主要介紹了Android手機(jī)衛(wèi)士之綁定sim卡序列號(hào)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android Studio使用教程(四):Gradle基礎(chǔ)
這篇文章主要介紹了Android Studio使用教程(四):Gradle基礎(chǔ),本文講解了什么是Gradle、安裝Gradle、Gradle 基本概念等內(nèi)容,需要的朋友可以參考下2015-05-05Android異步加載數(shù)據(jù)和圖片的保存思路詳解
這篇文章主要介紹了Android異步加載數(shù)據(jù)和圖片的保存思路詳解的相關(guān)資料,需要的朋友可以參考下2016-04-04Android 實(shí)現(xiàn)帶字母索引的側(cè)邊欄功能
這篇文章主要介紹了Android 實(shí)現(xiàn)帶字母索引的側(cè)邊欄功能,需要的朋友可以參考下2017-08-08