几个位置后的自定义gridview显示不正确的数据

Posted

技术标签:

【中文标题】几个位置后的自定义gridview显示不正确的数据【英文标题】:Custom gridview after few positions shows incorrect data 【发布时间】:2020-07-16 23:53:52 【问题描述】:

我发现我的自定义 gridview 适配器有问题,我正在添加从 1 到 10 的位置。 在 6 号之前一切都是正确的,之后 显示的是 2 号和 1 号,而不是显示 7 / 8 号。 我不知道怎么了。

我是新手,我将不胜感激。谢谢

Here is a picture of a problem.

这是来自 gridview 适配器的代码

public class GvOrdersAdapter extends BaseAdapter

    private Context context;
    private final String[] nameValues;
    private final String[] danieValues;
    private final String[] danie2Values;
    private final String[] zupaValues;
    private final String[] statusValues;




    GvOrdersAdapter(Context context, String[] nameValues, String[] danieValues, String[] danie2Values,
 String[] zupaValues, String[] statusValues)
    
        this.context = context;
        this.nameValues = nameValues;
        this.danieValues = danieValues;
        this.danie2Values = danie2Values;
        this.zupaValues = zupaValues;
        this.statusValues = statusValues;

    

    @SuppressLint("ResourceAsColor", "SetTextI18n", "InflateParams")
    public View getView(int position, View convertView, ViewGroup parent)
    


        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null)
        

            // get layout from mobile.xml
            assert inflater != null;
            gridView = inflater.inflate(R.layout.gv_ordered_list, null);



            // set value into textview
            TextView tvName = gridView.findViewById(R.id.tvName);
            TextView tvDanie = gridView.findViewById(R.id.tvDanieGlowne);
            TextView tvDanie2 = gridView.findViewById(R.id.tvDodatki);
            TextView tvZupa = gridView.findViewById(R.id.tvZupa);
            TextView tvStatus = gridView.findViewById(R.id.tvStatus);

            tvName.setText(nameValues[position]);
            tvDanie.setText((danieValues[position]));
            tvDanie2.setText((danie2Values[position]));
            tvZupa.setText((zupaValues[position]));


            if (statusValues[position].contains("Poczekaj na akceptacje zamówienia"))
            
                tvStatus.setText((statusValues[position]) + "\n");
            

            else if (statusValues[position].contains("Przyjęto, w trakcie realizacji"))
            
                tvStatus.setText((statusValues[position])+"\n");
                tvStatus.setTextColor(context.getResources().getColor(R.color.ordered_wait_for_accept));
            

            else if (statusValues[position].contains("W transporcie"))
            
                tvStatus.setText((statusValues[position])+"\n");
                tvStatus.setTextColor(context.getResources().getColor(R.color.ordered_accepted));
            

            else if (statusValues[position].contains("Dostarczone"))
            
                tvStatus.setText((statusValues[position])+"\n");
                tvStatus.setTextColor(context.getResources().getColor(R.color.colorPrimary));
            



         else
        
            gridView = convertView;
        


        return gridView;
    


    @Override
    public int getCount()
    
        return nameValues.length;
    

    @Override
    public Object getItem(int position)
    
        return null;
    

    public boolean isEnabled(int position)
    
        return true;
    

    @Override
    public long getItemId(int position) 
        return position;
    


从活动中收集来自 firebase 的数据

