可过滤的界面确定,但BaseAdapter的getview位置错误

时间:2016-05-01 16:15:34

标签: android listview

我实施了GrantResourceOwnerCredentials和自定义SearchView来过滤我的应用中的Filter。我遇到了这个问题:当我在输入ListView字段后点击ListView并点击某个项目时,它会按原样打开一个新活动,但它们会获得所有错误的数据位置。我试图放入onclickListener SeachView,但它的“word”字符串的位置错误。

MainActivity

i.putExtra("word", word);

ListViewAdapter

public class MainActivity extends Activity implements
    SearchView.OnQueryTextListener {

    ListView list;
    SearchView mSearchView;

    ArrayList<Vocabulary> vocabularylist;
    ListViewAdapter adapter;
    String[] definition;
    String[] word;
    String[] example;
    int[] flag;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.listview_main);

        definition = new String[] { "loira burra", 
                                    "arrasa-corações",
                                    "vagabundo"};

        word = new String[] { "bimbo", 
                              "heartthrob",
                              "good-for-nothing"};

        example = new String[] { "✎ She's a real bimbo.", 
                                 "✎ He's a real heartthrob.",
                                 "✎ What are you doing wasting time here? Get a job, you good-for-nothing!"};

        flag = new int[] { R.drawable.bimbo, 
                           R.drawable.heartthrob, 
                           R.drawable.vagabundo};

        list = (ListView) findViewById(R.id.listview);
        mSearchView = (SearchView) findViewById(R.id.search_view);

        vocabularylist = new ArrayList<Vocabulary>();
        for (int i = 0; i < word.length; i++) {
            Vocabulary vocabulary = new Vocabulary(word[i], definition[i], example[i],
                    flag[i]);
            vocabularylist.add(vocabulary);
        }

        adapter = new ListViewAdapter(getApplicationContext(), vocabularylist, word, definition, example, flag);

        list.setAdapter(adapter);
        list.setTextFilterEnabled(true);
        setupSearchView();
        // Capture ListView item click
        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
                Intent i = new Intent(MainActivity.this, SingleItemView.class);
                // Pass all data definition
                Vocabulary vocabulary = (Vocabulary) adapter.getItem(position);

                i.putExtra("definition", vocabulary.getDefinition());
                // Pass word
                i.putExtra("word", vocabulary.getWord());
                // Pass example
                i.putExtra("example", vocabulary.getExample());
                // Pass flag
                i.putExtra("flag", vocabulary.getFlag());
                // Pass position
                i.putExtra("position", position);
                // Open SingleItemView.java Activity
                startActivity(i);
            }
        }); 
    }

    private void setupSearchView() {
        mSearchView.setIconifiedByDefault(false);
        mSearchView.setOnQueryTextListener(this);
        mSearchView.setSubmitButtonEnabled(true); 
        mSearchView.setQueryHint("Search Here");
    }

    public boolean onQueryTextChange(String newText) {
        adapter.getFilter().filter(newText);
        return false;
    }

    public boolean onQueryTextSubmit(String query) {
        return false;
    }
}

词汇

public class ListViewAdapter extends BaseAdapter implements Filterable {
    Context context;
    String[] word;
    String[] definition;
    String[] example;
    int[] flag;
    LayoutInflater inflater;
    ArrayList<Vocabulary> vocabularylist;
    ArrayList<Vocabulary> mStringFilterList;
    ValueFilter valueFilter;

    public ListViewAdapter(Context context, ArrayList<Vocabulary> vocabularylist, String[] word, String[] definition,
            String[] example, int[] flag) {
        this.context = context;
        this.word = word;
        this.definition = definition;
        this.example = example;
        this.flag = flag;
        this.vocabularylist = vocabularylist;
        mStringFilterList = vocabularylist;
    }

    @Override
    public int getCount() {
        return vocabularylist.size();
    }

    @Override
    public Object getItem(int position) {
        return vocabularylist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return vocabularylist.indexOf(getItem(position));
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        TextView txtword;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.listview_item, parent, false);

