2012-03-20 64 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(); 
      }