不能从转换查看上级位置 [英] Can't get parent position from convert view

查看:224
本文介绍了不能从转换查看上级位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有当前显示的图像的名字和它旁边的图像的拇指沿一个ListView。我想强调所选的textBox绿色的onClick,但在滚动列表其他项目将突出显示为好。帮助将AP preciated;是否可以很容易在第一次海报。

适配器:

 公共类CustomListAdapter延伸BaseAdapter {
   私人的ArrayList<串GT;数据;
   私人布尔[] isSelected;
   私人活动活动;
   私有静态LayoutInflater吹气= NULL;
   公共ImageLoader的ImageLoader的;
   查看六;公共CustomListAdapter(活动一,ArrayList的<串以及c){
    活性=一个;
    数据= C;
    吹气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ImageLoader的=新ImageLoader的(activity.getApplicationContext());
    isSelected =新布尔[data.size()];
    的System.out.println(数据大小:+ data.size());
    的for(int i = 0; I< isSelected.length;我++)isSelected [I] = FALSE;
}
公众诠释的getCount(){
    返回data.size();
}
公共对象的getItem(INT位置){
    返回data.get(位置);
}众长getItemId(INT位置){
    返回的位置;
}
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    VI = convertView;
    如果(convertView == NULL)//如果它没有回收,初始化一些属性
        VI = inflater.inflate(R.layout.each,NULL);        TextView的文本=(TextView的)vi.findViewById(R.id.text);
        ImageView的图像=(ImageView的)vi.findViewById(R.id.image);        ///////////////// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!这里
        如果(isSelected [位置])text.setBackgroundColor(Color.GREEN);        text.setText(形象:。JPG+ data.get(位置).substring(da​​ta.get(位置).lastIndexOf(/)+ 1,data.get(位置).indexOf())) ;
        imageLoader.DisplayImage(data.get(位置),图像);        返回VI;
    }
公共无效setMy_ItemSelected(INT位置,布尔TF){/////每个convertview重置该值?
    的System.out.println(数组中选择的位置大小:+ Integer.toString(isSelected.length));
    的System.out.println(选择的位置:+位置);    如果(TF){isSelected [位置] =真;}
    notifyDataSetChanged();
    的System.out.println(选定位置真/假:+ isSelected);
}

}

点击监听器:

 私有类数据导出实现AdapterView.OnItemClickListener {
    公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){        adapter.setMy_ItemSelected(位置,真正的);        / *的FileDialog popWindow =新的FileDialog();为文件夹选择// USB弹出窗口
        popWindow.open_PathSelector(MainActivity.this);
        * /
    }
}


解决方案

您需要两个项目视图类型:一为选定的项目,一个用于所有没有选择的。该适配器将自动的只有通过正确的类型到您的 getView 方法治疗。

目前,您的适配器只有一种类型的视图的人都知道,所以它只会通过任何回收的观点有可用到 getView 法 - 其中一些可能还有绿色的亮点。

您需要执行 getItemViewType getViewTypeCount 在适配器中,它会工作。

修改

我很无聊,现在所以这里应该是什么样子::D

 受保护的静态最终诠释TYPE_NORMAL = 0;
受保护的静态最终诠释TYPE_SELECTED = 1;
公众诠释getItemViewType(INT位置){
    返回isSelected [位置] TYPE_SELECTED:TYPE_NORMAL;
}公众诠释getViewTypeCount(){
    返回2;
}

I have a listView that currently displays the names of images along with a thumb of the image beside it. I highlight the selected textBox green onClick, but while scrolling through the list other items are highlighted as well. Help would be appreciated; take it easy on a first time poster.

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.

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.

You need to implement getItemViewType and getViewTypeCount in your adapter and it will work.

Edit

I'm bored right now so here's what it should be like: :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;
}

这篇关于不能从转换查看上级位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