我正在搜索互联网,但我没有解决我的问题。
我有五个TextView的ListView。 TextView中的一个在某些情况下是彩色的。除非我的ListView未被过滤,否则一切正常。在过滤列表视图后,textview mojView_holder.tekst_wart
中的颜色更改(在getView中切换)无法正常工作并保持默认状态。
也许有些人可以帮我找到解决方案,请...
这是我的适配器代码的片段:
public class wiersz_listy_zamowien extends BaseAdapter implements Filterable {
ArrayList<Zamowienie> zamowienia;
CustomFilter filter;
ArrayList<Zamowienie> filterList;
private Context ctx;
public wiersz_listy_zamowien(Context ctx, ArrayList<Zamowienie> zamowienia) {
this.ctx = ctx;
this.zamowienia = zamowienia;
this.filterList = zamowienia;
}
@Override
public int getCount() {
return zamowienia.size();
}
@Override
public Object getItem(int position) {
return zamowienia.get(position);
}
@Override
public long getItemId(int position) {
return zamowienia.indexOf(getItem(position));
}
private class ViewHolderPattern {
TextView tekst_nr;
TextView tekst_naz;
TextView tekst_wart;
TextView tekst_data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderPattern mojView_holder;
if (convertView == null) {
mojView_holder = new ViewHolderPattern();
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.wiersz_listy_zamowien, parent, false);
mojView_holder.tekst_nr = (TextView) convertView.findViewById(R.id.nr_zam);
mojView_holder.tekst_naz = (TextView) convertView.findViewById(R.id.nazwa_zam);
mojView_holder.tekst_wart = (TextView) convertView.findViewById(R.id.wart_zam);
mojView_holder.tekst_data = (TextView) convertView.findViewById(R.id.data_zloz_zam);
convertView.setTag(mojView_holder);
} else {
mojView_holder = (ViewHolderPattern) convertView.getTag();
}
//Ustawienie danych
mojView_holder.tekst_nr.setText(zamowienia.get(position).getId());
mojView_holder.tekst_naz.setText(zamowienia.get(position).getNazwa());
mojView_holder.tekst_data.setText(zamowienia.get(position).getData());
mojView_holder.tekst_wart.setText(String.format("%.2f", Float.parseFloat( zamowienia.get(position).getPlatn() )) + " zł ");
//kolory kwoty zależne od statusu płatności
switch(zamowienia.get(position).getStatusPlatn()) {
case "0": mojView_holder.tekst_wart.setTextColor(convertView.getContext().getResources().getColor(R.color.zamZgloszColor)); break;
default: mojView_holder.tekst_wart.setTextColor(0xFF000000); //silver
}
return convertView;
}
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
if(filter == null)
{
filter=new CustomFilter();
}
return filter;
}
//INNER CLASS
class CustomFilter extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
// TODO Auto-generated method stub
FilterResults results=new FilterResults();
if(constraint != null && constraint.length()>0)
{
//CONSTARINT TO UPPER
constraint=constraint.toString().toUpperCase();
ArrayList<Zamowienie> filters=new ArrayList<Zamowienie>();
//get specific items
for(int i=0;i<filterList.size();i++)
{
if(filterList.get(i).getNazwa().toUpperCase().contains(constraint))
{
Zamowienie p=new Zamowienie(filterList.get(i).getId(), filterList.get(i).getNazwa(), filterList.get(i).getPlatn(), filterList.get(i).getStatus(), filterList.get(i).getStatusPlatn(), filterList.get(i).getData());
filters.add(p);
}
}
results.count=filters.size();
results.values=filters;
}else
{
results.count=filterList.size();
results.values=filterList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
// TODO Auto-generated method stub
zamowienia=(ArrayList<Zamowienie>) results.values;
notifyDataSetChanged();
}
}
答案 0 :(得分:1)
你可以从这段代码中获取参考,因为在过滤后你的listview项目id被改变了。所以实现了getItemId方法。
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
/**
* Created by windev on 8/2/2016.
*/
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MyViewHolder> implements Filterable {
private ArrayList<DataModel> dataSet;
public ArrayList<DataModel> orig;
Context context;
//String url="http://focusmedica.com/FMAPPS/appcatalogue/";
String url="http://192.168.1.44:81/DOMAINS/Focusmedica.com/FMAPPS/appcatalogue/";
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tvChapterName;
ImageView ivChapterIcon;
public MyViewHolder(View itemView) {
super(itemView);
this.tvChapterName = (TextView) itemView.findViewById(R.id.tvChapterName);
this.ivChapterIcon = (ImageView) itemView.findViewById(R.id.ivChapterIcon);
}
}
public MainAdapter(Context context,ArrayList<DataModel> data) {
this.dataSet = data;
this.context=context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout, parent, false);
view.setOnClickListener(MainActivity.myOnClickListener);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public long getItemId(int position) {
int itemID;
// orig will be null only if we haven't filtered yet:
if (orig == null) {
itemID = position;
} else {
itemID = orig.indexOf(dataSet.get(position));
}
return itemID;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {
TextView tvChapterName = holder.tvChapterName;
ImageView ivChapterIcon = holder.ivChapterIcon;
String chapterName=dataSet.get(listPosition).getChapterName();
String iconName = chapterName.replaceAll(" ", "_");
tvChapterName.setText(chapterName);
Picasso.with(context).load(url+iconName+".png").into(ivChapterIcon);
//ivChapterIcon.setImageResource(R.drawable.caticon);
}
@Override
public int getItemCount() {
return dataSet.size();
}
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<DataModel> results = new ArrayList<DataModel>();
if (orig == null)
orig = dataSet;
if (constraint != null) {
if (orig != null && orig.size() > 0) {
for (final DataModel g : orig) {
if (g.getChapterName().toLowerCase().contains(constraint.toString().toLowerCase())||
g.getChapterName().toUpperCase().contains(constraint.toString().toUpperCase()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
dataSet = (ArrayList<DataModel>) results.values;
notifyDataSetChanged();
}
};
}
}