2013-06-17 7 views
7

मैं एक सूची दृश्य के कस्टम एडाप्टर के साथ काम कर रहा हूं जिसमें मेरे पास टेक्स्ट व्यू और स्पिनर है। एक स्पिनर से मूल्यों का चयन करने के बाद, सूची के संबंधित पंक्ति के टेक्स्ट व्यू में कॉपी करने के बाद मूल्य।एंड्रॉइड में स्क्रॉल पर दोहराए गए मान दिखाते हुए सूचीदृश्य पंक्तियों में टेक्स्ट व्यू?

समस्या है, क्योंकि मेरे पास सूची दृश्य में 40 से अधिक तत्व हैं, जब मैं पहला स्पिनर चुनता हूं और स्क्रॉल पर संबंधित टेक्स्ट व्यू को मान सेट करता हूं, तो वही मान 10 वीं पंक्ति टेक्स्ट व्यू में देखा जाता है।

स्क्रॉल पर पहले टेक्स्ट व्यू 10 वीं टेक्स्ट व्यू से मूल्यों की प्रतिलिपि बनाई गई है।

नीचे कोड जो मैं उपयोग कर रहा हूँ है:

public class AppListAdapter extends BaseAdapter { 

    private LayoutInflater mInflater; 
    private List<App> mApps = Constants.list; 
    private Context _activity; 
    ArrayList<String> months=null; 
    ArrayAdapter<String> dataAdapter =null; 
    int spinerposition; 
    int viewposition; 

    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) { 
     // 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<String>(); 
     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"); 


     // Creating adapter for spinner 
     dataAdapter = new ArrayAdapter<String>(_activity, 
       android.R.layout.simple_spinner_item, months); 

     // Drop down layout style - list view with radio button 
     dataAdapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice); 


    } 

    public int getCount() { 

     return mApps.size(); 
    } 

    public Object getItem(int position) { 
     return mApps.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

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

     final AppViewHolder holder; 

     viewposition=position; 
     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.offTxt = (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.offTxt.setTypeface(typeface); 

      if(screenWidth>480){ 
       holder.offTxt.setTextSize(30); 
       holder.apptitleTxt.setTextSize(30); 
      } 
      convertView.setTag(holder); 
     } else { 
      holder = (AppViewHolder) convertView.getTag(); 
     } 

     holder.setTitle(mApps.get(position).getTitle(),mApps.get(position).getVersionName()); 

     notifyDataSetChanged(); 

     holder.offTxt.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       holder.spiner.performClick(); 

      } 
     }); 

     holder.spiner.setAdapter(dataAdapter); 
     holder.spiner.setOnItemSelectedListener(new OnItemSelectedListener() { 
      public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) { 
       spinerposition=arg2; 
       switch (spinerposition) 
       { 
       case 1: 

        holder.offtext.setText("None"); 
        break; 
       case 2: 

        holder.offtext.setText("Entertainment"); 
        break; 
       case 3: 

        holder.offtext.setText("Games"); 
        break; 
       case 4: 

        holder.offtext.setText("News/Books"); 
        break; 
       case 5: 

        holder.offtext.setText("Social Networking"); 
        break; 
       case 6: 

        holder.offtext.setText("Utilities"); 
        break; 
       case 7: 

        holder.offtext.setText("Texting"); 
        break; 
       case 8: 

        holder.offtext.setText("Web Browsers"); 
        break; 
       } 
      } 
      public void onNothingSelected(AdapterView<?> arg0) { 
      } 
     }); 
     return convertView; 
    } 



    /** 
    * Sets the list of apps to be displayed. 
    * 
    * @param list the list of apps to be displayed 
    */ 
    public void setListItems(List<App> list) { 
     mApps = list; 
    } 

    /** 
    * A view holder which is used to re/use views inside a list. 
    */ 
    public class AppViewHolder { 

     private TextView mTitle = null; 
     private TextView apptitleTxt = null; 
     private TextView offTxt = null; 
     private Spinner spiner=null; 
     public TextView offtext; 
     /** 
     * Sets the text to be shown as the app's title 
     * 
     * @param title the text to be shown inside the list row 
     */ 
     public void setTitle(String title,String category) { 
      apptitleTxt.setText(title); 
//   offtext.setText(category); 
     } 
    } 

} 
+0

क्या holder.offtext के लिए शीर्षक सही ढंग से सेट नहीं हो रहा है? यदि ऐसा है तो मैं आपको हर बार प्राप्त दृश्य में नहीं देखता हूं। आप इसे केवल अपने OnItemSlectedListener में करते हैं। जिसके कारण यह कुछ समय में पुराना डेटा दिखाएगा। –

+0

मेरे विचार पर एक नज़र डालें http://stackoverflow.com/questions/6470089/why-did-the-listview-repeated-every-6th-item/41900575#41900575 – Sam

उत्तर

2

मैं इस मुद्दे के लिए समाधान मिल गया। मैंने dialogList() पेश किया है जिसमें मैं एक ऐरेलिस्ट के साथ काम कर रहा हूं। नीचे मैंने अपने एडाप्टर वर्ग के कोड का उल्लेख किया है।

public class AppListAdapter extends BaseAdapter { 

    private LayoutInflater mInflater; 
    private List<App> mApps = Constants.list; 
    private Context _activity; 
    ArrayList<String> months=null; 
    ArrayAdapter<String> 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<String>(); 
     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<App> 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(); 
    } 

} 
+0

धन्यवाद सैम, मुझे भी इस मुद्दे का सामना करना पड़ रहा था। इससे मुझे उस समस्या से छुटकारा पाने में मदद मिली। –

+0

मेरी सभी खुशी ... समुदाय के लिए :) –

