Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to manage the state of Custom ListView Items
    primarykey
    data
    text
    <p>I am developing an application in which I have to move from one ListView to another ListView by clicking the first ListView.The 2nd Activities ListView is a custom ListView with one yes Icon Image and a TextView. The image icon is invisible in starting and after clicking on list item the corresponding image will become Visible.2nd ListView is single click listview.![enter image description here][1] When I click on the back button and again clicks on the First ListView,The Clicked item of 2nd ListView become invisible..</p> <p>I am new in android and don't know how I have to manage the state of the listitem clicking. Plz help... I have to move from 1st screen to 2nd screen and maintain the state of listview item when reclick is made on 1st listview item.</p> <p>2nd listview Activity</p> <pre><code>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&lt;?&gt; 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); } </code></pre> <p>}</p> <p><strong>Custom Adapter class:</strong></p> <pre><code>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; } } </code></pre> <p>Thanks....</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload