Change image icon of currently selected row item in listview android browser

In my ListView , I have TextView and Imageview on the same line. ListView row data comes from the local database. Now, when the user touches any row for selection, I want to change this image of the row row. Now, when the user closes this application and opens it again, I want to install a new image in Imageview the row that was last selected.

A second activity is also called when the user selects any row in the ListView . and when the user clicks the back button from this new action, the selected Imageview row Imageview not change, showing only the original image. I used the code below, but it does not work fine.

the onstart method works here:

 @Override public void onStart() { super.onStart(); int pos = prefs.getInt(PrefernceData.PREF_CURR_SELECTED_ID, -1); for(int i = 0; i < text.length; i++){ if(Integer.parseInt(text[i][0]) == pos){ dotPosition = i; } } 

listview itemclick method:

 public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) { dotPosition = position; clearSelection(); lastSelectedView = (ImageView)view.findViewById(R.id.doticon); ImageView v = (ImageView)view.findViewById(R.id.doticon); v.setImageResource(R.drawable.dothighlight); prefs.edit().putInt(PrefernceData.PREF_CURR_SELECTED_ID, Integer.parseInt(text[position][0]).commit(); Intent i = new Intent(this,SecondActivity.class); startActivity(i); 

Here is the clearselection method:

 public void clearSelection() { if(lastSelectedView != null) { Log.i("myLog","clear selection:lastselected icon::"); lastSelectedView.setImageResource(R.drawable.dot); } } 

and here is the getview method for the adapter:

 public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; LayoutInflater mInflater = LayoutInflater.from(myActivity.this); if(convertView == null) { convertView = mInflater.inflate(R.layout.site_listview_items, null); holder = new ViewHolder(); holder.siteText = (TextView)convertView.findViewById(R.id.homesitelisttext); holder.dotIcon = (ImageView)convertView.findViewById(R.id.doticon); convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } holder.siteText.setText(text[position][1]); Log.i("myLog","position::"+position+"dotPosition:"+dotPosition); if(position == dotPosition) { holder.dotIcon.setImageResource(R.drawable.dothighlight); lastSelectedView = holder.dotIcon; } else { holder.dotIcon.setImageResource(R.drawable.dot); } return convertView; } 
+4
source share

Source: https://habr.com/ru/post/1468943/


All Articles