如何在过滤setonitemclicklistener后更改android listview位置 [英] How to change android listview position after filtering for setonitemclicklistener

查看:63
本文介绍了如何在过滤setonitemclicklistener后更改android listview位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几天,我正在努力完成一项简单的任务。我想过滤一个列表视图,当我点击过滤后的列表项时,OnItemClickListener从所需的列表项中收集 postID 并将该值放到下一个活动中。



但是问题是,onItemClickListener无法从过滤后的位置列表中获取 postID ,看来它点击了已过滤的列表项但得到 postID 旧列表项的位置。



例如,在初始位置说我的列表视图如下所示:



A

AB

BA

C

CB




在这个位置,如果我选择任何列表项,它将获得正确项目的 postID



现在当我使用编辑文本过滤ListView时出现问题。说,我在编辑文本中输入了C.然后ListView更改如下:



C

CB




在这个位置,如果我点击 C ,它将获得 A postID ,如果我点击< b> CB 它将按照初始列表位置获得 AB postID



我尝试过的事情:



请查看我的片段代码生成列表:

For a couple of days, I am trying to achieve a simple task. I want to filter a listview and when I click on the filtered list item, the OnItemClickListener collect the postID from the desired list item and put that value to next activity.

But the problem is, the onItemClickListener is not able to get postID from the filtered position list, it appears that, it clicking the filtered list item but getting postID of old list item's position.

For Example, Say my listview at initial position looks like this:

A
AB
BA
C
CB


In this position, if I select any list item, it will get the postID of the correct item.

Now the problem arises when I filter the ListView with an edit text. Say, I typed C in my edit text. Then the ListView changes as follows:

C
CB


In this position, if I click on C, it will get the postID of A and if I click on CB it will get the postID of AB as per the initial list position.

What I have tried:

Please look at my fragment's code where the list is being generated:

public void filterTextmethod() {

        editText = (EditText) rootView.findViewById(R.id.inputsearchenglishhome);

        initlist();

        editText.addTextChangedListener(new TextWatcher() {


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


            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


                if (adapter != null) {
                    System.out.println("Text[" + s + "]");
                    adapter.getFilter().filter(s.toString());
                    adapter.notifyDataSetChanged();
                }


            }

            @Override
            public void afterTextChanged(Editable s) {


            }

        });

    }


    private void initlist() {


            progressBar.setVisibility(View.VISIBLE);
            StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {

                gson = new Gson();
                list = (List) gson.fromJson(s, List.class);
                postTitle = new String[list.size()];

                for (int i = 0; i < list.size(); ++i) {
                    mapPost = (Map<String, Object>) list.get(i);
                    mapTitle = (Map<String, Object>) mapPost.get("title");
                    postTitle[i] = (String) mapTitle.get("rendered");
                 }


                adapter = new ArrayAdapter<>(Objects.requireNonNull(getActivity()), android.R.layout.simple_list_item_1, postTitle);                
                postList.setAdapter(adapter);
                progressBar.setVisibility(View.GONE);                

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                progressBar.setVisibility(View.GONE);
                if (mToast != null) {
                    mToast.cancel();
                }
                mToast = Toast.makeText(getActivity(), "Error! Check your Internet Connection.", Toast.LENGTH_LONG);
                mToast.show();
            }
        });
                request.setRetryPolicy(new DefaultRetryPolicy(5000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                RequestQueue rQueue = Volley.newRequestQueue(Objects.requireNonNull(getActivity()).getApplicationContext());
                rQueue.getCache().invalidate(url, true);
                rQueue.add(request);

                postList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        mapPost = (Map<String, Object>) list.get(position);
                        postID = ((Double) mapPost.get("id")).intValue();

                        Intent intent = new Intent(getActivity(), FullPostView.class);
                        intent.putExtra("id", "" + postID);
                        //intent.putExtra("colorcode", "#FF5252");
                        //intent.putExtra("parentFragmentSubject", "English");
                        startActivity(intent);

                    }


                });

    }





现在看看XML:





Now look at the XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".English.EnglishHomeScreen"

    android:gravity="center"

    android:id="@+id/searchlayoutenglishhome">

    <ProgressBar

        android:id="@+id/progressBar"

        style="@style/RedAccent"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_alignParentStart="true"

        android:visibility="invisible"

        android:layout_marginTop="150dp"

        android:layout_centerHorizontal="true"

        android:layout_below="@+id/inputsearchenglishhome"/>

    <EditText

        android:id="@+id/inputsearchenglishhome"

        android:layout_width="match_parent"

        android:layout_height="35dp"

        android:layout_marginLeft="15dp"

        android:layout_marginRight="15dp"

        android:layout_marginTop="10dp"

        android:drawableStart="@drawable/ic_search_black_24dp"

        android:drawableTint="@color/search_black"

        android:paddingStart="8dp"

        android:paddingEnd="8dp"

        android:drawablePadding="8dp"

        android:background="@drawable/edittext_round_red"

        android:hint="Search in all English Questions"

        android:layout_gravity="bottom"

        android:inputType="textVisiblePassword"/>


    <ListView

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_marginTop="10dp"

        android:layout_marginLeft="15dp"

        android:layout_marginRight="15dp"

        android:layout_marginBottom="10dp"

        android:paddingBottom="10dp"

        android:id="@+id/postList"

        android:layout_below="@+id/inputsearchenglishhome"

        android:descendantFocusability="blocksDescendants"

        android:clickable="false"

        android:focusable="false"/>


</RelativeLayout>





请帮我找到解决方案。我将非常感谢你的帮助。我无法在2-3天内弄清楚这个问题。请帮帮我。谢谢。



Please help me to find the solution. i will really appreciate your help. I cannot figure out the problem from 2-3 days. Help me please. Thank you.

推荐答案

onItemClick中的位置是适配器的索引,例如

The position in onItemClick is an index of the adapter, e.g.
adapter.getItemAtPosition(position)



但是你将它用作 mapPost 的索引。看起来你正在过滤适配器,但不是 mapPost


However you use it as an index for mapPost. It looks like you are filtering the adapter, but not mapPost.


这篇关于如何在过滤setonitemclicklistener后更改android listview位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