我想搜索演员,但我收到了这个错误。我之前做过但是,现在我正在尝试不同的适配器。它可能是如此基本,但我无法解决它。请帮忙。感谢。
public class Actors extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.movie_list);
ArrayList<Actor> actors = new ArrayList<Actor>();
actors.add(new Actor("jacknicholson",1,R.drawable.jacknicholson));
actors.add(new Actor("Marlon Brando",2,R.drawable.marlonbrando));
actors.add(new Actor("Robert De Niro",3,R.drawable.robertdeniro));
actors.add(new Actor("Al Pacino",4,R.drawable.alpacino));
actors.add(new Actor("Danel Day-Lewis",5,R.drawable.danielday));
actors.add(new Actor("Dustin Hoffman",6,R.drawable.dustinhoffman));
final MovieAdapter itemsAdapter = new MovieAdapter(this, actors);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(itemsAdapter);
EditText theFilter =(EditText) findViewById(R.id.editt);
theFilter.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) {
//Here I'm getting an error
(Actors.this).itemsAdapter.getFilter().filter(charSequence);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
}
这是我的电影适配器。在Actors类中我不能使用(Actors.this)。我不能将它用作itemsAdapter.getFilter().filter(char)
,但通过这种方式它找不到任何东西。
public class MovieAdapter extends ArrayAdapter<Actor> {
public MovieAdapter(Activity context, ArrayList<Actor> words) {
super(context, 0, words);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Actor currentWord = getItem(position);
TextView songTextView = (TextView) listItemView.findViewById(R.id.Name_text_view);
songTextView.setText((currentWord.getmPopularity()));
final TextView singerTextView = (TextView) listItemView.findViewById(R.id.popularity_text_view);
singerTextView.setText(currentWord.getmActor());
ImageView iconView = (ImageView) listItemView.findViewById(R.id.list_item_icon);
iconView.setImageResource(currentWord.getImageResourceId());
return listItemView;
}
}
这是演员类
package com.example.aylin.movieapp;
import java.util.Comparator;
/ ** *由Aylin于2.04.2018创建。 * /
public class Actor {
private String mActor;
private double mPopularity;
private int mImageResourceId;
public Actor(String mActor, double mPopularity, int imageResourceId){
this.mActor = mActor;
this.mPopularity = mPopularity;
mImageResourceId = imageResourceId;
}
public String getmPopularity() {
String pop = Double.toString(mPopularity);
return pop; }
public String getmActor() { return mActor; }
public int getImageResourceId() { return mImageResourceId; }
/*Comparator for sorting the list by roll no*/
}
忽略:afdsdgjhjgkıfgdsgjhsfdhfhafdgfhkjhılşdsfhljkipşdjlhişkdsfl
答案 0 :(得分:0)
完全省略(Actors.this).
。
匿名内部类可以引用包含方法的所有final
变量。
答案 1 :(得分:-1)
我认为问题是Actor
不会覆盖toString()
。当然,使用ArrayAdapter.getFilter()
的sh * tty文档,您要么必须猜测返回的Filter
做了什么,要么查看源代码,这就是我所做的,并调用{{ 1 {} toString()
负责的项目。因此,覆盖ArrayAdapter
中的toString()
以便它返回Actor
可能会解决问题。