如何管理自定义ListView项的状态

时间:2012-05-10 13:27:59

标签: android android-intent

我正在开发一个应用程序,我必须通过单击第一个ListView从一个ListView移动到另一个ListView。第二个活动ListView是一个自定义ListView,其中一个是图标图像和一个TextView。图像图标在开始时不可见,单击列表项后相应的图像将变为Visible.2nd ListView是单击列表视图。![在此处输入图像描述] [1] 当我点击后退按钮并再次点击第一个ListView时,第二个ListView的Clicked项变得不可见..

我是android新手,不知道如何管理listitem点击的状态。 Plz帮助...... 我必须从第一个屏幕移动到第二个屏幕,并在第一个listview项目上进行重新点击时保持listview项目的状态。

第二个listview活动

SharedPreferences pref;
 private static String names[] = {"SV3","SV4","SV6","LV1"};  
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.selectibx);
    lv=(ListView)findViewById(R.id.listView1);
    adapter = new LvCustomAdapter(this, names); 
    lv.setAdapter(adapter);
   LoadSelections();
    ImageView img=(ImageView) lv.findViewById(R.id.image);
  if(img!=null)
    {
        img.setImageResource(R.drawable.black_arrow);
        img.setVisibility(View.VISIBLE);
        }
 lv.setOnItemClickListener(this); 
    back=(ImageView)findViewById(R.id.imageback);
    back.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
Intent = new Intent(getApplicationContext(),MainActivity.class);
Bundle b=new Bundle();
b.putString("state", "clicked");
   intent.putExtras(b);
setResult(RESULT_OK, intent);
startActivity(intent);  
finish();
}

    });
}

public void onItemClick(AdapterView<?> lvv, View view, int position, long id) {



    if (current != -1) {
        View last = lvv.getChildAt(current); // the last one clicked
        last.findViewById(R.id.image).setVisibility(View.GONE);
     }
    view.findViewById(R.id.image).setVisibility(View.VISIBLE);
    current = position; // remember the new clicked position

    System.out.println("Selected State is: "+current);

    SaveSelections(view);

}

private void SaveSelections(View v) {
    // TODO Auto-generated method stub
     SharedPreferences settings = getSharedPreferences( PREFS_NAME,0 );
        SharedPreferences.Editor editor = settings.edit();

        editor.putInt("Raman", current);
        editor.commit();
}

private void LoadSelections() {


        pref = this.getApplicationContext().getSharedPreferences(PREFS_NAME, MODE_WORLD_READABLE);
        int xy=pref.getInt("Raman",1);
        adapter = new LvCustomAdapter(this, names);   
        lv.setAdapter(adapter);
          lv.setSelection(xy);

          System.out.println(lv.getSelectedItemPosition()+"****"+xy);

         View v=lv.getChildAt(xy);

    }

}

自定义适配器类:

public class LvCustomAdapter extends BaseAdapter  {
 protected ListView mListView;
   public String title[];  
   //public String designations[];
   public Activity context;  
    public LayoutInflater inflater; 
    int current=-1;
    public LvCustomAdapter(Activity context,String[] title) {  
        super();  

        this.context = context;  
        this.title = title;  
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    }  

    public int getCount() {  
        // TODO Auto-generated method stub  
        return title.length;  
    }  

    public Object getItem(int position) {  
        // TODO Auto-generated method stub  
        return null;  
    }  

    public long getItemId(int position) {  
        // TODO Auto-generated method stub  
        return 0;  
    }  

    public static class ViewHolder  
    {  
        ImageView imgarrow;  
        TextView txtViewName;  


    }  

    public View getView(int position, View convertView, ViewGroup parent) {  
        // TODO Auto-generated method stub  

        ViewHolder holder;  
        if(convertView==null)  
        {  
            holder = new ViewHolder();  
            convertView = inflater.inflate(R.layout.commonlist, null);  

            holder.txtViewName = (TextView) convertView.findViewById(R.id.tvItem);  


            holder.imgarrow = (ImageView) convertView.findViewById(R.id.image);  


           /* holder.email.setOnClickListener(emailClickListener);
            holder.mobile.setOnClickListener(mobileClickListener);
            holder.phone.setOnClickListener(phoneClickListener);
            convertView.setTag(holder);  */
            //int a=convertView.getSelectedItemPosition();
            int a=position;



            convertView.setTag(holder); 
        }  
        else  
            holder=(ViewHolder)convertView.getTag();  
   holder.txtViewName.setText(title[position]); 
    holder.imgarrow.setImageResource(R.drawable.black_arrow);
holder.imgarrow.setVisibility(View.INVISIBLE);

        return convertView;  
    }   
}  

...谢谢

1 个答案:

答案 0 :(得分:1)

为了维持您可以使用的状态..

Shared Preferences
Save your desired values in onPause() and get them again in onResume() 

希望它对你有用。