Android 按“搜索”按钮可清除列表视图中的所有项目/型号
我的Android 按“搜索”按钮可清除列表视图中的所有项目/型号,android,android-listview,searchview,Android,Android Listview,Searchview,我的ListView在启动时工作正常。问题是,每当我单击触发搜索视图的搜索按钮时,我的列表视图上的所有项目都会消失。即使清除了我的searchview上的文本字段,我的ListView仍处于空状态 Loanist\u Full.java(主要活动) package com.memger.pautangapp; 导入android.content.DialogInterface; 导入android.content.Intent; 导入android.database.DataSetObserve
ListView
在启动时工作正常。问题是,每当我单击触发搜索视图的搜索按钮时,我的列表视图上的所有项目都会消失。即使清除了我的searchview
上的文本字段,我的ListView
仍处于空状态
Loanist\u Full.java(主要活动)
package com.memger.pautangapp;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.database.DataSetObserver;
导入android.support.v7.app.AlertDialog;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.support.v7.widget.SearchView;
导入android.text.TextUtils;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.Button;
导入android.widget.ImageButton;
导入android.widget.ListView;
导入android.widget.TextView;
导入com.getbase.floatingactionbutton.floatingactionbutton;
导入java.text.DecimalFormat;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.Collections;
导入java.util.Comparator;
公共类贷款活动{
私有列表视图列表视图;
私有ListView_适配器;
私有ListView_模型;
私有ArrayList ArrayList=新ArrayList();
私有菜单项搜索项;
私有搜索视图搜索视图;
私有文本查看totalText,totalCount;
私人图像按钮排序;
私有浮动操作按钮addDebtorButton;
公共静态最终字符串currency=“₱”;
公共静态最终判定格式格式化程序=新判定格式(“####,###,##,##.#”);
字符串mContact[]={“Craig”、“Agatha”、“Dave”、“Brandon”、“Russel”、“Gleceper”、“Percy”、“Test”};
双mDebt[]={2000、525、8000、5000、955、4000、50123、51247};
双mDebtTotal=0;
字符串mDesc[]={“这是一个描述等”,“Ito bata pa dapat di pa pinapautang”,“Lalo na to kakatuto lang mag bike nangungutang na”,,,,,,,,,};
字符串mDate[]={“2016年4月21日”、“2017年3月4日”、“2011年5月14日”、“2000年3月1日”、“1971年7月14日”、“”、“”、“”、“”);
int-mImg[]={R.drawable.debtascontact_craig,R.drawable.debtascontact_agatha,R.drawable.debtascontact_dave,R.drawable.debtascontact_brandon,R.drawable.debtascontact_russel,R.drawable.debtascontact_gleceper,R.drawable.debtascontact_lola,R.drawable.ic_launcher_background};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.loanist_full);
totalText=findViewById(R.id.TotalTextView);
totalCount=findViewById(R.id.CountTextView);
listView=findViewById(R.id.listView);
adapter=新的ListView_适配器(此为arrayList);
setAdapter(适配器);
sortBy=findViewById(R.id.SortByButton);
addDebtorButton=findViewById(R.id.addDebtorButton);
/*做*/
displayListViewContents();
setTextTotal();
/*下面是我的听众*/
/*列表视图项*/
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共无效onItemClick(AdapterView AdapterView、View视图、int i、long l){
makeDialog(ListView_Adapter.modellist.get(i).getModelContact(),
ListView_Adapter.modellist.get(i).getModelDebt(),
ListView_Adapter.modellist.get(i).getModelDesc(),
ListView_Adapter.modellist.get(i.getModelDate());
}
});
/*按按钮排序*/
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
sortArray();
排序表();
}
});
/*债务人按钮*/
addDebtorButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
startActivity(新意图(Loanist_Full.this,Loanist_Add_deborter.class));
}
});
}/*创建时结束*/
/*显示列表视图内容*/
私有void displayListViewContents(){
/*显示listview的内容*/
for(int i=0;ipackage com.memger.pautanganapp;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.DataSetObserver;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import com.getbase.floatingactionbutton.FloatingActionButton;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
public class Loanist_Full extends AppCompatActivity {
private ListView listView;
private ListView_Adapter adapter;
private ListView_Model model;
private ArrayList<ListView_Model> arrayList = new ArrayList<ListView_Model>();
private MenuItem searchItem;
private SearchView searchView;
private TextView totalText, totalCount;
private ImageButton sortBy;
private FloatingActionButton addDebtorButton;
public static final String currency = "₱";
public static final DecimalFormat formatter = new DecimalFormat("###,###,###.##");
String mContact[] = {"Craig", "Agatha", "Dave", "Brandon", "Russel", "Gleceper", "Percy", "Test"};
double mDebt[] = {2000, 525, 8000, 5000, 955, 4000, 50123, 51247};
double mDebtTotal = 0;
String mDesc[] = {"This is a description et. cetera", "Ito bata pa dapat di pa pinapautang", "Lalo na to kakatuto lang mag bike nangungutang na", "", "", "", "", ""};
String mDate[] = {"Apr. 21, 2016", "Mar. 4, 2017", "May 14, 2011", "Mar. 1, 2000", "Jul. 14, 1971", "", "", ""};
int mImg[] = {R.drawable.debtcontact_craig, R.drawable.debtcontact_agatha, R.drawable.debtcontact_dave, R.drawable.debtcontact_brandon, R.drawable.debtcontact_russel, R.drawable.debtcontact_gleceper, R.drawable.debtcontact_lola, R.drawable.ic_launcher_background};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loanist_full);
totalText = findViewById(R.id.TotalTextView);
totalCount = findViewById(R.id.CountTextView);
listView = findViewById(R.id.listView);
adapter = new ListView_Adapter(this, arrayList);
listView.setAdapter(adapter);
sortBy = findViewById(R.id.SortByButton);
addDebtorButton = findViewById(R.id.AddDebtorButton);
/* TO DO */
displayListViewContents();
setTextTotal();
/* OnClickListeners below */
/* Listview item */
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
makeDialog(ListView_Adapter.modellist.get(i).getModelContact(),
ListView_Adapter.modellist.get(i).getModelDebt(),
ListView_Adapter.modellist.get(i).getModelDesc(),
ListView_Adapter.modellist.get(i).getModelDate());
}
});
/* Sort by button*/
sortBy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sortArray();
sortArrayList();
}
});
/* Debtor Button*/
addDebtorButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(Loanist_Full.this, Loanist_Add_Debtor.class));
}
});
} /* END onCreate*/
/* Display listview contents */
private void displayListViewContents(){
/* Display contents of listview*/
for(int i = 0; i < mDebt.length; i++){
model = new ListView_Model(mContact[i], mDebt[i], mDesc[i], mDate[i], mImg[i]);
arrayList.add(model);
}
totalCount.setText("Count: " + adapter.getCount());
adapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
totalCount.setText("Count: " + adapter.getCount());
}
});
}
/* Set "Total" view text */
private void setTextTotal(){
/* Set text of Total based on total amt of mDebt*/
for (double x: mDebt) { //Compute total mDebt
mDebtTotal += x;
}
if (mDebtTotal == 0){ //If mDebtTotal == 0
totalText.setText("Create a debt with an amount first.");
} else {
totalText.setText("Total: " + currency + " " + formatter.format(mDebtTotal));
}
}
/* Sort item list */
private void sortArray(){
Arrays.sort(mContact);
adapter.notifyDataSetChanged();
}
/* Sort array list */
private void sortArrayList(){
Collections.sort(arrayList, new Comparator<ListView_Model>() {
@Override
public int compare(ListView_Model listView_model, ListView_Model t1) {
return listView_model.getModelContact().compareTo(t1.getModelContact());
}
});
adapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
searchItem = menu.findItem(R.id.app_searchbutton);
searchItem.getActionView();
searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Enter a contact e.g 'Robert'");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)){
adapter.filter("");
listView.clearTextFilter();
}
else {
adapter.filter(newText);
}
//totalCount.setText("Count: " + adapter.getCount());
return true;
}
});
return true;
}
/* Creates a dialog whenever an item on listview is clicked*/
private void makeDialog(String contact, double debtAmt, String debtDesc, String debtDate){
AlertDialog alertDialog = new AlertDialog.Builder(Loanist_Full.this).create();
alertDialog.setTitle("Debt Information");
if (debtDesc.length() < 1)
debtDesc = "N/A";
if (debtDate.length() < 1)
debtDate = "N/A";
alertDialog.setMessage("\n" + contact + "'s debt is " + this.currency + " " + debtAmt + "\n\nDescription: " + debtDesc + "\nDebt created: " + debtDate);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".Loanist_Full">
<LinearLayout
android:id="@+id/TotalBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#eaeaea"
android:orientation="horizontal"
>
<TextView
android:id="@+id/TotalTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:text="Total"
android:textSize="16dp"
android:textStyle="bold"
android:layout_weight="1"
/>
<TextView
android:id="@+id/CountTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="15dp"
android:gravity="center_vertical"
android:text="Count"
android:textSize="14dp"
/>
<ImageButton
android:id="@+id/SortByButton"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginRight="15dp"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_sort_black_24dp"
android:background="?android:selectableItemBackground"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="3.0dip"
android:background="@drawable/divider_total"
android:layout_below="@id/TotalBar"
/>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@drawable/divider_listview"
android:dividerHeight="0.2dp"
android:layout_below="@id/TotalBar"
/>
<com.getbase.floatingactionbutton.AddFloatingActionButton
android:id="@+id/AddDebtorButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="8dp"
app:fab_colorNormal="@color/lightYellow"
app:fab_colorPressed="@color/lightYellowPressed"
/>
</RelativeLayout>
package com.memger.pautanganapp;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class ListView_Adapter extends BaseAdapter {
Context mcontext;
LayoutInflater inflater;
public static List<ListView_Model> modellist;
ArrayList<ListView_Model> arrayList;
public ListView_Adapter(Context context, List<ListView_Model> modellist){
mcontext = context;
inflater = LayoutInflater.from(mcontext);
this.modellist = modellist;
this.arrayList = new ArrayList<ListView_Model>();
this.arrayList.addAll(modellist);
}
public class ViewHolder{
TextView myContact, myDebt, myDesc, myDate;
ImageView myImg;
}
@Override
public int getCount() {
return modellist.size();
}
@Override
public Object getItem(int i) {
return modellist.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder holder;
if (convertView == null){
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.listview_row, null);
holder.myContact = convertView.findViewById(R.id.Contact);
holder.myDebt = convertView.findViewById(R.id.Debt);
holder.myDesc = convertView.findViewById(R.id.Desc);
holder.myDate = convertView.findViewById(R.id.Date);
holder.myImg = convertView.findViewById(R.id.ContactImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.myContact.setText(modellist.get(position).getModelContact());
holder.myDebt.setText(Loanist_Full.currency + " " + Loanist_Full.formatter.format(modellist.get(position).getModelDebt()));
holder.myDate.setText(modellist.get(position).getModelDate());
holder.myDesc.setText(modellist.get(position).getModelDesc());
holder.myImg.setImageResource(modellist.get(position).getModelImg());
return convertView;
}
/* Filter */
public void filter(String charText){
charText = charText.toLowerCase(Locale.getDefault());
modellist.clear();
if (charText.length()==0){
modellist.addAll(arrayList);
}
else {
for (ListView_Model model : arrayList){
if (model.getModelContact().toLowerCase(Locale.getDefault()).contains(charText)){
modellist.add(model); //Add to modellist those who contain the words in the search bar
}
}
}
notifyDataSetChanged();
}
}