private void ZlozoneZamowieniaList() 

        DatabaseReference ordered = FirebaseDatabase.getInstance().getReference().child("Złożone zamówienia");
        ordered.child(thisYear)
                .child(thisMonth)
                .child(thisDay)
                .child(Firma)
                .orderByChild("name")
                .equalTo(id)
                .addValueEventListener(new ValueEventListener() 


                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot)    
                    
                        ClearLists(); //WYCZYŚĆ LISTY
                        for (DataSnapshot snapshot : dataSnapshot.getChildren())  
                        
                            String imie = snapshot.child("imie").getValue(String.class); 
                            String danie = snapshot.child("danie").getValue(String.class);//ITD
                            String dodatek = snapshot.child("dodatek").getValue(String.class);//ITD
                            String zupa = snapshot.child("zupa").getValue(String.class);//ITD
                            String id = snapshot.child("informacjeDlaObslugi").getValue(String.class);//ITD
                            String gotKey = snapshot.child("key").getValue(String.class);//ITD
                            String status = snapshot.child("status").getValue(String.class);//ITD
                            if (status != null && !status.contains("Zamówienie anulowane przez klienta"))           
                            
                                mListName.add(imie);        
                                if (danie != null)
                                
                                    mListDish.add(danie);       
                                else
                                
                                    mListDish.add(" ");
                                

                                if (dodatek != null)                
                                
                                    mListDish2.add(dodatek);       
                                 else 
                                    mListDish2.add(" ");            
                                
                                if (zupa != null)                   
                                
                                    mListSoup.add(zupa);
                                 else 
                                    mListSoup.add(" ");
                                
                                if (id != null)                   
                                
                                    mListInfo.add(id);
                                 else 
                                    mListInfo.add(" ");
                                
                                mListStatus.add(status);
                                keyList.add(gotKey);
                            
                            String[] sListName = mListName.toArray(new String[0]);          
                            String[] sListDish = mListDish.toArray(new String[0]);         
                            String[] sListDish2 = mListDish2.toArray(new String[0]);       
                            String[] sListSoup = mListSoup.toArray(new String[0]);         
                            String[] sListStatus = mListStatus.toArray(new String[0]);      
                            GvOrdersAdapter GvAdapter = new GvOrdersAdapter(getContext(), sListName, sListDish,
 sListDish2, sListSoup, sListStatus);        
                            gvOrderedMeals.setAdapter(GvAdapter);
                            GvClear();                                  
                        
                    

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError)
                    

                    
                );
    

数据已正确收集,因为当我在单击 gridview 后创建 alertdaliog 时,它会显示正确的数据。

【问题讨论】:

@MikeM 你能解释一下吗?请显示一些示例或文档? 欢迎来到 Stack Overflow。您应该始终接受任何可以在 Stack Overflow 中解决您的问题的答案 【参考方案1】:

您的问题是 如果 convertView 不为空,则您从未更新其文本视图,但在以下代码中它将始终更新

像这样更改getView()

public View getView(int position, View convertView, ViewGroup parent)


   LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//<----------------------- add following line
   View gridView = convertView == null ? inflater.inflate(R.layout.gv_ordered_list, null) : convertView 


//<----------------------- "if else" is removed from your code

            // get layout from mobile.xml
            assert inflater != null;
            gridView = inflater.inflate(R.layout.gv_ordered_list, null);



            // set value into textview
            TextView tvName = gridView.findViewById(R.id.tvName);
            TextView tvDanie = gridView.findViewById(R.id.tvDanieGlowne);
            TextView tvDanie2 = gridView.findViewById(R.id.tvDodatki);
            TextView tvZupa = gridView.findViewById(R.id.tvZupa);
            TextView tvStatus = gridView.findViewById(R.id.tvStatus);

            tvName.setText(nameValues[position]);
            tvDanie.setText((danieValues[position]));
            tvDanie2.setText((danie2Values[position]));
            tvZupa.setText((zupaValues[position]));


            if (statusValues[position].contains("Poczekaj na akceptacje zamówienia"))
            
                tvStatus.setText((statusValues[position]) + "\n");
            

            else if (statusValues[position].contains("Przyjęto, w trakcie realizacji"))
            
                tvStatus.setText((statusValues[position])+"\n");
                tvStatus.setTextColor(context.getResources().getColor(R.color.ordered_wait_for_accept));
            

            else if (statusValues[position].contains("W transporcie"))
            
                tvStatus.setText((statusValues[position])+"\n");
                tvStatus.setTextColor(context.getResources().getColor(R.color.ordered_accepted));
            

            else if (statusValues[position].contains("Dostarczone"))
            
                tvStatus.setText((statusValues[position])+"\n");
                tvStatus.setTextColor(context.getResources().getColor(R.color.colorPrimary));
            


        return gridView;
    

【讨论】:

非常感谢您的回答。它有效,但我有一个问题 View gridView = [convertView == null ? inflater.inflate(R.layout.gv_ordered_list, null) : convertView] ;方括号中的所有内容都显示为“冗余” 为什么会这样? @Respin 是的,不需要方括号 所以我应该只写“View gridView;”,仅此而已?无论如何,非常感谢您的帮助:) @Respin View gridView = convertView == null ? inflater.inflate(R.layout.gv_ordered_list, null) : convertView

以上是关于几个位置后的自定义gridview显示不正确的数据的主要内容,如果未能解决你的问题,请参考以下文章

地图注释显示不正确的自定义图钉图像

GraphQL - 突变响应后的自定义错误消息

解决ScrollView下嵌套ListView/GridView进页面不在顶部的问题以及数据显示不全的问题( 只显示一行)

一个可以拖动的自定义Gridview代码

将 Gridview 导出到 Excel:无法导出显示的正确数据

颤动:GridView 未正确显示