Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在筛选listview后通过单击listview项目打开原始活动?_Java_Android_Listview_Intentfilter - Fatal编程技术网

Java 如何在筛选listview后通过单击listview项目打开原始活动?

Java 如何在筛选listview后通过单击listview项目打开原始活动?,java,android,listview,intentfilter,Java,Android,Listview,Intentfilter,我的代码在单击项目时运行良好。但当我尝试在过滤listview之后打开活动时,问题就出现了。它总是打开活动1 这是我的源代码 navigate.java public class navigate extends Activity { // Declare Variables ListView list; ListViewAdapter adapter; EditText editsearch; String[] rank; String

我的代码在单击项目时运行良好。但当我尝试在过滤listview之后打开活动时,问题就出现了。它总是打开活动1

这是我的源代码

navigate.java

    public class navigate extends Activity {

    // Declare Variables
    ListView list;
    ListViewAdapter adapter;
    EditText editsearch;
    String[] rank;
    String[] names;

    int[] flag;
    ArrayList<Object> arraylist = new ArrayList<Object>();

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

        // Generate sample data
        rank = new String[] { "1", "2", "3", "4", "5","6","7","8","9","10","11"};

        names = new String[] { "Animal Bite", "Asthma Attack", "Choking","CPR","Black eye",
                "Drowning", "Fracture","Heart Attack","Insect Bite","Poisoning","Spinal Injury",};


        flag = new int[] { R.drawable.animal,
                R.drawable.asthma, R.drawable.choke, R.drawable.cpricon,
                R.drawable.blckeye,R.drawable.drown,R.drawable.fracture,R.drawable.hrtattck,R.drawable.insect,R.drawable.poison,R.drawable.spinal };

        // Locate the ListView in listview_main.xml
        list = (ListView) findViewById(R.id.listview);

        for (int i = 0; i < rank.length; i++) 
        {
            Object wp = new Object(rank[i], names[i], flag[i]);
            // Binds all strings into an array
            arraylist.add(wp);
        }

        // Pass results to ListViewAdapter Class
        adapter = new ListViewAdapter(this, arraylist);

        // Binds the Adapter to the ListView
        list.setAdapter(adapter);

        // Locate the EditText in listview_main.xml
        editsearch = (EditText) findViewById(R.id.search);

        // Capture Text in EditText
        editsearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
                adapter.filter(text);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub
            }
        });
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(final AdapterView<?> listView, View view,
                                    final int position, long id) {
                switch ((int) adapter.getItemId(position)) {
                    case 0:
                        Intent newActivity = new    Intent(navigate.this, animalbite.class);
                        startActivity(newActivity);
                        break;
                    case 1:Intent newActivity1 = new    Intent(navigate.this, asthmaattack.class);
                        startActivity(newActivity1);
                        break;
                    case 2:Intent newActivity2 = new    Intent(navigate.this, animalbite.class);
                        startActivity(newActivity2);
                        break;}

            }
        });
    }


}
如何在筛选listview项目后,在单击这些项目时打开不同的活动

这是我使用的示例代码。虽然它不提供通过意图打开原始活动的功能,但只是一些伪造的活动,所以我正在尝试改变这一点。

我认为您的适配器有问题,我的应用程序下面的示例代码是从
可过滤的扩展而来的,可以正常工作

public class AdapterContacts extends BaseAdapter implements Filterable {

    private LayoutInflater inflater;
    private Context context;
    private List<ContactLists> categoryArrayList;
    private final ArrayList<ContactLists> originalList = new ArrayList<ContactLists>();
    private NameFilter filter;

    public AdapterContacts(ArrayList<ContactLists> array) {
        categoryArrayList = array;
    }

    public AdapterContacts(Context context, List<ContactLists> array) {
        this.context = context;
        inflater = LayoutInflater.from(this.context);
        categoryArrayList = array;
        originalList.addAll(array);
    }

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

    @Override
    public ContactLists getItem(int position) {
        return categoryArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mViewHolder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.layout_contacts_list_item, null);
            mViewHolder = new ViewHolder(convertView);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }

        ContactLists item = getItem(position);
        mViewHolder.fillItems(this, item, position);

        return convertView;
    }

    private static class UI extends HelperUI {
        public TextView tv_person_nickname_mobile_number;
        public TextView btn_invite_message;
        public ImageView img_contact_image;
        public ImageView imgv_user_rank;
        public TextView tv_contact_name;
        public LinearLayout ll_root;

        public UI(View view) {
            parseUi(view);
        }
    }

    private class ViewHolder {
        private UI UI;

        public ViewHolder(View view) {
            UI = new UI(view);
        }

        public void fillItems(final AdapterContacts adapter, final ContactLists item, final int position) {
            UI.tv_contact_name.setText(item.getContact_name());

            if (item.getStatus() == 1) {
                UI.btn_invite_message.setVisibility(View.GONE);
                UI.imgv_user_rank.setVisibility(View.VISIBLE);

                if (item.getRank() != null || !TextUtils.isEmpty(item.getRank())) {
                    //Picasso.with(G.context).load(item.getRank()).into(UI.imgv_user_rank);
                }

                UI.tv_person_nickname_mobile_number.setText(item.getNick_name());
                //UI.ll_root.setBackgroundDrawable(G.context.getResources().getDrawable(R.drawable.selector_button_actions));
                if (item.getContact_image() == null || TextUtils.isEmpty(item.getContact_image())) {
                    Bitmap bitmap = UC.getContactPhoto(item.getMobile_number(), G.context.getContentResolver());
                    if (bitmap != null) {
                        UI.img_contact_image.setImageBitmap(bitmap);
                    } else {
                        UI.img_contact_image.setImageDrawable(G.context.getResources().getDrawable(R.drawable.no_avatar));
                    }
                } else {
                    // show user avatar from web
                    //Picasso.with(G.context).load(item.getContact_image()).into(UI.img_contact_image);
                    UI.img_contact_image.setImageBitmap(BitmapFactory.decodeFile(G.DIR_IMAGE + "/" + item.getContact_image()));
                }
            } else {
                // UI.ll_root.setBackgroundDrawable(G.context.getResources().getDrawable(R.drawable.selector_invite_actions));
                UI.btn_invite_message.setVisibility(View.VISIBLE);
                UI.imgv_user_rank.setVisibility(View.GONE);
                UI.btn_invite_message.setText(UC.getString(R.string.invite_person));
                UI.btn_invite_message.setBackgroundDrawable(G.context.getResources().getDrawable(R.drawable.shape_invite_button_default));
                UI.tv_person_nickname_mobile_number.setText(item.getMobile_number());
                Bitmap bitmap = UC.getContactPhoto(item.getMobile_number(), G.context.getContentResolver());
                if (bitmap != null) {
                    UI.img_contact_image.setImageBitmap(bitmap);
                } else {
                    UI.img_contact_image.setImageDrawable(G.context.getResources().getDrawable(R.drawable.no_avatar));
                }
            }
        }
    }

    @Override
    public Filter getFilter() {
        if (filter == null) {
            filter = new NameFilter();
        }
        return filter;
    }

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

            String searchText = constraint.toString().toLowerCase();
            ArrayList<ContactLists> newList = filterListBasedOnSearchText(searchText);
            results.values = newList;
            results.count = newList.size();

            return results;
        }

        private ArrayList<ContactLists> filterListBasedOnSearchText(String constraint) {
            ArrayList<ContactLists> newList = new ArrayList<ContactLists>();

            int l = originalList.size();
            for (int i = 0; i < l; i++) {
                ContactLists nameList = originalList.get(i);

                if (nameList.getContact_name().toString().contains(constraint)) {
                    newList.add(nameList);
                }
            }

            return newList;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            categoryArrayList = (ArrayList<ContactLists>) results.values;
            notifyDataSetChanged();
        }
    }
}
公共类AdapterContacts扩展BaseAdapter实现可过滤{
私人充气机;
私人语境;
私有列表分类列表;
私有最终ArrayList originalList=新ArrayList();
私有名称过滤器;
公共适配器触点(ArrayList数组){
CategoryAryList=数组;
}
公共AdapterContacts(上下文、列表数组){
this.context=上下文;
充气器=从(此上下文)开始的充气器;
CategoryAryList=数组;
原始列表addAll(数组);
}
@凌驾
public int getCount(){
返回CategoryAryList.size();
}
@凌驾
公共联系人列表getItem(内部位置){
返回categoryArrayList.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视窗支架;
if(convertView==null){
convertView=充气机。充气(R.layout.layout\u contacts\u list\u item,空);
mViewHolder=新的视图保持架(convertView);
setTag(mViewHolder);
}否则{
mViewHolder=(ViewHolder)convertView.getTag();
}
联系人列表项=获取项(位置);
mViewHolder.fillItems(此、项、位置);
返回视图;
}
私有静态类UI扩展了HelperUI{
公共文本视图电视\个人\昵称\手机\号码;
公共文本查看btn_邀请_消息;
公共图像查看img_联系人图像;
公共图像查看imgv_用户_排名;
公共文本查看电视联系人姓名;
公共线路布置图(L_根);
公共用户界面(视图){
parseUi(视图);
}
}
私有类视窗持有者{
私有用户界面;
公共视图持有者(视图){
UI=新UI(视图);
}
公共无效填充项(最终适配器接触适配器、最终联系人列表项、最终int位置){
UI.tv_contact_name.setText(item.getContact_name());
if(item.getStatus()==1){
UI.btn_invite_message.setVisibility(View.GONE);
UI.imgv_user_rank.setVisibility(View.VISIBLE);
if(item.getRank()!=null | |!TextUtils.isEmpty(item.getRank())){
//Picasso.with(G.context).load(item.getRank())到(UI.imgv\u user\u rank)中;
}
UI.tv_person_昵称_mobile_number.setText(item.getNick_name());
//UI.ll_root.setBackgroundDrawable(G.context.getResources().getDrawable(R.drawable.selector_按钮_操作));
if(item.getContact_image()==null || TextUtils.isEmpty(item.getContact_image()){
位图位图=UC.getContactPhoto(item.getMobile_number(),G.context.getContentResolver());
if(位图!=null){
UI.img_contact_image.setImageBitmap(位图);
}否则{
UI.img_contact_image.setImageDrawable(G.context.getResources().getDrawable(R.drawable.no_avatar));
}
}否则{
//从web显示用户头像
//Picasso.with(G.context).load(item.getContact\u image())到(UI.img\u contact\u image)中;
UI.img_contact_image.setImageBitmap(BitmapFactory.decodeFile(G.DIR_image+“/”+item.getContact_image());
}
}否则{
//UI.ll_root.setBackgroundDrawable(G.context.getResources().getDrawable(R.drawable.selector_invite_actions));
UI.btn_invite_message.setVisibility(View.VISIBLE);
UI.imgv_user_rank.setVisibility(View.GONE);
UI.btn_invite_message.setText(UC.getString(R.string.invite_person));
UI.btn_invite_message.setBackgroundDrawable(G.context.getResources().getDrawable(R.drawable.shape_invite_button_default));
UI.tv_person_昵称_mobile_number.setText(item.getMobile_number());
位图位图=UC.getContactPhoto(item.getMobile_number(),G.context.getContentResolver());
if(位图!=null){
UI.img_contact_image.setImageBitmap(位图);
}否则{
UI.img_contact_image.setImageDrawable(G.context.getResources().getDrawable(R.drawable.no_avatar));
}
}
}
}
@凌驾
公共过滤器getFilter(){
if(filter==null){
filter=新名称filter();
}
回流过滤器;
}
公共类NameFilter扩展了过滤器{
@凌驾
受保护的筛选器结果性能筛选(CharSequence约束){
FilterResults results=新的FilterResults();
字符串searchText=constraint.toString().toLowerCase();
ArrayList newList=filterListBasedOnSearchText(searchText);
results.values=newList;
results.count=newList.size();
返回结果;
}
私有ArrayList筛选器ListBasedOnSearchText(字符串约束){
ArrayList newList=新的ArrayList();
int l=orig
    public class Object {
    private String rank;
    private String country;
    private int flag;

    public Object(String rank, String country,
                  int flag) {
        this.rank = rank;
        this.country = country;

        this.flag = flag;
    }



    public String getCountry() {
        return this.country;
    }



    public int getFlag() {
        return this.flag;
    }
}
public class AdapterContacts extends BaseAdapter implements Filterable {

    private LayoutInflater inflater;
    private Context context;
    private List<ContactLists> categoryArrayList;
    private final ArrayList<ContactLists> originalList = new ArrayList<ContactLists>();
    private NameFilter filter;

    public AdapterContacts(ArrayList<ContactLists> array) {
        categoryArrayList = array;
    }

    public AdapterContacts(Context context, List<ContactLists> array) {
        this.context = context;
        inflater = LayoutInflater.from(this.context);
        categoryArrayList = array;
        originalList.addAll(array);
    }

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

    @Override
    public ContactLists getItem(int position) {
        return categoryArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mViewHolder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.layout_contacts_list_item, null);
            mViewHolder = new ViewHolder(convertView);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }

        ContactLists item = getItem(position);
        mViewHolder.fillItems(this, item, position);

        return convertView;
    }

    private static class UI extends HelperUI {
        public TextView tv_person_nickname_mobile_number;
        public TextView btn_invite_message;
        public ImageView img_contact_image;
        public ImageView imgv_user_rank;
        public TextView tv_contact_name;
        public LinearLayout ll_root;

        public UI(View view) {
            parseUi(view);
        }
    }

    private class ViewHolder {
        private UI UI;

        public ViewHolder(View view) {
            UI = new UI(view);
        }

        public void fillItems(final AdapterContacts adapter, final ContactLists item, final int position) {
            UI.tv_contact_name.setText(item.getContact_name());

            if (item.getStatus() == 1) {
                UI.btn_invite_message.setVisibility(View.GONE);
                UI.imgv_user_rank.setVisibility(View.VISIBLE);

                if (item.getRank() != null || !TextUtils.isEmpty(item.getRank())) {
                    //Picasso.with(G.context).load(item.getRank()).into(UI.imgv_user_rank);
                }

                UI.tv_person_nickname_mobile_number.setText(item.getNick_name());
                //UI.ll_root.setBackgroundDrawable(G.context.getResources().getDrawable(R.drawable.selector_button_actions));
                if (item.getContact_image() == null || TextUtils.isEmpty(item.getContact_image())) {
                    Bitmap bitmap = UC.getContactPhoto(item.getMobile_number(), G.context.getContentResolver());
                    if (bitmap != null) {
                        UI.img_contact_image.setImageBitmap(bitmap);
                    } else {
                        UI.img_contact_image.setImageDrawable(G.context.getResources().getDrawable(R.drawable.no_avatar));
                    }
                } else {
                    // show user avatar from web
                    //Picasso.with(G.context).load(item.getContact_image()).into(UI.img_contact_image);
                    UI.img_contact_image.setImageBitmap(BitmapFactory.decodeFile(G.DIR_IMAGE + "/" + item.getContact_image()));
                }
            } else {
                // UI.ll_root.setBackgroundDrawable(G.context.getResources().getDrawable(R.drawable.selector_invite_actions));
                UI.btn_invite_message.setVisibility(View.VISIBLE);
                UI.imgv_user_rank.setVisibility(View.GONE);
                UI.btn_invite_message.setText(UC.getString(R.string.invite_person));
                UI.btn_invite_message.setBackgroundDrawable(G.context.getResources().getDrawable(R.drawable.shape_invite_button_default));
                UI.tv_person_nickname_mobile_number.setText(item.getMobile_number());
                Bitmap bitmap = UC.getContactPhoto(item.getMobile_number(), G.context.getContentResolver());
                if (bitmap != null) {
                    UI.img_contact_image.setImageBitmap(bitmap);
                } else {
                    UI.img_contact_image.setImageDrawable(G.context.getResources().getDrawable(R.drawable.no_avatar));
                }
            }
        }
    }

    @Override
    public Filter getFilter() {
        if (filter == null) {
            filter = new NameFilter();
        }
        return filter;
    }

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

            String searchText = constraint.toString().toLowerCase();
            ArrayList<ContactLists> newList = filterListBasedOnSearchText(searchText);
            results.values = newList;
            results.count = newList.size();

            return results;
        }

        private ArrayList<ContactLists> filterListBasedOnSearchText(String constraint) {
            ArrayList<ContactLists> newList = new ArrayList<ContactLists>();

            int l = originalList.size();
            for (int i = 0; i < l; i++) {
                ContactLists nameList = originalList.get(i);

                if (nameList.getContact_name().toString().contains(constraint)) {
                    newList.add(nameList);
                }
            }

            return newList;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            categoryArrayList = (ArrayList<ContactLists>) results.values;
            notifyDataSetChanged();
        }
    }
}