        // Locate the TextViews in listview_item.xml
        txtword = (TextView) itemView.findViewById(R.id.word);

        Vocabulary vocabulary = vocabularylist.get(position);
        txtword.setText(vocabulary.getWord());

        return itemView;
    }

    @Override
    public Filter getFilter() {
        if (valueFilter == null) {
            valueFilter = new ValueFilter();
        }
        return valueFilter;
    }

    private class ValueFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();

            if (constraint != null && constraint.length() > 0) {
                ArrayList<Vocabulary> filterList = new ArrayList<Vocabulary>();
                for (int i = 0; i < mStringFilterList.size(); i++) {
                    if ((mStringFilterList.get(i).getWord().toUpperCase())
                            .contains(constraint.toString().toUpperCase())) {

                        Vocabulary vocabulary = new Vocabulary(mStringFilterList.get(i)
                                .getWord(), mStringFilterList.get(i)
                                .getDefinition(), mStringFilterList.get(i)
                                .getExample(), mStringFilterList.get(i)
                                .getFlag());

                        filterList.add(vocabulary);
                    }
                }
                results.count = filterList.size();
                results.values = filterList;
            } else {
                results.count = mStringFilterList.size();
                results.values = mStringFilterList;
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            vocabularylist = (ArrayList<Vocabulary>) results.values;
            notifyDataSetChanged();
        }
    }
}

SingleItemView

public class Vocabulary {
    String word;
    int flag;
    String definition;
    String example;

    Vocabulary(String word, String definition, String example, int flag) {
        this.word = word;
        this.definition = definition;
        this.example = example;
        this.flag = flag;
    }

    public String getDefinition() {
        return definition;
    }

    public void setDefinition(String definition) {
        this.definition = definition;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }



    public String getExample() {
        return example;
    }

    public void setExample(String example) {
        this.example = example;
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

}

1 个答案:

答案 0 :(得分:1)

您对问题的位置是正确的。问题是那些源数组没有被过滤,因此索引与正在过滤的适配器的索引不匹配。 重写onItemClick()如下:

@Override
public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
    Intent i = new Intent(MainActivity.this, SingleItemView.class);

    Vocabulary voc = (Vocabulary) adapter.getItem(position);

    i.putExtra("definition", voc.getDefinition());
    // Pass word
    i.putExtra("word", voc.getWord());
    // Pass example
    i.putExtra("example", voc.getExample());
    // Pass flag
    i.putExtra("flag", voc.getFlag());
    // Pass position
    i.putExtra("position", position);
    // Open SingleItemView.java Activity
    startActivity(i);
}

这只会传递您在其他SingleItemView活动中需要的值。不是整个阵列。

此外,您可能需要考虑的一些改进是使Adapter扩展ArrayAdapter<Vocabulary>而不是BaseAdapter以避免投射。 如果您将整个Vocabulary对象设为Activity而不是传递单个字段,则可以将整个Parcelable对象传递给新的SingleItemView

更新:

以下是需要对public class SingleItemView extends Activity { // Declare Variables TextView txtdefinition; TextView txtword; TextView txtexample; ImageView imgflag; String definition; String word; String example; int flag; int position; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.singleitemview); // Retrieve data from MainActivity on listview item click Intent i = getIntent(); position = i.getIntExtra("position", -1); definition = i.getStringExtra("definition"); word = i.getStringExtra("word"); example = i.getStringExtra("example"); flag = i.getIntExtra("flag", -1); txtdefinition = (TextView) findViewById(R.id.definition); txtword = (TextView) findViewById(R.id.word); txtexample = (TextView) findViewById(R.id.example); imgflag = (ImageView) findViewById(R.id.flag); txtdefinition.setText(definition); txtword.setText(word); txtexample.setText(example); imgflag.setImageResource(flag); } } 进行的更改。您不需要整个数组显示单个项目,因此我更新了额外内容以仅传递所选项目值。

{{1}}