gpt4 book ai didi

java - 使用 editText 进行搜索功能 - ListView

转载 作者:行者123 更新时间:2023-12-01 09:40:55 34 4
gpt4 key购买 nike

我是 Android 新手,我使用 ListBooks 从 FirebaseDatabase 检索所有书籍,并且看得很清楚,但是当我使用搜索功能时,我的列表不正确。我不知道为什么。预先感谢您的帮助。

列表图书:

    public class ListBooks extends AppCompatActivity {

EditText searchBooks;
Context mContext;
static final String KEY = "KEY", TAG_LOGIN = "TAG_LOGIN";
String key, getTagLogin;

String user_name, searchSth;
DatabaseReference user;
DatabaseReference userBook = FirebaseDatabase.getInstance().getReference("Books").child("User's books");
ListView mListView;
ArrayList<String> mArrayUserName = new ArrayList<>();
ArrayList<String> mArrayTitle = new ArrayList<>();
ArrayList<String> mArrayAuthor = new ArrayList<>();
ArrayList<String> mArrayPrice = new ArrayList<>();
ArrayList<String> mArrayImage = new ArrayList<>();
customAdapter customAdapter;


@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_books);
mContext = this;



Intent intent = getIntent();
key = intent.getStringExtra(KEY);
getTagLogin = intent.getStringExtra(TAG_LOGIN);



mListView = (ListView) findViewById(R.id.recyclerView);

customAdapter = new customAdapter(mContext, mArrayTitle, mArrayAuthor, mArrayUserName, mArrayPrice, mArrayImage);
mListView.setAdapter(customAdapter);

userBook.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
UserBook userBook = dataSnapshot.getValue(UserBook.class);
user_name = userBook.user_name;
String book_title = userBook.book_title;
String book_author = userBook.book_author;
String book_price = userBook.book_price;
String book_cover = userBook.book_urlImage;


mArrayUserName.add(user_name);
mArrayTitle.add(book_title);
mArrayAuthor.add(book_author);
mArrayPrice.add(book_price);
mArrayImage.add(book_cover);
customAdapter.notifyDataSetChanged();

}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


searchBooks = (EditText) findViewById(R.id.search_books);

searchBooks.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
customAdapter.clear();
searchSth = searchBooks.getText().toString();

}

@Override
public void afterTextChanged(Editable editable) {
userBook.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
UserBook userBook = snapshot.getValue(UserBook.class);
String book_title = userBook.book_title;
if (book_title.contains(searchSth) && searchSth != null){
user_name = userBook.user_name;
String book_author = userBook.book_author;
String book_price = userBook.book_price;
String book_cover = userBook.book_urlImage;


mArrayUserName.add(user_name);
mArrayTitle.add(book_title);
mArrayAuthor.add(book_author);
mArrayPrice.add(book_price);
mArrayImage.add(book_cover);
customAdapter.notifyDataSetChanged();
}
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}
});




}

}

customAdapter.java:

    public class customAdapter extends ArrayAdapter<String>{

Context mContext;
ArrayList<String> user_name;
ArrayList<String> book_title;
ArrayList<String> book_author;
ArrayList<String> book_price;
ArrayList<String> book_cover;


public customAdapter(Context context, ArrayList<String> user_name, ArrayList<String> book_title,
ArrayList<String> book_author, ArrayList<String> book_price, ArrayList<String> book_cover) {
super(context, R.layout.row_list_books, book_title);
// TODO Auto-generated constructor stub

this.mContext = context;
this.user_name = user_name;
this.book_title = book_title;
this.book_author = book_author;
this.book_price = book_price;
this.book_cover = book_cover;
}

public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View rowView = inflater.inflate(R.layout.row_list_books, null, true);

TextView user = (TextView) rowView.findViewById(R.id.user_name_search);
TextView title = (TextView) rowView.findViewById(R.id.bookTitle);
TextView author = (TextView) rowView.findViewById(R.id.bookAuthor);
TextView price = (TextView) rowView.findViewById(R.id.priceBook);
ImageView bookImage = (ImageView) rowView.findViewById(R.id.coverBookSearch);


user.setText(user_name.get(position));
title.setText(book_title.get(position));
author.setText(book_author.get(position));
price.setText(book_price.get(position));
if (book_cover.get(position).startsWith("https://")) {
Picasso.with(mContext).load(book_cover.get(position)).into(bookImage);
} else {
bookImage.setImageBitmap(StringToBitMap(book_cover.get(position)));
}


return rowView;

}





//Transform a String in a Bitmap Image (used with camera)
public Bitmap StringToBitMap(String encodedString){
try {
byte [] encodeByte= Base64.decode(encodedString, Base64.DEFAULT);
Bitmap bitmap= BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
} catch(Exception e) {
e.getMessage();
return null;
}
}
}

用户簿:

public class UserBook {

public String user_key;
public String user_tag_login;
public String book_title;
public String book_author;
public String book_urlImage;
public String book_publisher;
public String book_price;
public String user_name;

public UserBook() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}

public UserBook(String user_name, String user_key, String user_tag_login, String book_title, String book_author, String book_urlImage, String book_publisher, String book_price) {
this.user_name = user_name;
this.user_key = user_key;
this.user_tag_login = user_tag_login;
this.book_title = book_title;
this.book_author = book_author;
this.book_urlImage = book_urlImage;
this.book_publisher = book_publisher;
this.book_price = book_price;
}


}

最佳答案

不能保证这会解决搜索问题,但它肯定会降低代码的复杂性。

替换所有这些

ArrayList<String> user_name;
ArrayList<String> book_title;
ArrayList<String> book_author;
ArrayList<String> book_price;
ArrayList<String> book_cover;

使用其中之一

ArrayList<UserBook> books;

然后

public class customAdapter extends ArrayAdapter<UserBook>

并更改在适配器中显示图书对象的所有其他逻辑。

关于java - 使用 editText 进行搜索功能 - ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38460749/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com