ListView 在筛选后显示错误的项目名称。

huangapple go评论182阅读模式
英文:

ListView showing wrong item name after filtering

问题

I am new in android apps development. 我是新手在安卓应用程序开发方面。
I am learning coding from YouTube. 我正在从YouTube学习编程。
Below is my customListView code containing OnItemClick Listener and Action Bar searcView. 以下是我的自定义ListView代码,包含OnItemClick监听器和Action Bar搜索视图。
Everything is okay but the problem is after filtering. 一切都没问题,但问题是在过滤之后。
I am not getting the right item name on Toast after filtering. 在过滤后,我无法正确获取Toast上的项目名称。
it always takes data from my array source I have written in the ArrayItemlist. 它总是从我在ArrayItemlist中编写的数组源获取数据。
I know there is a similar question here but I sorry not to implement those in the same way. 我知道这里有一个类似的问题,但我抱歉没有以相同的方式实施它们。
I have searched everywhere I can but didn't get the right way I wanted. 我已经搜索了我能找到的每个地方,但没有找到我想要的正确方法。
At last, I have registered at StackOverflow and this is my first post. 最后,我在StackOverflow注册了帐户,这是我的第一篇帖子。
Please help me. 请帮助我。
Maybe the solution is about 3 or 4 lines. 也许解决方案大约是3或4行。
Please instruct me on the coding. 请指导我编码。

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    ArrayList<allCountry> items = new ArrayList<>();  //item are countries
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.listViewId);

        ArrayList<allCountry> detail = new ArrayList<>();  //detail are the short description after item.

        items.add(new allCountry("Bangladesh", "Is My Motherland", R.drawable.bangladesh));
        items.add(new allCountry("India", "The Seven Wonder", R.drawable.india));
        items.add(new allCountry("Pakistan", "The Traitor 71", R.drawable.pakistan));
        items.add(new allCountry("Afghanistan", "The Ghost Cricket Team", R.drawable.afghanistan));
        items.add(new allCountry("Sri Lanka", "The Mountain", R.drawable.srilanka));
        items.add(new allCountry("Bhutan", "No Smoking Zone", R.drawable.bhutan));
        items.add(new allCountry("Nepal", "The Buddhist", R.drawable.nepal));
        items.add(new allCountry("Maldives", "Oh! Island", R.drawable.maldives));
        items.add(new allCountry("Argentina", "The Hero Maradona", R.drawable.argentina));
        items.add(new allCountry("Brazil", "The Hero Pele", R.drawable.brazil));
        items.add(new allCountry("Canada", "The Hero Trudo", R.drawable.canada));
        items.add(new allCountry("Mexico", "The Beautiful City", R.drawable.mexico));
        items.add(new allCountry("Uganda", "To be Described", R.drawable.uganda));
        listView.setAdapter(new customAdapter(MainActivity.this, R.layout.my_list_item, items, detail));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View countryNames, int position, long l) {
                // Here is the problem. it needs update position
                Toast.makeText(MainActivity.this, items.get(position).countryNames, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.my_menu, menu);
        MenuItem menuItem = menu.findItem(R.id.SearchMenuId);
        SearchView searchView = (SearchView) menuItem.getActionView();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                ArrayList<allCountry> result = new ArrayList<>();
                for (allCountry x : items) {
                    if (x.countryNames.toLowerCase().trim().contains(newText) ||
                        x.countryNames.toUpperCase().trim().contains(newText)) {
                        result.add(x);
                    }
                }
                ((customAdapter) listView.getAdapter()).update(result);
                return false;
            }
        });

        return super.onCreateOptionsMenu(menu);
    }
}

///CustomAdapter.java

public class customAdapter extends ArrayAdapter {
    ArrayList<allCountry> items;
    ArrayList<allCountry> detail;
    public customAdapter(Context context, int layout, ArrayList<allCountry> items, ArrayList<allCountry> detail) {
        super(context, layout);
        this.items = items;
        this.detail = detail;
    }

    public void update(ArrayList<allCountry> result) {
        items = new ArrayList<>();
        items.addAll(result);
        notifyDataSetChanged();
    }

    public class ViewHolder {
        TextView textView;
        ImageView imageView;
        TextView textView1;
    }

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

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View row;
        row = convertView;
        ViewHolder viewHolder;
        if (convertView == null) {
            row = LayoutInflater.from(getContext()).inflate(R.layout.my_list_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.imageView = row.findViewById(R.id.imageViewId);
            viewHolder.textView = row.findViewById(R.id.countryNamesTextViewId);
            viewHolder.textView1 = row.findViewById(R.id.showDetailsTextViewId);
            row.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) row.getTag();
        }
        viewHolder.imageView.setImageResource(items.get(position).flags);
        viewHolder.textView.setText(items.get(position).countryNames);
        viewHolder.textView1.setText(items.get(position).showDetails);
        return row;
    }
}

///allcountry.java

public class allCountry {
    String countryNames;
    String showDetails;
    int flags;

    public allCountry(String countryNames, String showDetails, int flags) {
        this.countryNames = countryNames;
        this.flags = flags;
        this.showDetails = showDetails;
    }
}
英文:

