yvi yvi -4 years ago 141
Android Question

Android: ListView selected item (onClick) stay selected after action

I have a problem with my ListView. If I click an item, this item is highlighted (this is ok) and another activity starts to edit details or to delete a dataset. After coming back to my list the item is still selected, even if it is no longer available...

<img src="https://i.stack.imgur.com/7Xi1z.png">
<img src="https://i.stack.imgur.com/JaqCP.png">
<img src="https://i.stack.imgur.com/p3OAU.png">
here should be some pictures that i cannot insert unfortunately because the editor thinks it is code ...


Only if I use the back-narrow (left of "Vertrag) the item is no longer highlighted. If I use another menu item the item stays highlighted. Also if I use the FloatingActionButton the item in my list stays highlighted.

How can I remove this highlighting? I already tried clearChoices() in different ways but it did not work.

Here is my code:

public class ContractList extends MainFrame {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.activity_list, frame);

ContractListFragment fragment = ContractListFragment.newInstance();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.replace(R.id.fragment, fragment)
.commit();

// Floating Action Button
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
}


The Fragment Class:

public class ContractListFragment extends ListFragment {

private View rootView;
private ContractListAdapter contractListAdapter;
private ListView listView = null;

public static ContractListFragment newInstance() {
ContractListFragment fragment = new ContractListFragment();
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_list, container, false);

contractListAdapter = new ContractListAdapter(this.getActivity(), R.layout.list);
setListAdapter(contractListAdapter);

return rootView;
}

@Override
public void onResume() {
super.onResume();
listView = (ListView) rootView.findViewById(android.R.id.list);
refreshView();
}

@Override
public void onListItemClick(ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);

Intent edit = new Intent(rootView.getContext(), Details.class);
//put some Intent extras here
startActivity(edit);
}

private void refreshView() {
Object[] rows = null;

contractListAdapter.clear();

// fill "rows" with data from table

contractListAdapter.add((ArrayList<String>) rows[1],
(ArrayList<String>) rows[2],
//...
);
contractListAdapter.notifyDataSetChanged();
}
}


My fragment_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ContractList">

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@color/colorListDivider"
android:dividerHeight="@dimen/divider_height"
android:listSelector="@color/colorListSelected"
android:cacheColorHint="@color/colorListcacheColorHint"
android:drawSelectorOnTop="false" />

<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/nodata"
android:visibility="invisible"
android:background="@color/colorListEmptyBackground" />

</RelativeLayout>


My list.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="@dimen/list_padding"
android:paddingBottom="@dimen/list_padding">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/col1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingEnd="25dp"/>

<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
app:srcCompat="@drawable/ic_notes"
android:tint="@color/colorListIconActive"/>

</RelativeLayout>

<TextView
android:id="@+id/col2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingEnd="25dp"/>

//...

</LinearLayout>


My ContractListAdapter.class

public class ContractListAdapter extends ArrayAdapter<String> {

private Activity mContext;
private int mViewId;
private int listSize;
private ArrayList<String> listDscr1;
private ArrayList<String> listDscr2;
//... some more lists

public ContractListAdapter(Activity context, int tvResId) {
super(context, tvResId);
mContext = context;
mViewId = tvResId;
listSize = 0;
}

public void add(ArrayList<String> dscr1,
ArrayList<String> dscr2,
//...
) {
listDscr1 = dscr1;
listDscr2 = dscr2;
//...

listSize = listDateS.size();
}

@Override
public int getCount() {
return listSize;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = mContext.getLayoutInflater();
v = vi.inflate(mViewId, null);
}

if (listSize > 0) {
TextView col1 = (TextView) v.findViewById(R.id.col1);
TextView col2 = (TextView) v.findViewById(R.id.col2);
//...

col1.setText(listDscr1.get(position));
col2.setText(listDscr2.get(position));
if (col2.getText().toString().trim().isEmpty()) {
col2.setVisibility(View.GONE);
} else {
col2.setVisibility(View.VISIBLE);
}
//... fill other fields in list.xml
}

return v;
}
}

Answer Source

Please remove this from your fragment_xml's listview: android:listSelector="@color/colorListSelected"

or use

android:listSelector="?attr/selectableItemBackground" instead

and if it does not work, you may like to use your own custom style.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download