2012-03-20 43 views
1

我正在嘗試創建任務管理器,而我只有一個問題。我有一個充氣的列表視圖。列表視圖中的所有元素都是正確的。問題是,當我選擇一個項目時,listview會選擇另一個項目。我聽說列表視圖重新填充列表,因爲它向下滾動以節省內存。我認爲這可能是某種問題。這是problem的圖片。 如果我加載了更多的應用程序,那麼它會一次選擇多個應用程序。Listview在點擊時選擇多個項目

這是我的適配器和活性和XML相關的

public class TaskAdapter extends BaseAdapter{ 
private Context mContext; 
private List<TaskInfo> mListAppInfo; 
private PackageManager mPack; 


public TaskAdapter(Context c, List<TaskInfo> list, PackageManager pack) { 
    mContext = c; 
    mListAppInfo = list; 
    mPack = pack; 
} 

@Override 
public int getCount() { 
    return mListAppInfo.size(); 
} 

@Override 
public Object getItem(int position) { 
    return mListAppInfo.get(position); 
} 

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

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    TaskInfo entry = mListAppInfo.get(position); 


    if (convertView == null) 
    { 
     LayoutInflater inflater = LayoutInflater.from(mContext); 
     //System.out.println("Setting LayoutInflater in TaskAdapter " +mContext +" " +R.layout.taskinfo +" " +R.id.tmbox); 
     convertView = inflater.inflate(R.layout.taskinfo,null); 
    } 

     ImageView ivIcon = (ImageView)convertView.findViewById(R.id.tmImage); 
     ivIcon.setImageDrawable(entry.getIcon()); 

     TextView tvName = (TextView)convertView.findViewById(R.id.tmbox); 
     tvName.setText(entry.getName()); 

     convertView.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) { 
       final CheckBox checkBox = (CheckBox)v.findViewById(R.id.tmbox); 
       if(v.isSelected()) 
       { 
        System.out.println("Listview not selected "); 
        //CK.get(arg2).setChecked(false); 
        checkBox.setChecked(false); 
        v.setSelected(false); 
       } 
       else 
       { 
        System.out.println("Listview selected "); 
        //CK.get(arg2).setChecked(true); 
        checkBox.setChecked(true); 
        v.setSelected(true); 
       } 

      } 
     }); 

    return convertView; 




public class TaskManager extends Activity implements Runnable 
    { 
private ProgressDialog pd; 
private TextView ram; 
private String s; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.taskpage); 
     setTitleColor(Color.YELLOW); 

     Thread thread = new Thread(this); 
     thread.start(); 

    } 
    @Override 
    public void run() 
    { 
     //System.out.println("In Taskmanager Run() Thread"); 
     final PackageManager pm = getPackageManager(); 
     final ListView box = (ListView) findViewById(R.id.cBoxSpace); 
     final List<TaskInfo> CK = populate(box, pm); 
     runOnUiThread(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       ram.setText(s); 
       box.setAdapter(new TaskAdapter(TaskManager.this, CK, pm)); 

       //System.out.println("In Taskmanager runnable Run()");  
       endChecked(CK); 
      } 
     }); 
       handler.sendEmptyMessage(0); 
    } 

Taskinfo.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:gravity="center_horizontal"> 

<ImageView 
    android:id="@+id/tmImage" 
    android:layout_width="48dp" 
    android:layout_height="48dp" 
    android:scaleType="centerCrop" 
    android:adjustViewBounds="false" 
    android:focusable="false" /> 
<CheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/tmbox" 
    android:lines="2"/> 
     </LinearLayout> 

Taskpage.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 
    <ListView 
     android:id="@+id/cBoxSpace" 
     android:layout_width="wrap_content" 
     android:layout_height="400dp" 
     android:orientation="vertical"/> 
<TextView 
     android:id="@+id/RAM" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="18sp" /> 
<Button 
     android:id="@+id/endButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="End Selected Tasks" /> 
</LinearLayout> 

任何想法是什麼原因複式項目的代碼選擇與一個單一的點擊將非常感激。我一直在搞不同的實現,聽衆和listadapters,但無濟於事。

+1

當選擇多個項目時,多個選項是否出現在屏幕上可見的項目上或僅在滾動時出現? – 2012-03-20 17:33:56

+0

對不起,沒有編輯您的文章,意味着編輯我的。<我在編輯中解釋了答案 – xlph 2012-03-20 20:25:09

回答

1

我認爲重點是您只保存視圖中的檢查狀態(v.setSelected)。

而你重用這些視圖,所以它的複選框總是不會改變它的狀態。

您可以創建狀態數組來保存每個TaskInfo的每個檢查狀態,並在創建視圖時檢查此數組。

例如

