这是我的适配器类:
public class Searchlistviewadapter extends ArrayAdapter<DataModel> implements
Filterable {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<DataModel> datamodel;
ArrayList<DataModel> productlist;
private ProductFilter filter;
private SparseBooleanArray mSelectedItemsIds;
MyDatabaseHelper dbhelper;
int counter;
int Quantity = 0;
HashMap<Integer, Integer> hashMap;
int searchBy = 0;
static final String TAG = "LISTT";
List<DataModel> listDatamodels = new ArrayList<DataModel>();
public Searchlistviewadapter(Context context, int resourceId,
ArrayList<DataModel> worldpopulationlist) {
super(context, resourceId, worldpopulationlist);
mSelectedItemsIds = new SparseBooleanArray();
this.context = context;
this.datamodel = worldpopulationlist;
inflater = LayoutInflater.from(context);
dbhelper = new MyDatabaseHelper(context);
productlist = worldpopulationlist;
hashMap = new HashMap<Integer, Integer>();
}
private class ViewHolder {
TextView tv;
TextView Quantity;
TextView cost;
ImageView img;
ImageView plusitem;
ImageView minusitem;
TextView itemnumber;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listitem, null);
//
holder.tv = (TextView) view.findViewById(R.id.textView1);
holder.img = (ImageView) view.findViewById(R.id.imageView1);
holder.Quantity = (TextView) view.findViewById(R.id.textView3);
holder.cost = (TextView) view.findViewById(R.id.textView2);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Capture position and set to the TextViews
String rupee = context.getResources().getString(R.string.Rs);
final MyDatabaseHelper dbManager = new MyDatabaseHelper(context);
dbManager.open();
holder.tv.setText(datamodel.get(position).getProductname());
holder.Quantity.setText(datamodel.get(position).getProdcutQuantity());
holder.cost.setText(rupee + datamodel.get(position).getProdcutCost());
holder.img.setImageResource(datamodel.get(position).getProductimage());
holder.plusitem = (ImageView) view.findViewById(R.id.imageButton2);
holder.plusitem.setTag(position);
holder.minusitem = (ImageView) view.findViewById(R.id.imageButton);
holder.minusitem.setTag(position);
holder.itemnumber = (TextView) view.findViewById(R.id.textView4);
holder.plusitem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
int index = Integer.parseInt(arg0.getTag().toString());
Log.e(TAG, Integer.toString(index));
int quantity;
if (hashMap.containsKey(index)) {
quantity = hashMap.get(index);
} else
quantity = 0;
quantity++;
hashMap.put(index, quantity);
holder.itemnumber.setText(quantity + "");
// holder.itemnumber.invalidate();
dbhelper.open();
String producttype = dbhelper.getCteogryusingId(datamodel.get(
position).getProdcutid());
dbManager.AddShopingItem(
datamodel.get(position).getProdcutid(), producttype,
datamodel.get(position).getProductname(), datamodel
.get(position).getProdcutQuantity(), datamodel
.get(position).getProdcutCost(), String
.valueOf(datamodel.get(position)
.getProductimage()), String
.valueOf(quantity), "true");
sendBroadcaset(true);
}
});
holder.minusitem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int index = Integer.parseInt(v.getTag().toString());
Log.e(TAG, Integer.toString(index));
int quantity;
if (hashMap.containsKey(index)) {
quantity = hashMap.get(index);
} else
quantity = 0;
quantity--;
if (quantity < 0)
quantity = 0;
hashMap.put(index, quantity);
holder.itemnumber.setText(quantity + "");
// holder.itemnumber.invalidate();
dbhelper.open();
String producttype = dbhelper.getCteogryusingId(datamodel.get(
position).getProdcutid());
dbManager.AddShopingItem(
datamodel.get(position).getProdcutid(), producttype,
datamodel.get(position).getProductname(), datamodel
.get(position).getProdcutQuantity(), datamodel
.get(position).getProdcutCost(), String
.valueOf(datamodel.get(position)
.getProductimage()), String
.valueOf(quantity), "true");
sendBroadcaset(false);
}
});
return view;
}
private void sendBroadcaset(boolean b) {
// TODO Auto-generated method stub
LocalBroadcastManager.getInstance(context).sendBroadcast(
new Intent("update_boolean_variable").putExtra("action", b));
}
private class ProductFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();
if (constraint != null && constraint.toString().length() > 0) {
ArrayList<DataModel> filteredItems = new ArrayList<DataModel>();
for (int i = 0, l = datamodel.size(); i < l; i++) {
DataModel country = datamodel.get(i);
if (country.getProductname().toString().toLowerCase()
.contains(constraint))
filteredItems.add(country);
}
result.count = filteredItems.size();
result.values = filteredItems;
} else {
synchronized (this) {
result.values = datamodel;
result.count = datamodel.size();
}
}
return result;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
productlist = (ArrayList<DataModel>) results.values;
notifyDataSetChanged();
clear();
for (int i = 0, l = productlist.size(); i < l; i++)
add(productlist.get(i));
notifyDataSetChanged();
}
}
@Override
public Filter getFilter() {
if (filter == null) {
filter = new ProductFilter();
}
return filter;
}
}
这是我的活动claSS:
public class MainActivity extends Activity implements View.OnClickListener {
ImageView backbutton;
private ListView lv;
Searchlistviewadapter listviewadapter;
EditText inputSearch;
ArrayList<HashMap<String, String>> productList;
MyDatabaseHelper dbhelper;
public static final int img2 = R.drawable.asirwadatata;
public static final int img3 = R.drawable.asirwadatata;
public static final int img4 = R.drawable.asirwadatata;
public static final int img5 = R.drawable.asirwadatata;
public static final int img6 = R.drawable.asirwadatata;
public static final int img7 = R.drawable.asirwadatata;
ArrayList<DataModel> lstDataModel;
public static final int[] images = new int[] { img2, img3, img4, img5,
img6, img7 };
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbhelper = new MyDatabaseHelper(MainActivity.this);
dbhelper.open();
String ProductId[] = dbhelper.getAllProductID();
dbhelper.open();
String products[] = dbhelper.getAllProductName();
dbhelper.open();
String productsquant[] = dbhelper.getAllProductQuntity();
dbhelper.open();
String productcost[] = dbhelper.getAllProductCost();
lv = (ListView) findViewById(R.id.listView1);
inputSearch = (EditText) findViewById(R.id.inputSearch);
lstDataModel = new ArrayList<DataModel>();
for (int i = 0; i < products.length; i++) {
DataModel datamodel = new DataModel();
datamodel.setProdcutid(ProductId[i]);
datamodel.setProductname(products[i]);
datamodel.setProdcutQuantity(productsquant[i]);
datamodel.setProdcutCost(productcost[i]);
datamodel.setProductimage(images[i]);
lstDataModel.add(datamodel);
}
listviewadapter = new Searchlistviewadapter(MainActivity.this,
R.layout.listitem, lstDataModel);
lv.setAdapter(listviewadapter);
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
System.out.println("Text [" + cs + "]");
listviewadapter.getFilter().filter(cs.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onClick(View v) {
}
}
的DataModel:
public class DataModel {
public String getProdcutid() {
return prodcutid;
}
public void setProdcutid(String prodcutid) {
this.prodcutid = prodcutid;
}
private String prodcutid;
private String Cateogry;
private String Productname;
private String ProdcutQuantity;
private int Productimage;
private String ProdcutCost;
private String NuberofProductQuantity;
public String eachProdcutcount;
public String getEachProdcutcount() {
return eachProdcutcount;
}
public void setEachProdcutcount(String eachProdcutcount) {
this.eachProdcutcount = eachProdcutcount;
}
public DataModel() {
// TODO Auto-generated constructor stub
}
public DataModel(String productname, String prodcutQuantity,
int productimage) {
super();
this.Productname = productname;
this.ProdcutQuantity = prodcutQuantity;
this.Productimage = productimage;
}
public String getCateogry() {
return Cateogry;
}
public void setCateogry(String cateogry) {
Cateogry = cateogry;
}
public String getProductname() {
return Productname;
}
public void setProductname(String productname) {
Productname = productname;
}
public String getProdcutQuantity() {
return ProdcutQuantity;
}
public void setProdcutQuantity(String prodcutQuantity) {
ProdcutQuantity = prodcutQuantity;
}
public int getProductimage() {
return Productimage;
}
public void setProductimage(int productimage) {
Productimage = productimage;
}
public String getProdcutCost() {
return ProdcutCost;
}
public void setProdcutCost(String prodcutCost) {
ProdcutCost = prodcutCost;
}
public String getNuberofProductQuantity() {
return NuberofProductQuantity;
}
public void setNuberofProductQuantity(String nuberofProductQuantity) {
NuberofProductQuantity = nuberofProductQuantity;
}
我正在尝试从Listview过滤或搜索项目我能够过滤数据但问题当我清除数据时我的意思是当我们从编辑文本过滤后空白数据然后我的listview项目再次变为空白我们有启动应用程序然后项目列表视图项目变得可见请告诉我哪里出错了请建议我。
答案 0 :(得分:0)
我已更改您的适配器类..请尝试使用以下类
public class Searchlistviewadapter extends ArrayAdapter<DataModel> implements
Filterable {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<DataModel> datamodel;
ArrayList<DataModel> productlist;
private ProductFilter filter;
private SparseBooleanArray mSelectedItemsIds;
MyDatabaseHelper dbhelper;
int counter;
int Quantity = 0;
HashMap<Integer, Integer> hashMap;
int searchBy = 0;
static final String TAG = "LISTT";
List<DataModel> listDatamodels = new ArrayList<DataModel>();
public Searchlistviewadapter(Context context, int resourceId,
ArrayList<DataModel> worldpopulationlist) {
super(context, resourceId, worldpopulationlist);
mSelectedItemsIds = new SparseBooleanArray();
this.context = context;
// this.datamodel = worldpopulationlist;
this.datamodel = new ArrayList<DataModel>();
// productlist = worldpopulationlist;
this.datamodel.addAll(worldpopulationlist);
inflater = LayoutInflater.from(context);
dbhelper = new MyDatabaseHelper(context);
productlist = new ArrayList<DataModel>();
// productlist = worldpopulationlist;
productlist.addAll(worldpopulationlist);
hashMap = new HashMap<Integer, Integer>();
}
private class ViewHolder {
TextView tv;
TextView Quantity;
TextView cost;
ImageView img;
ImageView plusitem;
ImageView minusitem;
TextView itemnumber;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listitem, null);
//
holder.tv = (TextView) view.findViewById(R.id.textView1);
holder.img = (ImageView) view.findViewById(R.id.imageView1);
holder.Quantity = (TextView) view.findViewById(R.id.textView3);
holder.cost = (TextView) view.findViewById(R.id.textView2);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Capture position and set to the TextViews
String rupee = context.getResources().getString(R.string.Rs);
final MyDatabaseHelper dbManager = new MyDatabaseHelper(context);
dbManager.open();
holder.tv.setText(datamodel.get(position).getProductname());
holder.Quantity.setText(datamodel.get(position).getProdcutQuantity());
holder.cost.setText(rupee + datamodel.get(position).getProdcutCost());
holder.img.setImageResource(datamodel.get(position).getProductimage());
holder.plusitem = (ImageView) view.findViewById(R.id.imageButton2);
holder.plusitem.setTag(position);
holder.minusitem = (ImageView) view.findViewById(R.id.imageButton);
holder.minusitem.setTag(position);
holder.itemnumber = (TextView) view.findViewById(R.id.textView4);
holder.plusitem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
int index = Integer.parseInt(arg0.getTag().toString());
Log.e(TAG, Integer.toString(index));
int quantity;
if (hashMap.containsKey(index)) {
quantity = hashMap.get(index);
} else
quantity = 0;
quantity++;
hashMap.put(index, quantity);
holder.itemnumber.setText(quantity + "");
// holder.itemnumber.invalidate();
dbhelper.open();
String producttype = dbhelper.getCteogryusingId(datamodel.get(
position).getProdcutid());
dbManager.AddShopingItem(
datamodel.get(position).getProdcutid(), producttype,
datamodel.get(position).getProductname(), datamodel
.get(position).getProdcutQuantity(), datamodel
.get(position).getProdcutCost(), String
.valueOf(datamodel.get(position)
.getProductimage()), String
.valueOf(quantity), "true");
sendBroadcaset(true);
}
});
holder.minusitem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int index = Integer.parseInt(v.getTag().toString());
Log.e(TAG, Integer.toString(index));
int quantity;
if (hashMap.containsKey(index)) {
quantity = hashMap.get(index);
} else
quantity = 0;
quantity--;
if (quantity < 0)
quantity = 0;
hashMap.put(index, quantity);
holder.itemnumber.setText(quantity + "");
// holder.itemnumber.invalidate();
dbhelper.open();
String producttype = dbhelper.getCteogryusingId(datamodel.get(
position).getProdcutid());
dbManager.AddShopingItem(
datamodel.get(position).getProdcutid(), producttype,
datamodel.get(position).getProductname(), datamodel
.get(position).getProdcutQuantity(), datamodel
.get(position).getProdcutCost(), String
.valueOf(datamodel.get(position)
.getProductimage()), String
.valueOf(quantity), "true");
sendBroadcaset(false);
}
});
return view;
}
private void sendBroadcaset(boolean b) {
// TODO Auto-generated method stub
LocalBroadcastManager.getInstance(context).sendBroadcast(
new Intent("update_boolean_variable").putExtra("action", b));
}
private class ProductFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = null;
try {
String filterString = constraint.toString().toLowerCase();
results = new FilterResults();
List<DataModel> list = productlist;
ArrayList<DataModel> nlist = new ArrayList<DataModel>();
if (constraint != null && constraint.toString().length() > 0) {
for (int i = 0; i < productlist.size(); i++) {
DataModel filterableString = list.get(i);
if (filterableString.getProductname().toString()
.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
} else {
synchronized (this) {
results.values = datamodel;
results.count = datamodel.size();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
try {
datamodel = (ArrayList<DataModel>) results.values;
notifyDataSetChanged();
clear();
for (int i = 0, l = datamodel.size(); i < l; i++)
add(datamodel.get(i));
notifyDataSetInvalidated();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public Filter getFilter() {
if (filter == null) {
filter = new ProductFilter();
}
return filter;
}
}