如何保存更改的ImageView的状态列表视图行,它已经从屏幕上消失后? [英] How to save state of changed ImageView in Listview row, after it' has disappeared from screen?

查看:128
本文介绍了如何保存更改的ImageView的状态列表视图行,它已经从屏幕上消失后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这个问题是这样的:我设计了一个列表视图的应用程序中,我可以改变列表项的颜色,这里是从应用到屏幕的链接(我会很高兴,如果有人能发布为图像)

http://i.stack.imgur.com/0Xd9d.jpg

颜色由上列表行的左侧部分改变图像改变。
现在,当我改变颜色滑下在列表中,并且回来了颜色回到原来的状态(这意味着图像的视图回到它的原始源)。

为列表的数据是从一个XML文件中取出,解析到散列映射,并使用膨胀成列表中的一个适配器。

做了一些人知道我怎么可以去解决这个问题?

当我看到它,因为列表视图重用那些不在屏幕上的意见,当我回来的改变一行走出了画面,然后回来从哈希地图再次PULS其信息。所以我需要做的是寻找被绑定的适配器排布置,并有改变相关的颜色的哈希地图数据的索引,接下来的时间适配器重现此行,将被分配的方式用正确的颜色。有些人有一个想法,它是如何做?

在此先感谢。

下面是java code为这项活动:

 公共类SkyGiraffeAppActivity扩展活动实现AdapterView.OnItemClickListener,View.OnClickListener {
//所有静态变量
静态最后弦乐TAG =SkyGiraffeAppActivity;
// XML节点键
静态最后弦乐KEY_TASK =任务; //父节点
静态最后弦乐KEY_ID =ID;
静态最后弦乐KEY_TITLE =称号;
静态最后弦乐KEY_DATE =日期;
静态最后弦乐KEY_TIME =时间;
静态最后弦乐KEY_PRIORITY =优先级;
静态最后弦乐KEY_USERPRIORITY =userpriority;查看currentRow;
最终的ArrayList<&HashMap的LT;字符串,字符串>>菜单项=新的ArrayList<&HashMap的LT;字符串,字符串>>();@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    ListView控件列表=新的ListView(这一点);
    list.setDivider(NULL);
    list.setDividerHeight(0);    的setContentView(名单);
    。getWindow()setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.taskstitle);
    Log.e(TAG,创建阵列);    XMLParser的解析器=新XMLParser的();    Log.e(TAG,从原始文件夹中获取XML);
    InputStream的XML = getResources()openRawResource(R.raw.taskslists)。 //获取XML    Log.e(TAG,读取XML文件的字符串);
    串sxml = parser.readTextFile(XML);    Log.e(TAG,创造从字符串DOM元素);
    文档的DOC = parser.getDomElement(sxml); //获得DOM元素    Log.e(TAG,所有的元素越来越节点列表);
    节点列表节点列表= doc.getElementsByTagName(KEY_TASK);    Log.e(TAG,通过所有的元素循环);
    的for(int i = 0; I< nodeList.getLength();我++)
    {
        //创建新的HashMap
        HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
        元素e =(元)nodeList.item(I)
        Log.e(TAG元素号+ I +添加到哈希映射);
        //将每个子节点的HashMap键=>值
        map.put(KEY_ID,parser.getValue(即KEY_ID));
        map.put(KEY_TITLE,parser.getValue(即KEY_TITLE));
        map.put(KEY_DATE,parser.getValue(即KEY_DATE));
        map.put(KEY_TIME,parser.getValue(即KEY_TIME));
        map.put(KEY_PRIORITY,parser.getValue(即KEY_PRIORITY));
        map.put(KEY_USERPRIORITY,parser.getValue(即KEY_USERPRIORITY));        //添加HashList到ArrayList的
        menuItems.add(地图);
    }    //添加菜单项到ListView控件
    ListAdapter适配器=新SimpleAdapter(这一点,菜单项,R.layout.row,
          新的String [] {KEY_TITLE,KEY_DATE,KEY_TIME,KEY_PRIORITY,KEY_USERPRIORITY},
          新的INT [] {R.id.text_title,R.id.text_date,R.id.text_time,R.id.image_priority,R.id.bchangecolor}){        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
            查看排= super.getView(位置,convertView,父母);
            查看左= row.findViewById(R.id.bchangecolor);
            left.setTag(位置);
            left.setOnClickListener(SkyGiraffeAppActivity.this);
            查看右= row.findViewById(R.id.barrow);
            right.setTag(位置);
            right.setOnClickListener(SkyGiraffeAppActivity.this);
            ImageView的prioriy =(ImageView的)row.findViewById(R.id.image_priority);
            ImageView的usercolor =(ImageView的)row.findViewById(R.id.bchangecolor);
            字符串userpriority = menuItems.get(位置)获得(KEY_USERPRIORITY);
            Log.e(TAG,对这一观点用户优先级的颜色为:+ userpriority);            如果(userpriority.contentEquals(绿色))
            {
                usercolor.setImageResource(R.drawable.task_bg_left_green);
            }
            否则,如果(userpriority.contentEquals(蓝))
            {
                usercolor.setImageResource(R.drawable.task_bg_left_blue);
            }
            否则,如果(userpriority.contentEquals(黄))
            {
                usercolor.setImageResource(R.drawable.task_bg_left_yellow);
            }
            否则,如果(userpriority.contentEquals(橙色))
            {
                usercolor.setImageResource(R.drawable.task_bg_left_orange);
            }
            否则,如果(userpriority.contentEquals(红))
            {
                usercolor.setImageResource(R.drawable.task_bg_left_red);
            }
            INT数= Integer.valueOf(menu​​Items.get(位置)获得(KEY_PRIORITY));
            开关(数)
            {
            情况1:
                prioriy.setImageResource(R.drawable.priority_first);
                打破;
            案例2:
                prioriy.setImageResource(R.drawable.priority_second);
                打破;
            案例3:
                prioriy.setImageResource(R.drawable.priority_third);
                打破;
            情况4:
                prioriy.setImageResource(R.drawable.priority_fourth);
                打破;
            情况5:
                prioriy.setImageResource(R.drawable.priority_fifth);
                打破;
            默认:
                prioriy.setImageResource(R.drawable.priority_first);
            }            返回行;
        }
    };    list.setAdapter(适配器);
    list.setOnItemClickListener(本);
}公共无效的onClick(视图v)
{
     意图I =新意图(这一点,TaskColorChangeActivity.class);
     i.putExtra(色,CHOOSECOLOR);     开关(v.getId()){
        案例R.id.bchangecolor:
            Log.e(TAG,pressed的颜色变化按钮+ v.getId());
            currentRow = V;
            startActivityForResult(I,1);
            打破;
        案例R.id.barrow:
            Log.e(TAG,pressed上的箭头键+ v.getId());
            I = createIntentwithExtra(v.getRootView());
            startActivity(ⅰ);
            打破;
        默认:
            打破;
        }
}@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    // TODO自动生成方法存根
    super.onActivityResult(要求code,结果code,数据);    如果(data.getExtras()。的containsKey(chosencolor))
    {
        ImageView的ImageView的=(ImageView的)currentRow.findViewById(R.id.bchangecolor);
        如果(data.getStringExtra(chosencolor)。contentEquals(蓝))
        {
            imageView.setImageResource(R.drawable.task_bg_left_blue);
            //menuItems.get(po)
        }
        否则,如果(data.getStringExtra(chosencolor)。contentEquals(绿色))
        {
            imageView.setImageResource(R.drawable.task_bg_left_green);
        }
        否则如果(data.getStringExtra(chosencolor)。contentEquals(黄色))
        {
            imageView.setImageResource(R.drawable.task_bg_left_yellow);
        }
        否则,如果(data.getStringExtra(chosencolor)。contentEquals(橙色))
        {
            imageView.setImageResource(R.drawable.task_bg_left_orange);
        }
        否则,如果(data.getStringExtra(chosencolor)。contentEquals(红))
        {
            imageView.setImageResource(R.drawable.task_bg_left_red);
        }
    }
}公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){
    Log.e(TAG,pressed该行本身+ v.getId());
    意向意图= createIntentwithExtra(V);
    startActivity(意向);
}公共意向createIntentwithExtra(视图V)
{
    意向意图=新意图(getApplicationContext(),SingleMenuItemActivity.class);
    字符串title =((的TextView)v.findViewById(R.id.text_title))的getText()的toString()。
    字符串日期=((的TextView)v.findViewById(R.id.text_date))的getText()的toString()。
    字符串时间=((的TextView)v.findViewById(R.id.text_time))的getText()的toString()。
    intent.putExtra(KEY_TITLE,职称);
    intent.putExtra(KEY_DATE,日期);
    intent.putExtra(KEY_TIME,时间);    返回意图;
}}


