2012-06-06 77 views
0

我使用this在列表視圖中設置複選框我已按照給定的教程,但有一些關鍵的問題與輸出,所以當我選擇第一個複選框,並向下滾動將改變選定的所有步驟項目並自動出現3。問題與複選框選中

所以我認爲getview有問題。所以請大家幫我出這....

這裏是我的代碼::

package com.AppFavorits; 

import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 

import android.app.ListActivity; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.CompoundButton; 
import android.widget.ListView; 
import android.widget.RatingBar; 

public class Favorites extends ListActivity { 
    protected static final String TAG = "Favorites"; 
    CommentsDataSource datasource; 
    ListView lstFavrowlistv; 
    ArrayList alAppName; 
    float[] rate; 
    boolean[] bSelected; 
    ArrayList<Comment> alPackagenm; 
    Drawable[] alIcon; 
    ViewHolder holder; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     datasource = new CommentsDataSource(this); 
     datasource.open(); 

     alAppName = datasource.getAllComments(); 
     alPackagenm = datasource.getAllPackage(); 

     Log.i(TAG, "values >>>" + alAppName); 
     Log.i(TAG, "values >>>" + alPackagenm); 
     int inc = 0; 
     alIcon = new Drawable[200]; 
     for (int i = 0; i < alPackagenm.size(); i++) { 
      Log.i(TAG, "Appname >>>" + GetAllApp.lstpinfo.get(i).pname); 
      for (int j = 0; j < GetAllApp.lstpinfo.size(); j++) { 
       if (alPackagenm 
         .get(i) 
         .toString() 
         .equalsIgnoreCase(
           GetAllApp.lstpinfo.get(j).pname.toString())) { 
        alIcon[inc] = GetAllApp.lstpinfo.get(j).icon; 
        Log.i("TAG", "sqlPackagename" 
          + alPackagenm.get(i).toString()); 
        Log.i("TAG", "from getAllapp" 
          + GetAllApp.lstpinfo.get(j).pname.toString()); 
        inc++; 
       } 

      } 
     } 

     ArrayList<RowModel> list = new ArrayList<RowModel>(); 
     ArrayList<Model> Mlist = new ArrayList<Model>(); 
     rate = new float[alAppName.size()]; 
     bSelected = new boolean[alAppName.size()]; 
     Iterator itr = alAppName.iterator(); 
     String strVal = null; 
     while (itr.hasNext()) { 
      strVal += itr.next().toString() + ","; 

     } 

     int lastIndex = strVal.lastIndexOf(","); 
     strVal = strVal.substring(0, lastIndex); 
     System.out.println("Output String is : " + strVal); 
     String strAr[] = strVal.split(","); 

     for (int i = 0; i < strAr.length; i++) { 
      System.out.println("strAr[" + i + "] " + strAr[i]); 
     } 

     for (String s : strAr) { 
      list.add(new RowModel(s)); 
     } 
     for (String s : strAr) { 
      Mlist.add(new Model(s)); 
     } 
     setListAdapter(new RatingAdapter(list, Mlist)); 
     datasource.close(); 
    } 

    class RowModel { 
     String label; 
     float rating = 0.0f; 

     RowModel(String label) { 
      this.label = label; 
     } 

     public String toString() { 
      if (rating >= 3.0) { 
       return (label.toUpperCase()); 
      } 

      return (label); 
     } 
    } 

    private RowModel getModel(int position) { 
     return (((RatingAdapter) getListAdapter()).getItem(position)); 

    } 

    class RatingAdapter extends ArrayAdapter<RowModel> { 
     private ArrayList<Model> mlist; 

     RatingAdapter(ArrayList<RowModel> list, ArrayList<Model> mlist) { 
      super(Favorites.this, R.layout.outbox_list_item, 
        R.id.txvxFavrowiconappname, list); 
      this.mlist = mlist; 
     } 

     public View getView(final int position, View convertView, 
       ViewGroup parent) { 
      View row = super.getView(position, convertView, parent); 
      holder = (ViewHolder) row.getTag(); 
      if (holder == null) { 
       holder = new ViewHolder(row); 
       row.setTag(holder); 

       RatingBar.OnRatingBarChangeListener l = new RatingBar.OnRatingBarChangeListener() { 
        public void onRatingChanged(RatingBar ratingBar, 
          float rating, boolean fromTouch) { 
         Integer myPosition = (Integer) ratingBar.getTag(); 
         RowModel model = getModel(myPosition); 

         model.rating = rating; 
         rate[position] = rating; 

        } 
       }; 
       holder.chkbxFavrowsel 
         .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

          @Override 
          public void onCheckedChanged(
            CompoundButton buttonView, boolean isChecked) { 
           Model element = (Model) holder.chkbxFavrowsel 
             .getTag(); 
           element.setSelected(buttonView.isChecked()); 
           bSelected[position] = isChecked; 

          } 
         }); 
       holder.chkbxFavrowsel.setTag(mlist.get(position)); 
       holder.ratingBar1.setOnRatingBarChangeListener(l); 
      } else { 
       row = convertView; 
       ((ViewHolder) row.getTag()).chkbxFavrowsel.setTag(mlist 
         .get(position)); 
      } 

      RowModel model = getModel(position); 
      ViewHolder holder = (ViewHolder) row.getTag(); 
      holder.ratingBar1.setTag(new Integer(position)); 
      holder.ratingBar1.setRating(model.rating); 
      holder.imgvFavrowiconappicon.setImageDrawable(alIcon[position]); 
      holder.txvxFavrowiconappname.setText(alAppName.get(position) 
        .toString()); 
      holder.chkbxFavrowsel.setChecked(mlist.get(position).isSelected()); 

      return (row); 
     } 
    } 
} 

回答

0

我真的不明白你的代碼正是你想要做的。在此之前我沒有看到您正在檢查holder == null,因爲它應該是convertView == null。如果你有滾動問題,你可以檢查我的blog post

holder = (ViewHolder) row.getTag(); 
      if (holder == null) { 
       holder = new ViewHolder(row); 
       row.setTag(holder); 
       ... 
      } 
      else { 
       row = convertView; 
       ((ViewHolder) row.getTag()).chkbxFavrowsel.setTag(mlist 
         .get(position)); 
      } 
0

使用下面的代碼到你的MainActivity.java文件。

public class ListViewActivity extends Activity { 

    ListView mLstView1; 
    Button mBtn1; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     mLstView1 = (ListView) findViewById(R.id.mLstView1); 
     String[] lv_items = { "Dipak", "Rahul", "Hiren", "Nandlal", "Keyur", 
       "Kapil", "Dipak", "Rahul", "Hiren", "Nandlal", "Keyur", 
       "Kapil" }; 
     ; 
     mLstView1.setAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_multiple_choice, lv_items)); 
     mLstView1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

     mBtn1 = (Button) findViewById(R.id.mBtn1); 
     mBtn1.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       // Using a List to hold the IDs, but could use an array. 
       List<Integer> checkedIDs = new ArrayList<Integer>(); 
       // Get all of the items that have been clicked - either on or 
       // off 
       final SparseBooleanArray checkedItems = mLstView1 
         .getCheckedItemPositions(); 
       for (int i = 0; i < checkedItems.size(); i++) { 
        // And this tells us the item status at the above position 
        final boolean isChecked = checkedItems.valueAt(i); 
        if (isChecked) { 
         // This tells us the item position we are looking at 
         final int position = checkedItems.keyAt(i); 
         // Put the value of the id in our list 
         checkedIDs.add(position); 
         System.out.println("Position is:- " + position); 
        } 
       } 
      } 
     }); 
    } 
}