+0

धन्यवाद सैम यह वास्तव में मदद ..... –

1

इसका कारण यह है ListView सभी पिछले Views पुन: उपयोग किया जाता है, जो स्क्रॉल किया जाता है (दिखाई नहीं)।

कृपया this विषय देखें।

+0

हाँ ... मुझे इस मुद्दे के साथ किया गया है। सूची दृश्य गुणों के बारे में बड़ा ज्ञान मिला। जो भी हो धन्यवाद। –

1

बदलें

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.offTxt = (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.offTxt.setTypeface(typeface); 

      if(screenWidth>480){ 
       holder.offTxt.setTextSize(30); 
       holder.apptitleTxt.setTextSize(30); 
      } 
      convertView.setTag(holder); 
     } else { 
      holder = (AppViewHolder) convertView.getTag(); 
     } 

को
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.offTxt = (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.offTxt.setTypeface(typeface); 

    if(screenWidth>480){ 
     holder.offTxt.setTextSize(30); 
     holder.apptitleTxt.setTextSize(30); 
    } 
    convertView.setTag(holder); 

अब आप देख सकते हैं कि हर चीज ठीक से काम करेंगे। समस्या को ठीक करने का यह सबसे अच्छा तरीका नहीं है !, रीसाइक्लिंग के लिए कन्वर्ट व्यू का उपयोग करते समय आप की स्थिति का प्रबंधन करने की आवश्यकता है।

संपादित

public class AppListAdapter extends BaseAdapter { 

    private LayoutInflater mInflater; 
    private List<App> mApps = Constants.list; 
    private Context _activity; 
    ArrayList<String> months=null; 
    ArrayAdapter<String> dataAdapter =null; 
    int spinerposition; 
    int viewposition; 

    int temp=0; 
    private int screenWidth; 


    ArrayList<String> vals=null; 


    /** 
    * Constructor. 
    * 
    * @param context the application context which is needed for the layout inflater 
    * @param screenWidth 
    */ 
    public AppListAdapter(Context context, int screenWidth) { 
     // 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<String>(); 
     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"); 

     vals = new ArrayList<String>(); 


     // Creating adapter for spinner 
     dataAdapter = new ArrayAdapter<String>(_activity, 
       android.R.layout.simple_spinner_item, months); 

     // Drop down layout style - list view with radio button 
     dataAdapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice); 


    } 

    public int getCount() { 

     return mApps.size(); 
    } 

    public Object getItem(int position) { 
     return mApps.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

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

     final AppViewHolder holder; 

     viewposition=position; 

      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.offTxt = (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.offTxt.setTypeface(typeface); 

      if(screenWidth>480){ 
       holder.offTxt.setTextSize(30); 
       holder.apptitleTxt.setTextSize(30); 
      } 
      convertView.setTag(holder); 


if(vals.get(position)!=null) 
{ 
holder.offtext.setText(vals.get(position)); 
} 


     holder.setTitle(mApps.get(position).getTitle(),mApps.get(position).getVersionName()); 

     notifyDataSetChanged(); 

     holder.offTxt.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       holder.spiner.performClick(); 

      } 
     }); 

     holder.spiner.setAdapter(dataAdapter); 
     holder.spiner.setOnItemSelectedListener(new OnItemSelectedListener() { 
      public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) { 
       spinerposition=arg2; 
       switch (spinerposition) 
       { 
       case 1: 

        holder.offtext.setText("None"); 
        break; 
       case 2: 

        holder.offtext.setText("Entertainment"); 
        break; 
       case 3: 

        holder.offtext.setText("Games"); 
        break; 
       case 4: 

        holder.offtext.setText("News/Books"); 
        break; 
       case 5: 

        holder.offtext.setText("Social Networking"); 
        break; 
       case 6: 

        holder.offtext.setText("Utilities"); 
        break; 
       case 7: 

        holder.offtext.setText("Texting"); 
        break; 
       case 8: 

        holder.offtext.setText("Web Browsers"); 
        break; 
       } 
     vals.add(arg2,holder.offtext.getText()); 
      } 
      public void onNothingSelected(AdapterView<?> arg0) { 
      } 
     }); 
     return convertView; 
    } 



    /** 
    * Sets the list of apps to be displayed. 
    * 
    * @param list the list of apps to be displayed 
    */ 
    public void setListItems(List<App> list) { 
     mApps = list; 
    } 

    /** 
    * A view holder which is used to re/use views inside a list. 
    */ 
    public class AppViewHolder { 

     private TextView mTitle = null; 
     private TextView apptitleTxt = null; 
     private TextView offTxt = null; 
     private Spinner spiner=null; 
     public TextView offtext; 
     /** 
     * Sets the text to be shown as the app's title 
     * 
     * @param title the text to be shown inside the list row 
     */ 
     public void setTitle(String title,String category) { 
      apptitleTxt.setText(title); 
//   offtext.setText(category); 
     } 
    } 

} 
+0

मैंने परिवर्तन किया है, लेकिन जब भी मैं नीचे स्क्रॉल करता हूं और फिर से मूल्य को फिर से शुरू नहीं करता हूं। –

+0

आपको मूल्यों को किसी सरणी सूची में सहेजने की आवश्यकता है या कुछ और कहां है और फिर इसे बंद करें() को ऑफ टेक्स्ट टेक्स्टव्यू में पुनर्स्थापित करें। –

+0

यह अभी भी काम नहीं कर रहा है ... पहली बार दुर्घटना को संभालने के बाद, मैं ऐरेलिस्ट में दुर्घटनाग्रस्त हो रहा था, समस्या एक ही है। –

संबंधित मुद्दे