TextView in listview rows showing repeated values on scroll in Android?

后端 未结 3 1319
小鲜肉
小鲜肉 2021-01-12 11:50

I am working with custom Adapter of a ListView in which I have a TextView and a Spinner. After selecting the values from a Spinner, the value after copied to the TextView of

3条回答
  •  抹茶落季
    2021-01-12 12:08

    I got the solution for the issue. I have introduced a dialogList() in which I am working with a ArrayList. Below I have mentioned the code of my Adapter class.

    public class AppListAdapter extends BaseAdapter {
    
        private LayoutInflater mInflater;
        private List mApps = Constants.list;
        private Context _activity;
        ArrayList months=null;
        ArrayAdapter dataAdapter =null;
        int spinerposition;
        Context contextfordatabase=null;
    
        int temp=0;
        private int screenWidth;
    
    
        /**
         * Constructor.
         * 
         * @param context the application context which is needed for the layout inflater
         * @param screenWidth 
         */
        public AppListAdapter(Context context, int screenWidth) {
            contextfordatabase=context;
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);
            this._activity=context;
            this.screenWidth = screenWidth;
    
            months = new ArrayList();
            months.add("No Item Selected");
            months.add("None");
            months.add("Entertainment");
            months.add("Games");
            months.add("News/Books");
            months.add("Social Networking");
            months.add("Utilities");
            months.add("Texting");
            months.add("Web Browsers");
        }
    
        public int getCount() {
    
            return mApps.size();
        }
    
        public Object getItem(int position) {
            return mApps.get(position);
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        public class AppViewHolder {
    
            private TextView mTitle = null;
            private TextView apptitleTxt = null;
            private TextView offTxt = null;
            private Spinner spiner=null;
            public TextView offtext;
        }
    
        public View getView(final int position, View convertView, ViewGroup parent) {
    
            final AppViewHolder holder;
            if(convertView == null) {
                convertView = mInflater.inflate(R.layout.row, null);
                // creates a ViewHolder and stores a reference to the children view we want to bind data to
                holder = new AppViewHolder();
    
                holder.spiner=(Spinner)convertView.findViewById(R.id.spinner);
                holder.offtext=(TextView)convertView.findViewById(R.id.off_txt);
    
                holder.apptitleTxt = (TextView) convertView.findViewById(R.id.apptitle_txt);
                Typeface typeface = Typeface.createFromAsset(_activity.getAssets(),"CHICM___.TTF");
                holder.apptitleTxt.setTypeface(typeface);
                holder.offtext.setTypeface(typeface);
    
                if(screenWidth>480){
                    holder.offtext.setTextSize(30);
                    holder.apptitleTxt.setTextSize(30);
                }
                convertView.setTag(holder);
            } else { 
                holder = (AppViewHolder) convertView.getTag();
            }
    
            holder.offtext.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    dialogList(holder.offtext, position);
                }
            });
    
            holder.apptitleTxt.setText(mApps.get(position).getTitle());
            holder.offtext.setText(mApps.get(position).getVersionName());
    
            return convertView; 
        }
    
        /**
         * Sets the list of apps to be displayed.
         * 
         * @param list the list of apps to be displayed
         */
        public void setListItems(List list) { 
            mApps = list; 
        }
    
        public void dialogList(final TextView textView, final int clickedPosition){
            Builder builder = new AlertDialog.Builder(_activity);
            builder.setTitle("Select Category");
            builder.setItems(R.array.category_list, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) 
                {
                    textView.setText(months.get(which+1));
                    App app = new App();
                    app.setTitle(mApps.get(clickedPosition).getTitle());
                    app.setPackageName(mApps.get(clickedPosition).getPackageName());
                    app.setVersionName(months.get(which+1));
                    app.setVersionCode(mApps.get(clickedPosition).getVersionCode());
                    mApps.set(clickedPosition, app);
                    System.out.println(clickedPosition+" : "+months.get(which+1));
    
    
                    update_database(mApps.get(clickedPosition).getPackageName(),months.get(which+1));
    
    
                    AppListAdapter.this.notifyDataSetChanged();
                }
    
            });
            builder.create();
            builder.show();
        }
    
        public void update_database(String packageName, String string) {
            CallBackDatabase callback = new CallBackDatabase(contextfordatabase);
            callback.open();
            Cursor cursor =callback.getAll(packageName);
            int y=cursor.getCount();
            int j=0;
            if(y!=0)
            {
                callback.UpdateCategory(packageName, string);
            }
            else
            {
                callback.InsertAppInfo(null, packageName, "0", "0", "0", "null", string);
            }
            cursor.deactivate();
            cursor.close();
            callback.close();
        }
    
    }
    

提交回复
热议问题