简体   繁体   English

无法从转换视图获取父级职位

[英]Can't get parent position from convert view

I have a listView that currently displays the names of images along with a thumb of the image beside it. 我有一个listView,当前显示图像的名称以及旁边的图像缩略图。 I highlight the selected textBox green onClick, but while scrolling through the list other items are highlighted as well. 我将选中的文本框突出显示为绿色的onClick,但在列表中滚动时,其他项目也会突出显示。

ADAPTER: 适配器:

public class CustomListAdapter extends BaseAdapter {
   private ArrayList<String> data;
   private Boolean[] isSelected;
   private Activity activity;
   private static LayoutInflater inflater=null;
   public ImageLoader imageLoader;
   View vi;

public CustomListAdapter(Activity a, ArrayList<String> c){
    activity = a;
    data = c;       
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
    isSelected = new Boolean[data.size()];
    System.out.println("data size :  " + data.size());
    for(int i =0; i < isSelected.length; i++)isSelected[i] = false;
}
public int getCount(){
    return data.size();
}
public Object getItem(int position){
    return data.get(position);  
}

public long getItemId(int position){        
    return position;
}
public View getView(int position, View convertView, ViewGroup parent){
    vi=convertView;
    if(convertView == null) // if it's not recycled, initialize some attributes
        vi = inflater.inflate(R.layout.each, null);

        TextView text=(TextView)vi.findViewById(R.id.text);
        ImageView image=(ImageView)vi.findViewById(R.id.image);

        /////////////////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! HERE
        if(isSelected[position])text.setBackgroundColor(Color.GREEN);

        text.setText("image: "+ data.get(position).substring(data.get(position).lastIndexOf("/")+1, data.get(position).indexOf(".jpg")));
        imageLoader.DisplayImage(data.get(position), image);

        return vi;
    }
public void setMy_ItemSelected(int position, Boolean tf){/////each convertview resets this value?
    System.out.println("selected position size of array: " + Integer.toString(isSelected.length));
    System.out.println("selected position: " + position);

    if(tf){ isSelected[position] = true;}
    notifyDataSetChanged();
    System.out.println("selected position true/false: " + isSelected);
}

} }

CLICK LISTENER: 点击留言:

    private class dataExport implements AdapterView.OnItemClickListener {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

        adapter.setMy_ItemSelected(position, true);

        /*FileDialog popWindow = new FileDialog();              //USB pop window for folder selection
        popWindow.open_PathSelector(MainActivity.this);
        */
    }
}

You need two item view types: one for the selected item and one for all the unselected ones. 您需要两种项目视图类型:一种用于所选项目,另一种用于所有未选择的项目。 The adapter will automatically take care of only passing the correct type into your getView method. 适配器将自动处理仅将正确类型传递给您的getView方法。

At the moment, your adapter knows of only one type of view, so it will just pass any recycled view it has available into your getView method - some of which may still have the green highlight. 目前,您的适配器仅知道一种视图类型,因此它将仅将其可用的任何回收视图传递到getView方法中-其中一些可能仍带有绿色突出显示。

You need to implement getItemViewType and getViewTypeCount in your adapter and it will work. 您需要在适配器中实现getItemViewTypegetViewTypeCount ,它将起作用。

Edit 编辑

I'm bored right now so here's what it should be like: :D 我现在很无聊,所以这应该是这样的:D

protected static final int TYPE_NORMAL = 0;
protected static final int TYPE_SELECTED = 1;
public int getItemViewType(int position) {
    return isSelected[position] ? TYPE_SELECTED : TYPE_NORMAL;
}

public int getViewTypeCount() {
    return 2;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM