AndriodStudio使用listview實(shí)現(xiàn)簡(jiǎn)單圖書(shū)管理
本文實(shí)例為大家分享了使用listview實(shí)現(xiàn)簡(jiǎn)單圖書(shū)管理的具體代碼,供大家參考,具體內(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>
接下來(lái)是類方法:首先定義一個(gè)Book類用來(lái)接收和返回值
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沒(méi)有使用BaseAdapter是因?yàn)橥瓿珊?jiǎn)單問(wè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;
? ? }
}接下來(lái)就是書(shū)寫主類:
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)刪除書(shū)籍"+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,"可愛(ài)卡卡西|寫不下去了","學(xué)習(xí)kiss");
? ? ? ? bookList.add(book2);
? ? ? ? Book book3 = new Book("hello",R.drawable.book,"雷切|我說(shuō)真的","自己去kiss");
? ? ? ? bookList.add(book3);
? ? ? ? Book book4 = new Book("我是卡卡西",R.drawable.book3,"小澤瑪利亞|真寫不下去了","巨好看的kiss");
? ? ? ? bookList.add(book4);
? ? ? ? Book book5 = new Book("我是鳴人",R.drawable.book4,"廣末涼子|詞窮了","我燃起來(lái)了");
? ? ? ? bookList.add(book5);
? ? ? ? Book book6 = new Book("你是個(gè)好人",R.drawable.book5,"鳴人|但是還是得寫","我炸了");
? ? ? ? bookList.add(book6);
? ? ? ? Book book7 = new Book("我不想辜負(fù)你",R.drawable.book6,"自來(lái)也|沒(méi)辦法","開(kāi)始戰(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)擊操作未完善,可以書(shū)寫頁(yè)面跳轉(zhuǎn)自行更改
運(yùn)行效果:

長(zhǎng)按效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 圖書(shū)管理系統(tǒng)java代碼實(shí)現(xiàn)
- 圖書(shū)管理系統(tǒng)java版
- 一個(gè)簡(jiǎn)陋的java圖書(shū)管理系統(tǒng)
- java實(shí)現(xiàn)圖書(shū)館管理系統(tǒng)
- Java+MySQL實(shí)現(xiàn)圖書(shū)管理系統(tǒng)(完整代碼)
- java GUI實(shí)現(xiàn)學(xué)生圖書(shū)管理簡(jiǎn)單實(shí)例
- 圖書(shū)信息管理java實(shí)現(xiàn)代碼
- JAVA初級(jí)項(xiàng)目——實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
- java控制臺(tái)輸出圖書(shū)館管理系統(tǒng)
- java+mysql實(shí)現(xiàn)圖書(shū)館管理系統(tǒng)實(shí)戰(zhàn)
相關(guān)文章
安卓APP測(cè)試之使用Burp Suite實(shí)現(xiàn)HTTPS抓包方法
這篇文章主要介紹了安卓APP測(cè)試之使用Burp Suite實(shí)現(xiàn)HTTPS抓包方法,本文詳解講解了測(cè)試環(huán)境和各個(gè)軟件的配置方法,需要的朋友可以參考下2015-04-04
Android導(dǎo)航欄功能項(xiàng)的顯示與屏蔽介紹
大家好,本篇文章主要講的是Android導(dǎo)航欄功能項(xiàng)的顯示與屏蔽介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
activity全屏實(shí)現(xiàn)沉浸式效果,并且單獨(dú)觸摸不會(huì)彈出虛擬按鍵的方法
今天小編就為大家分享一篇activity全屏實(shí)現(xiàn)沉浸式效果,并且單獨(dú)觸摸不會(huì)彈出虛擬按鍵的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Android手機(jī)聯(lián)系人帶字母索引的快速查找
這篇文章主要為大家詳細(xì)介紹了Android手機(jī)聯(lián)系人帶字母索引的快速查找實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-03-03
Gradle學(xué)習(xí)教程之部署上傳項(xiàng)目詳解
這篇文章主要給大家介紹了關(guān)于Gradle學(xué)習(xí)教程之部署上傳項(xiàng)目的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
Android style的繼承方式 點(diǎn)(.)和parent詳解及實(shí)例
這篇文章主要介紹了Android style的繼承方式 點(diǎn)(.)和parent詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android和iOS 測(cè)試五個(gè)最好的開(kāi)源自動(dòng)化工具
本文主要介紹Android和iOS 五個(gè)最好的開(kāi)源自動(dòng)化工具,這里整理了相關(guān)資料,希望能幫助測(cè)試軟件的朋友,有需要的看下2016-09-09