I am new in android apps development. I am learning coding from YouTube. Below is my customListView code containing OnItemClick Listener and Action Bar searcView. Everything is okay but the problem is after filtering. I am not getting the right item name on Toast after filtering. it always takes data from my array source I have written in the ArrayItemlist. I know there is a similar question here but I sorry not to implement those in the same way. I have searched everywhere i can but didn't get the right way I wanted. At last, I have registered at StackOverflow and this is my first post. Please help me. Maybe the solution is about 3 or 4 lines. Please instruct me on the coding.

   public class MainActivity extends AppCompatActivity {
private ListView listView;
ArrayList&lt;allCountry&gt; items=new ArrayList&lt;&gt;();  //item are countries
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.listViewId);
ArrayList&lt;allCountry&gt; detail= new ArrayList&lt;&gt;();  //detail are the short description after item.
items.add(new allCountry(&quot;Bangladesh&quot;,&quot;Is My Motherland&quot;,R.drawable.bangladesh));
items.add(new allCountry(&quot;India&quot;,&quot;The Seven Wonder&quot;,R.drawable.india));
items.add(new allCountry(&quot;Pakistan&quot;,&quot;The Traitor 71&quot;,R.drawable.pakistan));
items.add(new allCountry(&quot;Afghanistha&quot;,&quot;The Ghost Cricket Team&quot;,R.drawable.afghanistan));
items.add(new allCountry(&quot;Sri Lanka&quot;,&quot;The Mountain&quot;,R.drawable.srilanka));
items.add(new allCountry(&quot;Bhutan&quot;,&quot;No Smoking Zone&quot;,R.drawable.bhutan));
items.add(new allCountry(&quot;Nepal&quot;,&quot;The Buddhist&quot;,R.drawable.nepal));
items.add(new allCountry(&quot;Maldivs&quot;,&quot;Oh! Island&quot;,R.drawable.maldives));
items.add(new allCountry(&quot;Argentina&quot;,&quot;The Hero Maradona&quot;,R.drawable.argentina));
items.add(new allCountry(&quot;Brazil&quot;,&quot;The Hero Pele&quot;,R.drawable.brazil));
items.add(new allCountry(&quot;Canada&quot;,&quot;The Hero Trudo&quot;,R.drawable.canada));
items.add(new allCountry(&quot;Mexico&quot;,&quot;The Beautiful City&quot;,R.drawable.mexico));
items.add(new allCountry(&quot;Uganda&quot;,&quot;To be Described&quot;,R.drawable.uganda));
listView.setAdapter(new customAdapter(MainActivity.this,R.layout.my_list_item,items,detail));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView&lt;?&gt; adapterView, View countryNames, int position, long l) {
// Here is the problem. it needs update position
Toast.makeText(MainActivity.this,items.get(position).countryNames,Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu,menu);
MenuItem menuItem=menu.findItem(R.id.SearchMenuId);
SearchView searchView= (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {  
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
ArrayList&lt;allCountry&gt; result=new ArrayList&lt;&gt;();
for (allCountry x: items){
if (x.countryNames.toLowerCase().trim().contains(newText)|| 
x.countryNames.toUpperCase().trim().contains(newText)){
result.add(x);
}
}
(  (customAdapter) listView.getAdapter()).update(result);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
}

///CustomAdapter.java


public class customAdapter extends ArrayAdapter {
ArrayList&lt;allCountry&gt;items;
ArrayList&lt;allCountry&gt; detail;
public  customAdapter (Context context, int layout, ArrayList&lt;allCountry&gt; items,ArrayList&lt;allCountry&gt; 
detail){
super(context,layout);
this.items=items;
this.detail=detail;
}
public void update(ArrayList&lt;allCountry&gt; result){
items=new ArrayList&lt;&gt;();
items.addAll(result);
notifyDataSetChanged();
}
public class ViewHolder{
TextView textView;
ImageView imageView;
TextView textView1;
}
@Override
public int getCount() {
return items.size();
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View row;
row=convertView;
ViewHolder viewHolder;
if (convertView==null){
row= LayoutInflater.from(getContext()).inflate(R.layout.my_list_item,parent,false);
viewHolder=new ViewHolder();
viewHolder.imageView=row.findViewById(R.id.imageViewId);
viewHolder.textView=row.findViewById(R.id.countryNamesTextViewId);
viewHolder.textView1=row.findViewById(R.id.showDetailsTextViewId);
row.setTag(viewHolder);
}
else {
viewHolder= (ViewHolder) row.getTag();
}
viewHolder.imageView.setImageResource(items.get(position).flags);
viewHolder.textView.setText(items.get(position).countryNames);
viewHolder.textView1.setText(items.get(position).showDetails);
return row;
}
}

///allcountry.java


public class allCountry {
String countryNames;
String showDetails;
int flags;
public allCountry(String countryNames, String showDetails,int flags){
this.countryNames=countryNames;
this.flags=flags;
this.showDetails=showDetails;
}
}

答案1

得分: 0

在getView方法中的一种方法是将以下代码放入其中:

row.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(view.getContext(), items.get(position).countryNames, Toast.LENGTH_SHORT).show();
    }
});

然后移除OnItemClickListener。

英文:

One way would be to put inside getView:

row.setOnClickListener(.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(),items.get(position).countryNames,Toast.LENGTH_SHORT).show();
}
});

and remove OnItemClickListener

huangapple
  • 本文由 发表于 2020年7月31日 19:19:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63190905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定