当我搜索listview项目然后单击欲望项目时,它会打开列表视图的第一个位置

时间:2015-07-05 14:47:14

标签: java android listview

我有可搜索的国家ListView。当我单击国家列表项而不进行搜索时,它可以正常工作。但是当我在搜索框中键入Brazil并单击Brazil时,请打开Country_Details.Java Activity并传​​递Afganistan详细信息。 当我在搜索框中键入巴西并单击巴西时,我希望在WebView中传递巴西详细信息。
完整源代码https://drive.google.com/open?id=0B46bPR7LKLjpZFRhcV9DdmIweTQ
这是我的代码。

 package com.nasir.search;

    import java.util.ArrayList;
    import java.util.Arrays;

    import android.app.Activity;
    import android.app.ListActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.BaseAdapter;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.TextView;


    public class Search_Country extends ListActivity {

        private EditText SearchText;
        private ListView ListText;

        private String[] Number_List = { "Afghanistan", "Albania", "Algeria", "Brazil"};

        private ArrayList<String> array_sort;
        int textlength = 0;

        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.search_country);

            SearchText = (EditText) findViewById(R.id.listview_search);
            ListText = (ListView) findViewById(android.R.id.list);

            array_sort = new ArrayList<String>(Arrays.asList(Number_List));
            setListAdapter(new bsAdapter(this));

            SearchText.addTextChangedListener(new TextWatcher()
            {
                public void afterTextChanged(Editable s) 
                {

                }

                public void beforeTextChanged(CharSequence s, int start, int count, int after)
                {

                }

                public void onTextChanged(CharSequence s, int start, int before, int count)
                {
                    textlength = SearchText.getText().length();
                    array_sort.clear();
                    for (int i = 0; i < Number_List.length; i++) 
                    {
                        if (textlength <= Number_List[i].length()) 
                        {                       
                            if(Number_List[i].toUpperCase().contains(SearchText.getText().toString().toUpperCase().trim()))
                            {
                                array_sort.add(Number_List[i]);
                            }
                        }
                    }
                    AppendList(array_sort);
                }
            });

            ListText.setOnItemClickListener(new OnItemClickListener() 
            {
                public void onItemClick(AdapterView<?> parent, View view,
                     int position, long id) {
                        switch( position )
                        {
                           case 0:  Intent intent = new Intent(Search_Country.this, Country_Details.class);
                                    intent.putExtra("header", getString(R.string.html_afganistan));
                                    startActivity(intent);
                                    break;
                           case 1:  Intent intent1 = new Intent(Search_Country.this, Country_Details.class);
                                    intent1.putExtra("header", getString(R.string.html_albenia));
                                    startActivity(intent1);
                                    break;
                           case 2:  Intent intent2 = new Intent(Search_Country.this, Country_Details.class);
                                    intent2.putExtra("header", getString(R.string.html_algeria));
                                    startActivity(intent2);
                                    break;

                           case 3:  Intent intent3 = new Intent(Search_Country.this, Country_Details.class);
                                    intent3.putExtra("header", getString(R.string.html_brazil));
                                    startActivity(intent3);
                                    break;

                        }
                    }
            });
        }

        public void AppendList(ArrayList<String> str) 
        {
            setListAdapter(new bsAdapter(this));
        }

        public class bsAdapter extends BaseAdapter 
        {
            Activity cntx;

            public bsAdapter(Activity context) 
            {
                this.cntx = context;
            }

            public int getCount()
            {
                return array_sort.size();
            }

            public Object getItem(int position) 
            {
                return array_sort.get(position);
            }

            public long getItemId(int position)
            {
                return array_sort.size();
            }

            public View getView(final int position, View convertView, ViewGroup parent) 
            {
                View row = null;
                LayoutInflater inflater = cntx.getLayoutInflater();
                row = inflater.inflate(R.layout.search_country_listview, null);
                TextView tv = (TextView) row.findViewById(R.id.listview_seacrh_text);
                tv.setText(array_sort.get(position));
                return row;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

问题在于此处,您在静态位置调用webview,而是相应地调用您的数组列表 如下所示。

    if( array_list.get(position).equals("Afghanistan")){
    Intent intent = new Intent(Search_Country.this, Country_Details.class);                            intent.putExtra("header", getString(R.string.html_afganistan));                                       startActivity(intent);
}