我的ListView
在启动时工作正常。问题是,每当我单击触发搜索视图的搜索Button
时,我的ListView
上的所有项目都会消失。即使清除了searchview
上的文本字段,我的ListView
仍处于空白状态。
Loanist_Full.java(主要活动)
package 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();
}
}
Loanist_full.xml
<?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>
ListView_Adapter.java
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();
}
}