解决方案

您应该重新使用已经创建view.Some事情是这样的:

 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    ViewHolder支架=无效;
    如果(convertView == NULL){
        //创建新持有人
        持有人=新ViewHolder();
        //布局XML
        INT layoutFile = R.layout.adapter_layout;
        LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(layoutFile,NULL);
        convertView.setTag(保持器);
        holder.tvName =(TextView中)convertView.findViewById(R.id.tvName);              /// SET DE值的参考这里,其中包括
              /// ONCLICK到视图
    用户U = arrayUser.get(位置);    holder.tvName.setText(u.getName());
    }
    其他
        支架=(ViewHolder)convertView.getTag();
    返回convertView;
}静态类ViewHolder {
    TextView的tvName;
}

so the problem is like this: i have designed an application with a list-view in which i can change the colors of list items, here is a link to a screen from the application(i would be glad if some one could post it as an image)

http://i.stack.imgur.com/0Xd9d.jpg

the colors are changed by changing an image on the left part of the list row. now when i change the color slide down in the list, and the come back up the color go back to their original state (meaning the Image-view come back to it's original source).

The data for the list is taken from an XML file, parsed to a hash-map and the using an adapter inflated into the list.

does some one knows how i can go around this issue?

As I see it because the list-view reuses the views that are not on screen, when i come back to the changed row that went out of the screen and then comes back it puls its information again from the hash-map. So what i need to do is to find the index of data in the hash-map that was bind to row layout by the adapter, and there to change the associated color, that way the next time the adapter recreates this row it will be assigned with the right color. Some one has an idea how it's done?

Thanks in advance.

here is the java code for this activity:

public class SkyGiraffeAppActivity extends Activity implements AdapterView.OnItemClickListener, View.OnClickListener {
// All static variables
static final String TAG = "SkyGiraffeAppActivity";
// XML node keys
static final String KEY_TASK = "task"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_DATE = "date";
static final String KEY_TIME = "time";
static final String KEY_PRIORITY  = "priority";
static final String KEY_USERPRIORITY = "userpriority";

View currentRow;
final ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    ListView list = new ListView(this);
    list.setDivider(null);
    list.setDividerHeight(0);

    setContentView(list);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.taskstitle);


    Log.e(TAG, "Created Array");

    XMLParser parser = new XMLParser();

    Log.e(TAG, "Getting xml from raw folder");
    InputStream xml = getResources().openRawResource(R.raw.taskslists); // getting XML

    Log.e(TAG, "reading xml file to string");
    String sxml = parser.readTextFile(xml);

    Log.e(TAG, "creating dom element from string");
    Document doc = parser.getDomElement(sxml); // getting DOM element

    Log.e(TAG, "getting node list of all the elements");
    NodeList nodeList = doc.getElementsByTagName(KEY_TASK);

    Log.e(TAG, "looping through all the elements");
    for (int i = 0; i < nodeList.getLength(); i++) 
    {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nodeList.item(i);
        Log.e(TAG, "element number: " + i + " added to hash map");
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
        map.put(KEY_TIME, parser.getValue(e, KEY_TIME));
        map.put(KEY_PRIORITY, parser.getValue(e, KEY_PRIORITY));
        map.put(KEY_USERPRIORITY, parser.getValue(e, KEY_USERPRIORITY));

        // adding HashList to ArrayList
        menuItems.add(map);
    }

    // Adding menuItems to ListView
    ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.row,
          new String[] { KEY_TITLE, KEY_DATE, KEY_TIME, KEY_PRIORITY, KEY_USERPRIORITY}, 
          new int[]    { R.id.text_title, R.id.text_date, R.id.text_time ,R.id.image_priority, R.id.bchangecolor}){

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row =  super.getView(position, convertView, parent);
            View left = row.findViewById(R.id.bchangecolor);
            left.setTag(position);
            left.setOnClickListener(SkyGiraffeAppActivity.this);
            View right = row.findViewById(R.id.barrow);
            right.setTag(position);
            right.setOnClickListener(SkyGiraffeAppActivity.this);


            ImageView prioriy = (ImageView) row.findViewById(R.id.image_priority);
            ImageView usercolor = (ImageView) row.findViewById(R.id.bchangecolor);


            String userpriority = menuItems.get(position).get(KEY_USERPRIORITY);   
            Log.e(TAG, "user priority color for this view was: " + userpriority);

            if (userpriority.contentEquals("green"))
            {
                usercolor.setImageResource(R.drawable.task_bg_left_green);
            }
            else if (userpriority.contentEquals("blue"))
            {
                usercolor.setImageResource(R.drawable.task_bg_left_blue);
            }
            else if (userpriority.contentEquals("yellow"))
            {
                usercolor.setImageResource(R.drawable.task_bg_left_yellow);
            }
            else if (userpriority.contentEquals("orange"))
            {
                usercolor.setImageResource(R.drawable.task_bg_left_orange);
            }
            else if (userpriority.contentEquals("red"))
            {
                usercolor.setImageResource(R.drawable.task_bg_left_red);
            }


            int number = Integer.valueOf(menuItems.get(position).get(KEY_PRIORITY));
            switch ( number )
            {
            case 1:
                prioriy.setImageResource(R.drawable.priority_first);
                break;
            case 2:
                prioriy.setImageResource(R.drawable.priority_second);
                break;
            case 3:
                prioriy.setImageResource(R.drawable.priority_third);
                break;
            case 4:
                prioriy.setImageResource(R.drawable.priority_fourth);
                break;
            case 5:
                prioriy.setImageResource(R.drawable.priority_fifth);
                break;
            default:
                prioriy.setImageResource(R.drawable.priority_first);
            }

            return row;
        }
    };

    list.setAdapter(adapter);
    list.setOnItemClickListener(this);  
}

