2013-06-17 1 views
7

TextView 및 Spinner가있는 ListView의 사용자 지정 어댑터로 작업하고 있습니다. Spinner에서 값을 선택한 후 해당 값의 동일한 Row 행의 TextView에 값을 복사합니다.안드로이드의 scroll에서 반복되는 값을 보여주는 listview 행의 TextView?

문제는 ListView에 40 개가 넘는 요소가 있기 때문에 첫 번째 회 전자를 선택하고 스크롤 할 때 각각의 TextView에 값을 설정하면 동일한 값이 10 번째 행 TextView에 표시됩니다.

값은 첫 번째 TextView에서 스크롤 할 때 10 번째 TextView로 복사됩니다. 다음은

내가 사용하고있는 코드입니다 :

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의 제목이 올바르게 설정되지 않았습니까? 그렇다면 getView 메소드를 사용할 때마다 제목이 표시되지 않습니다. OnItemSelectedListener에서만 수행합니다. 그 때문에 얼마간 오래된 데이터가 표시됩니다. –

+0

내 견해 좀 봐 http://stackoverflow.com/questions/6470089/why-did-the-listview-repeated-every-6th-item/41900575#41900575 – Sam

답변

2

나는이 문제에 대한 해결책을 얻었다. 나는 ArrayList로 작업하고있는 dialogList()을 소개했다. 아래에서 Adapter 클래스의 코드를 언급했습니다.

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

변경을 완료했습니다. 그러나 스크롤을 올렸을 때마다 값이 None으로 다시 초기화됩니다. –

+0

배열 목록이나 다른 곳으로 값을 저장 한 다음 getView()를 offTxt Textview로 복원해야합니다. –

+0

아직도 작동하지 않습니다 ... 첫째로 나는 Arraylist에서 사고를당했습니다. 충돌을 처리 한 후에도 문제는 같습니다. –

관련 문제