// default is false 
ArrayList<Boolean> checkingStates = new ArrayList<Boolean>(mListAppInfo.size()); 
@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    TaskInfo entry = mListAppInfo.get(position); 
    if (convertView == null) 
    { 
     LayoutInflater inflater = LayoutInflater.from(mContext); 
     convertView = inflater.inflate(R.layout.taskinfo,null); 
    } 

    ImageView ivIcon = (ImageView)convertView.findViewById(R.id.tmImage); 
    ivIcon.setImageDrawable(entry.getIcon()); 

    TextView tvName = (TextView)convertView.findViewById(R.id.tmbox); 
    tvName.setText(entry.getName()); 

    final CheckBox checkBox = (CheckBox)v.findViewById(R.id.tmbox); 
    checkBox.setChecked(checkingStates.get(position)); 
    convertView.setSelected(checkingStates.get(position)); 

    convertView.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) { 
      if(v.isSelected()) 
      { 
       System.out.println("Listview not selected "); 
       //CK.get(arg2).setChecked(false); 
       checkBox.setChecked(false); 
       v.setSelected(false); 
       checkingStates.get(position) = false; 
      } 
      else 
      { 
       System.out.println("Listview selected "); 
       //CK.get(arg2).setChecked(true); 
       checkBox.setChecked(true); 
       v.setSelected(true); 
       checkingStates.get(position) = true; 
      } 

     } 
    }); 

return convertView; 
} 
1

我不是100%肯定你正在嘗試做的,但你的問題的一部分可能會在onClick方法進行相關的條件:

if(v.isSelected()) 

我想你想要讀

if(v.isChecked()) 

isSelected是繼承並且這意味着與isChecked不同isChecked

此外,CheckBox是否被選中與數據模型無關,因爲它是一個循環視圖。你的CheckBox應根據entry選中(我假設你TextInfo類有一個isChecked()方法返回一個布爾值:

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    TaskInfo entry = mListAppInfo.get(position); 

    if (convertView == null) 
    { 
     LayoutInflater inflater = LayoutInflater.from(mContext); 
     //System.out.println("Setting LayoutInflater in TaskAdapter " +mContext +" " +R.layout.taskinfo +" " +R.id.tmbox); 
     convertView = inflater.inflate(R.layout.taskinfo,null); 
    } 

    ImageView ivIcon = (ImageView)convertView.findViewById(R.id.tmImage); 
    ivIcon.setImageDrawable(entry.getIcon()); 

    TextView tvName = (TextView)convertView.findViewById(R.id.tmbox); 
    tvName.setText(entry.getName()); 

    CheckBox checkBox = (CheckBox)v.findViewById(R.id.tmbox); 
    checkBox.setChecked(entry.isChecked()); 
} 

我不認爲你需要的View.OnClickListener要連接到convertView你應該處理。在連接到ListViewOnItemClickListener假設您的ListView被稱爲listViewTaskInfo實例具有setCheckedisChecked方法:

listView.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView parent, View v, int position, long id) { 
     entry = mListAppInfo.get(position); 
     entry.setChecked(!entry.isChecked()); 
    } 
}); 
0
First of all don't set the list checked or unchecked on view position. 
because view position means only visible items position in your listview but you would like to set checked or uncheked status on a particular list item. 

that's why this problem arising in your code. 


You have the need to set the items checked and unchecked on your custom arraylist getter setter like the code i have attached below: 


package com.app.adapter; 



public class CategoryDynamicAdapter { 

    public static ArrayList<CategoryBean> categoryList = new ArrayList<CategoryBean>(); 

    Context context; 
    Typeface typeface; 
    public static String videoUrl = "" ;  
    Handler handler; 
    Runnable runnable; 


     // constructor 
     public CategoryDynamicAdapter(Activity a, Context context, Bitmap [] imagelist,ArrayList<CategoryBean> list) { 

     this.context = context; 
     this.categoryList  = list; 
     this.a = a; 


    } 

    // Baseadapter to the set the data response from web service into listview. 
    public BaseAdapter mEventAdapter = new BaseAdapter() { 



     @Override 
     public int getCount() { 
      return categoryList.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return categoryList.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return 0; 
     } 

     class ViewHolder { 
      TextView title,category,uploadedBy; 
      ImageView image; 
      RatingBar video_rating; 
      Button report_video ,Flag_video; 
     } 

     public View getView(final int position, View convertView, final ViewGroup parent) { 
      ViewHolder vh = null ; 

       if(convertView == null) { 

       vh     =   new               ViewHolder(); 
       convertView   =   LayoutInflater.from(context).inflate (R .layout.custom_category_list_layout,null,false); 
       vh.title    =   (TextView)    convertView    .findViewById  (R.id.title); 
       vh.image = (ImageView)   convertView.findViewById(R.id.Imagefield); 

       convertView.setTag(vh); 
      } 
      else 
      { 
       vh=(ViewHolder) convertView.getTag(); 
      } 

      try 
      { 
       final CategoryBean Cb = categoryList.get(position); 


//pay attention to code below this line i have shown here how to select a listview using arraylist getter setter objects 

       String checkedStatus = Cb.getCheckedStringStaus(); 
      if(checkdStatus.equal("0") 
       { 
        System.out.println("Listview not selected "); 
        //CK.get(arg2).setChecked(false); 
        checkBox.setChecked(false); 
        v.setSelected(false); 
       } 
       else    ////checkdStatus.equal("1") 
       { 
        System.out.println("Listview selected "); 
        //CK.get(arg2).setChecked(true); 
        checkBox.setChecked(true); 
        v.setSelected(true); 
       } 

      catch (Exception e) 
      { 
       e.printStackTrace(); 
      }