public void onClick(View v) 
{
     Intent i = new Intent(this, TaskColorChangeActivity.class);
     i.putExtra("color", "choosecolor");

     switch(v.getId()) {
        case R.id.bchangecolor:
            Log.e(TAG, "pressed the color change button on" + v.getId());
            currentRow = v;
            startActivityForResult(i, 1);
            break;
        case R.id.barrow:
            Log.e(TAG, "pressed the arrow button on" + v.getId());
            i = createIntentwithExtra(v.getRootView());
            startActivity(i);
            break;
        default:
            break;
        }   
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if(data.getExtras().containsKey("chosencolor"))
    {
        ImageView imageView = (ImageView) currentRow.findViewById(R.id.bchangecolor);
        if (data.getStringExtra("chosencolor").contentEquals("blue"))
        {
            imageView.setImageResource(R.drawable.task_bg_left_blue);
            //menuItems.get(po)
        }
        else if (data.getStringExtra("chosencolor").contentEquals("green"))
        {
            imageView.setImageResource(R.drawable.task_bg_left_green);
        }
        else if (data.getStringExtra("chosencolor").contentEquals("yellow"))
        {
            imageView.setImageResource(R.drawable.task_bg_left_yellow);
        }
        else if (data.getStringExtra("chosencolor").contentEquals("orange"))
        {
            imageView.setImageResource(R.drawable.task_bg_left_orange);
        }
        else if (data.getStringExtra("chosencolor").contentEquals("red"))
        {
            imageView.setImageResource(R.drawable.task_bg_left_red);    
        } 
    }
}

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    Log.e(TAG, "pressed the row itself" + v.getId());
    Intent intent = createIntentwithExtra(v);
    startActivity(intent);  
}

