我已经开始学习安卓了 我正在处理列表视图我想在列表视图中搜索项目。 gi经历了很多帖子,并编写了以下代码,但有些代码是如何工作的。
QyestionActivity
public class QuestionActivity extends ActionBarActivity {
String filepath = null;
String question_filepath = null;
Controller c;
ListView question_list_view;
EditText txt_search;
ArrayList<SingleQuestion> question_list;
Main_QuestionList_Adapter questionlistAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question);
filepath = Constant.application_file_path;
question_filepath = filepath + "questions.txt";
File test_fiFile = new File(question_filepath);
if (!test_fiFile.exists()) {
copyAssets();
}
new ParseQuestionFIle().parseQuestionfile(question_filepath);
c = Controller.getController();
question_list = c.getQuestions().getQuestions();
question_list_view = (ListView) findViewById(R.id.lst_questionlit);
txt_search = (EditText) findViewById(R.id.editText1);
questionlistAdapter = new Main_QuestionList_Adapter(
QuestionActivity.this);
question_list_view.setAdapter(questionlistAdapter);
question_list_view.setTextFilterEnabled(true);
}
protected void onResume() {
super.onResume();
txt_search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
questionlistAdapter.getFilter().filter(s);
questionlistAdapter.notifyDataSetInvalidated();
questionlistAdapter.notifyDataSetChanged();
question_list_view.setAdapter(questionlistAdapter);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
适配器类
public class Main_QuestionList_Adapter extends BaseAdapter实现了Filterable {
Controller c = Controller.getController();
ArrayList<SingleQuestion> questions_aerraylist;
LayoutInflater inflater;
Context context;
SingleQuestion singleQuestion;
Filter filter;
public Main_QuestionList_Adapter(Context context) {
this.context = context;
questions_aerraylist = new ArrayList<SingleQuestion>();
questions_aerraylist = c.getQuestions().getQuestions();
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return questions_aerraylist.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View currentView, ViewGroup parentView) {
QuestioViewHolder questionviewholder;
if (currentView == null) {
currentView = inflater
.inflate(
com.example.ten_step.R.layout.main_question_list_adapter_view,
null);
questionviewholder = new QuestioViewHolder();
currentView.setTag(questionviewholder);
} else {
questionviewholder = (QuestioViewHolder) currentView.getTag();
}
questionviewholder.question_text_view = (TextView) currentView
.findViewById(com.example.ten_step.R.id.txt_question_text);
singleQuestion = new SingleQuestion();
singleQuestion = questions_aerraylist.get(position);
questionviewholder.question_text_view.setText(singleQuestion
.getQuestion().toString());
questionviewholder.question_text_view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(singleQuestion != null)
{
c.setSelected_question(questions_aerraylist.get(position));
Toast.makeText(context, c.getSelected_question().getQuestion(), Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(context, "Please Tr Againg", Toast.LENGTH_LONG).show();
}
}
});
return currentView;
}
class QuestioViewHolder {
TextView question_text_view;
}
@Override
public Filter getFilter() {
if (filter == null) {
filter = new QuestionFilter();
}
return filter;
}
private class QuestionFilter extends Filter {
ArrayList<SingleQuestion> actual_list;
ArrayList<SingleQuestion> sorted_list;
Controller controller;
public QuestionFilter() {
actual_list = new ArrayList<SingleQuestion>();
controller = Controller.getController();
actual_list = controller.getQuestions().getQuestions();
sorted_list = new ArrayList<SingleQuestion>();
}
@Override
protected FilterResults performFiltering(CharSequence searchQuer) {
String search_query = searchQuer.toString().toLowerCase();
FilterResults result = new FilterResults();
if(search_query != null || search_query.length() !=0 )
{
for (int i = 0 ; i < actual_list.size() ; i++)
{
if(actual_list.get(i).getQuestion().toLowerCase().contains(search_query) && !sorted_list.contains(actual_list.get(i)) )
{
sorted_list.add(actual_list.get(i));
}
}
if(sorted_list.size() !=0)
{
result.values = sorted_list;
result.count = sorted_list.size();
}
}
else
{
result.values = actual_list;
result.count = actual_list.size();
}
c.getQuestions().setQuestion((ArrayList<SingleQuestion>) result.values);
return result;
}
@Override
protected void publishResults(CharSequence searchQuer, FilterResults results) {
ArrayList<SingleQuestion> new_sorted_list = (ArrayList<SingleQuestion>) results.values;
if (results.count > 0) {
c.getQuestions().setQuestion(new_sorted_list);
// notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
答案 0 :(得分:0)
我已经以不同的方式搜索了列表视图。 我使用Textwatcher而不是实现适配器的Filterable接口。
请查看以下代码并提供建议(如果有)。
public class QuestionActivity extends ActionBarActivity {
String filepath = null;
String question_filepath = null;
Controller c;
ListView question_list_view;
EditText txt_search;
ArrayList<SingleQuestion> question_list;
Main_QuestionList_Adapter questionlistAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question);
filepath = Constant.application_file_path;
question_filepath = filepath + "questions.txt";
File test_fiFile = new File(question_filepath);
if (!test_fiFile.exists()) {
copyAssets();
}
new ParseQuestionFIle().parseQuestionfile(question_filepath);
c = Controller.getController();
question_list = c.getQuestions().getQuestions();
question_list_view = (ListView) findViewById(R.id.lst_questionlit);
txt_search = (EditText) findViewById(R.id.editText1);
questionlistAdapter = new Main_QuestionList_Adapter(
QuestionActivity.this , c.getQuestions().getQuestions());
question_list_view.setAdapter(questionlistAdapter);
question_list_view.setTextFilterEnabled(true);
txt_search.addTextChangedListener(searchTextChangeListener);
}
protected void onResume() {
super.onResume();
}
TextWatcher searchTextChangeListener = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
ArrayList<SingleQuestion> defaultQuestinList = c.getQuestions()
.getQuestions();
if (s.length() > 0) {
Set<SingleQuestion> sortedQuestionSet = new HashSet<SingleQuestion>();
int questionListCount = defaultQuestinList.size();
String searchQuery = s.toString().toLowerCase();
for (int i = 0; i < questionListCount; i++) {
if (defaultQuestinList.get(i).getQuestion().toLowerCase()
.contains(searchQuery)) {
sortedQuestionSet.add(defaultQuestinList.get(i));
}
}
ArrayList<SingleQuestion> sortedQuestionList = new ArrayList<SingleQuestion>();
sortedQuestionList.addAll(sortedQuestionSet);
questionlistAdapter = new Main_QuestionList_Adapter(QuestionActivity.this ,sortedQuestionList);
question_list_view.setAdapter(questionlistAdapter);
}
else
{
Questions q = c.getQuestions();
q.setQuestion(defaultQuestinList);
questionlistAdapter = new Main_QuestionList_Adapter(QuestionActivity.this ,defaultQuestinList);
question_list_view.setAdapter(questionlistAdapter);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
以上代码对我来说完全没问题。