AndriodStudio使用listview實現(xiàn)簡單圖書管理
本文實例為大家分享了使用listview實現(xiàn)簡單圖書管理的具體代碼,供大家參考,具體內容如下
在主類布局文件中只需要一個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>
接下來是類方法:首先定義一個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添加一個Adapter這里使用了ArrayAdapter沒有使用BaseAdapter是因為完成簡單問題使用ArrayAdapter更簡單
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動態(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("確認刪除").setMessage("是否確認刪除書籍"+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("火影忍者疾風",R.drawable.book1,"牛仔|可以了","看人kiss"); ? ? ? ? bookList.add(book1); ? ? ? ? Book book2 = new Book("火影劇場版",R.drawable.book2,"可愛卡卡西|寫不下去了","學習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("你是個好人",R.drawable.book5,"鳴人|但是還是得寫","我炸了"); ? ? ? ? bookList.add(book6); ? ? ? ? Book book7 = new Book("我不想辜負你",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); ? ? } }
這里只定義了長按刪除的操作,點擊操作未完善,可以書寫頁面跳轉自行更改
運行效果:
長按效果:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android中SharedPreferences簡單使用實例
這篇文章主要介紹了Android中SharedPreferences簡單使用案例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Android Studio使用教程(四):Gradle基礎
這篇文章主要介紹了Android Studio使用教程(四):Gradle基礎,本文講解了什么是Gradle、安裝Gradle、Gradle 基本概念等內容,需要的朋友可以參考下2015-05-05