public Intent createIntentwithExtra(View v)
{
    Intent intent = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
    String title = ((TextView) v.findViewById(R.id.text_title)).getText().toString();
    String date = ((TextView) v.findViewById(R.id.text_date)).getText().toString();
    String time = ((TextView) v.findViewById(R.id.text_time)).getText().toString();
    intent.putExtra(KEY_TITLE, title);
    intent.putExtra(KEY_DATE, date);
    intent.putExtra(KEY_TIME, time);

    return intent;
}}

解决方案

You should re-use the already created view.Some thing like this:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if(convertView == null){
        //CREATE NEW HOLDER
        holder = new ViewHolder();
        // Layout XML
        int layoutFile = R.layout.adapter_layout;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(layoutFile, null);
        convertView.setTag(holder);
        holder.tvName= (TextView)convertView.findViewById(R.id.tvName);

              /// SET DE VALUES TO THE VIEW HERE, INCLUDING THE
              /// ONCLICK TO THE VIEW
    Users u = arrayUser.get(position);

    holder.tvName.setText(u.getName());


    }
    else
        holder= (ViewHolder)convertView.getTag();
    return convertView;
}

static class ViewHolder{
    TextView tvName;
}   

这篇关于如何保存更改的ImageView的状态列表视图行,它已经从屏幕上消失后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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