Android : onitemclick disappears when back button is pressed

on Sunday, August 10, 2014


I have a listview which is in single choice mode. When I click on the item in the listview a button is displayed on the listview position. This button disappears when I press back button or Ok button. I want it to be saved until the next onitemclick.


I have included only the oncreate, getview and onitemclick methods in adapter


public class ExpiredActivity extends Activity implements View.OnClickListener { private final static String TAG = "ExpiredActivity";



private ListView mExpiredListView;
private Button mButtonDone;
private TextView mTextSummary;
private ExpiredListAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_expiry);
getActionBar().hide();
mExpiredListView = (ListView) findViewById(R.id.system_list);
mButtonDone = (Button) findViewById(R.id.button_done);
mTextSummary = (TextView) findViewById(R.id.text_summary);
mButtonDone.setOnClickListener(this);
mAdapter = new ExpiredListAdapter(getApplicationContext());
mExpiredListView.setAdapter(mAdapter);
mExpiredListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mExpiredListView.setOnItemClickListener(mAdapter);

private class ExpiredListAdapter extends BaseAdapter implements OnItemClickListener {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
final LayoutInflater inflater =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.system_list_item, parent, false);
}

LogonResponseData.SevInfo item = (LogonResponseData.SevInfo) getItem(position);

TextView txtName = (TextView) convertView.findViewById(R.id.text_name);
TextView txtType = (TextView) convertView.findViewById(R.id.text_type);

txtName.setText("Days");
txtType.setText("- " + item.value);
showMointorState(convertView, position);

return convertView;
}

private void showMointorState(View view, int position) {
CheckBox chk = (CheckBox) view.findViewById(R.id.chk_flag);
if(mExpiredListView.isItemChecked(position))
{
chk.setChecked(true);
}
else {
chk.setChecked(false);
}
chk.invalidate();
}

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

mExpiredListView.setItemChecked(position, true);
showMointorState(view, position);
}
}


Below is my xml file


activity_choose_expiry



<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="45dip"
android:layout_alignParentTop="true"
android:layout_marginBottom="10sp"
>
<Button
android:id="@+id/button_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/done"
android:layout_alignParentRight="true"
android:background="?light_blue"
android:textColor="@android:color/white"
android:textSize="18sp"
/>
<TextView
android:id="@+id/text_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/button_done"
android:text="@string/choose_expiry"
android:textSize="18sp"
android:textColor="?default_text"
/>
</RelativeLayout>

<TextView
android:id="@+id/text_summary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="?light_blue"
android:layout_alignParentBottom="true"
android:gravity="center"
/>

<ListView
android:id="@+id/system_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="6dp"
android:divider="#00000000"
android:layout_above="@id/text_summary"
android:layout_below="@id/header"
/>

</RelativeLayout>


system_list_item



<CheckBox
android:id="@+id/chk_flag"
android:layout_width="24sp"
android:layout_height="24sp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:button="?ic_tag_blue"
android:clickable="false"
android:focusable="false"
/>

<LinearLayout
android:id="@+id/left_up"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@id/chk_flag"
>

<TextView
android:id="@+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10sp"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="?default_text"
/>

<TextView
android:id="@+id/text_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="?default_text"
/>
</LinearLayout>

<TextView
android:id="@+id/text_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@id/chk_flag"
android:layout_below="@id/left_up"
android:textColor="?default_text"
/>

</RelativeLayout>

0 comments:

Post a Comment