• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Cache Memory and Expandable Listview Issue

I am new to android and I need some guidance.
I have two issues in my app - a) While using the application if I remove the app from the cache the data is not visible any more. We have to log out and again login to the app to get the data. I think there is an issue in SharedPreferenceClass.

b)In "Pending Approval" tag in "To Do List" I want to add an Expandable Listview. Though I have already added the listview. But nothing is going to visible in my activity. It will look like the picture below
BOVuo.png



a)Cache Memory Issue:

Code:
public class SharedPreferencesClass {


    private SharedPreferences sp;

    public boolean read(Context context)
    {
        Boolean value;
        sp = context.getSharedPreferences("MyPref", 0);
        value=sp.getBoolean("MyPref", false);
        return value;
    }
    public void write(Context context,Boolean val)
    {
        sp=context.getSharedPreferences("MyPref",0);
        SharedPreferences.Editor editor=sp.edit();
        editor.clear();
        editor.putBoolean("MyPref",val);
        editor.commit();
    }
    public void clear(Context context)
    {
        sp=context.getSharedPreferences("MyPref",0);
        SharedPreferences.Editor editor=sp.edit();
        editor.clear();
        editor.commit();
    }
}

Logout:

Code:
new SharedPreferencesClass().clear(ToDoListActivity.this);
Intent i=new Intent(ToDoListActivity.this,LoginActivity.class);
startActivity(i);
finish();

Login:

Code:
new SharedPreferencesClass().write(LoginActivity.this,true);

b)Expandable List View

pending_expandable_heading.xml

Code:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/lblListHeader"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textSize="20dp"
        android:text="Heading"
        android:layout_margin="10dp"/>

</LinearLayout>

pending_expandable_listview.xml

Code:
<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    xmlns:android="http://schemas.android.com/apk/res/android">
    <ExpandableListView
        android:id="@+id/lvExp"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>


activity_lead_details.xml

Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        card_view:cardUseCompatPadding="true"
        card_view:cardElevation="5dp"
        card_view:cardCornerRadius="15dp">

        <LinearLayout
            android:id="@+id/layout_details"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:padding="15dp"
            android:layout_height="wrap_content">

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

                <TextView
                    android:id="@+id/txt_date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Date"
                    android:textSize="16sp"
                    android:padding="5dp"/>

                <TextView
                    android:id="@+id/txt_loc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Location"
                    android:textSize="16sp"
                    android:padding="5dp"
                    android:layout_toRightOf="@+id/txt_date"
                    android:layout_marginLeft="10dp"/>
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <Button
                    android:id="@+id/noteBtn"
                    android:layout_height="40dp"
                    android:layout_width="40dp"
                    android:padding="10dp"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentRight="true"
                    android:onClick="clickNote"
                    android:background="@drawable/note"/>
                <TextView
                    android:layout_toLeftOf="@+id/noteBtn"
                    android:id="@+id/txtOne"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="20sp"
                    android:text="Home"
                    android:padding="5dp"/>
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <Button
                    android:id="@+id/call_btn"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:background="@drawable/call"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp"/>
                <TextView
                    android:id="@+id/txt_contact"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/call_btn"
                    android:textSize="16sp"
                    android:text="Contact"
                    android:padding="5dp"
                    android:layout_marginLeft="10dp"
                    android:background="@android:color/transparent"/>

            </RelativeLayout>

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

                <Button
                    android:id="@+id/mail_btn"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:background="@drawable/mail"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp"/>
                <TextView
                    android:id="@+id/txt_mail"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/mail_btn"
                    android:textSize="16sp"
                    android:text="Mail"
                    android:padding="5dp"
                    android:layout_marginLeft="10dp"
                    android:background="@android:color/transparent"/>
            </RelativeLayout>

            <TextView
                android:id="@+id/txtTwo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:text="Home"
                android:padding="5dp"/>

            <TextView
                android:id="@+id/txtThree"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:text="Home"
                android:padding="5dp"/>

            <TextView
                android:id="@+id/txtFour"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:text="Home"
                android:padding="5dp"/>
            <TextView
                android:id="@+id/txtFive"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:text="Home"
                android:padding="5dp"/>
            <TextView
                android:id="@+id/txtSix"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:text="Home"
                android:padding="5dp"/>
        </LinearLayout>

    </android.support.v7.widget.CardView>
    <RelativeLayout
        android:padding="20dp"
        android:layout_below="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <Button
        android:id="@+id/continueBtn"
        android:text="@string/continue_btn"
        android:padding="10dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_width="100dp"
        android:textColor="#fff"
        android:background="@drawable/lft"
        android:textStyle="bold"
        android:textSize="12sp"
        android:onClick="clickContinue"
        android:layout_height="50dp" />

    <Button
        android:id="@+id/yesBtn"
        android:text="@string/yes_btn"
        android:padding="10dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_width="100dp"
        android:textColor="#fff"
        android:background="@drawable/rit"
        android:textStyle="bold"
        android:textSize="12sp"
        android:onClick="clickYes"
        android:layout_height="50dp" />
    <Button
        android:id="@+id/reassignBtn"
        android:text="@string/reassign_btn"
        android:padding="10dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_width="100dp"
        android:textColor="#fff"
        android:background="@drawable/mid"
        android:textStyle="bold"
        android:textSize="12sp"
        android:onClick="clickReassign"
        android:layout_height="50dp" />

</RelativeLayout>
</RelativeLayout>

ExpandableListAdapter.java:

Code:
public class ExpandableListAdapter extends BaseExpandableListAdapter implements NetworkCallback {

private HashMap<String, List<LeadBO>> _listDataChild;
private List<PendingBO> itemList;
private Context context;
private ViewHolderItem viewHolder = null;
private ViewHolderItem viewHolder1 = null;

public  String empName = "";
public String empId = "";
List<EmpolyeBO> memberList;

public ExpandableListAdapter(Context context, List<PendingBO> itemList,
                             HashMap<String, List<LeadBO>> listChildData) {
    this.context = context;
    this.itemList = itemList;
    this._listDataChild = listChildData;
    callMemberList();
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this.itemList.get(groupPosition)).get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public void updateScreen(String data, String tag) {
    if(tag.compareTo(ApiUtil.TAG_MEMBER_LIST)==0){
        memberList = MemberListParsar.parseEmplyoee(data);
    }
    if(tag.compareTo(ApiUtil.TAG_DEAD_LIST_PENDING)==0){
        Utility.ShowLongNotification(context, ErrorUtil.DEAD_LEAD_MESSAGE);
        ((Activity) context).finish();
    }
    if(tag.compareTo(ApiUtil.TAG_ADD_LEAD_EMP)==0){
        Utility.ShowLongNotification(context, ErrorUtil.REASSIGNED_MSG);
        ((Activity) context).finish();
    }
}

static class ViewHolderItem {

    public TextView txtLoc;
    public TextView txtDate;
    public TextView txtOne;
    public TextView tv_contact;
    public TextView tv_mail;
    public TextView txtTwo;
    public TextView txtThree;
    public TextView txtFour;
    public TextView txtFive;
    public TextView txtSix;

    public CardView cardView;

    public TextView lblListHeader;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final LeadBO leadBO = (LeadBO) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.activity_lead_details, null);

        viewHolder = new ViewHolderItem();
        viewHolder.txtLoc = (TextView) convertView.findViewById(R.id.txt_loc);
        viewHolder.txtDate = (TextView) convertView.findViewById(R.id.txt_date);
        viewHolder.txtOne = (TextView) convertView.findViewById(R.id.txtOne);
        viewHolder.tv_contact = (TextView) convertView.findViewById(R.id.txt_contact);
        viewHolder.tv_mail = (TextView) convertView.findViewById(R.id.txt_mail);
        viewHolder.txtTwo = (TextView) convertView.findViewById(R.id.txtTwo);
        viewHolder.txtThree = (TextView) convertView.findViewById(R.id.txtThree);
        viewHolder.txtFour = (TextView) convertView.findViewById(R.id.txtFour);
        viewHolder.txtFive = (TextView) convertView.findViewById(R.id.txtFive);
        viewHolder.txtSix = (TextView) convertView.findViewById(R.id.txtSix);
        viewHolder.cardView = (CardView) convertView.findViewById(R.id.card_view);

        convertView.setTag(viewHolder);
    }else{
        viewHolder = (ViewHolderItem) convertView.getTag();
    }
    decComponent();
    return convertView;
}

private void decComponent() {
    PendingBO pendingBO = AppController.getInstance().pendingBO;

    LeadBO lead= pendingBO.getLead();
    viewHolder.txtLoc.setText(lead.getLocation());
    viewHolder.txtDate.setText(lead.getDate());
    viewHolder.txtOne.setText(lead.getName());
    viewHolder.tv_contact.setText(lead.getPhNo());
    viewHolder.tv_mail.setText(lead.getEmail());
    viewHolder.txtTwo.setText(lead.getProject()+" : "+lead.getArea());
    viewHolder.txtThree.setText("Budget"+" : "+lead.getBudget());
    viewHolder.txtFour.setText( lead.getSource());

    viewHolder.txtFive.setText("Reason :"+pendingBO.getReason());
    viewHolder.txtSix.setText("BDE Name : "+pendingBO.getEmpName());

    viewHolder.tv_contact.setPaintFlags(viewHolder.tv_contact.getPaintFlags() |   Paint.UNDERLINE_TEXT_FLAG);
    viewHolder.tv_mail.setPaintFlags(viewHolder.tv_mail.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

    viewHolder.tv_contact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String number = viewHolder.tv_contact.getText().toString().trim();
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:" + number));
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            context.startActivity(callIntent);
        }
    });

    viewHolder.tv_mail.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String mail=viewHolder.tv_mail.getText().toString().trim();
            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("message/rfc822");
            //  i.putExtra(Intent.EXTRA_EMAIL, new String[]{String.valueOf(Uri.parse(mail))});
            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
            i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
            i.putExtra(Intent.EXTRA_TEXT   , "body of email");
            try {
                context.startActivity(Intent.createChooser(i, "Send mail..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(context, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this.itemList.get(groupPosition).getEmpName()).size();
}

@Override
public Object getGroup(int groupPosition) {
    return this.itemList.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this.itemList.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    PendingBO pendingBO = (PendingBO) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.pending_approval_expandable_heading, null);
        viewHolder1 = new ViewHolderItem();
        viewHolder1.lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);

        convertView.setTag(viewHolder1);
    }else{
        viewHolder1 = (ViewHolderItem) convertView.getTag();
    }

    viewHolder1.lblListHeader.setTypeface(null, Typeface.BOLD);
    viewHolder1.lblListHeader.setText(pendingBO.getEmpName());

    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

public void clickContinue(View v){
    empName = AppController.getInstance().pendingBO.getEmpName();
    callAddLeadEmp(AppController.getInstance().pendingBO.getEmpID());
}

public void clickYes(View v){
    callDeadLeadApproval();
}
public void clickReassign(View v){
    ShowEmpList();
}
public void ShowEmpList(){
    String[] empArr = new String[memberList.size()];
    for(int i=0;i<memberList.size();i++){
        empArr[i]=memberList.get(i).getName();
    }

    new MaterialDialog.Builder(context)
            .title("Select BDE Name")
            .items(empArr)
            .alwaysCallSingleChoiceCallback()
            .itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() {
                @Override
                public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                    empId = memberList.get(which).getId();
                    empName =  text.toString();
                    Log.e("Emp Name",empName);
                    return true;
                }
            })
            .positiveText("Choose & Submit")

            .onPositive(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    if(empId.length()<=0){
                        Utility.ShowLongNotification(context, ErrorUtil.MEMBER_SELECTION_BLANK);
                        return;
                    }
                    callAddLeadEmp(empId);
                    dialog.dismiss();
                }
            })
            .show();

}

public void clickNote(View v){
    Intent intent = new Intent(context, NoteActivity.class);

    AppController ctrl = AppController.getInstance();
    ctrl.leadId = ctrl.pendingBO.getLead().getId();
    ctrl.noteList = ctrl.pendingBO.getLead().getNoteBOList();
    context.startActivity(intent);
}

public void callMemberList(){
    AppController ctrl = AppController.getInstance();
    MemberListAPI api = new MemberListAPI(context,this);
    api.processMemberList(ctrl.mEmpId);
}

public void callDeadLeadApproval(){
    AppController ctrl = AppController.getInstance();
    DeadLeadPendingAPI api = new DeadLeadPendingAPI(context,this);
    api.processDeadLead(""+ctrl.pendingBO.getLead().getId());
}
public void callAddLeadEmp(String empID){
    AppController ctrl = AppController.getInstance();
    AddLeadEmpAPI api = new AddLeadEmpAPI(context,this);
    api.processAddLeadEmp(empID, LMSUtil.PRE_VISIT,""+ctrl.pendingBO.getLead().getId());

}
}

PendingExpandableActivity.java:

Code:
public class PendingExpandableActivity extends Activity implements NetworkCallback{

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
AppController delegate;
private List<PendingBO> listDataHeader;
private HashMap<String, List<LeadBO>> listDataChild;
private static Context context;
private GridLayoutManager lLayout;
private SwipeRefreshLayout mSwipeRefreshLayout;
private Button homeBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.fragment_pending_expandablelistview);

    mSwipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipeRefreshLayout);
    homeBtn= (Button) findViewById(R.id.homeBtn);

    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            // Refresh items
            callDeadLead();
        }
    });

    // get the listview
    expListView = (ExpandableListView) findViewById(R.id.lvExp);

    listDataHeader = new ArrayList<PendingBO>();
    listDataChild = new HashMap<String, List<LeadBO>>();

    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
    expListView.setAdapter(listAdapter);

    expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {
            PendingBO pendingBO = listDataHeader.get(groupPosition);
            if (pendingBO.getStatus() == 0) {
                pendingBO.setStatus(1);
            } else {
                pendingBO.setStatus(0);
            }
            listAdapter.notifyDataSetChanged();
            return false;
        }
    });

    // Listview Group expanded listener
    expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
        }
    });

    // Listview Group collasped listener
    expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
        @Override
        public void onGroupCollapse(int groupPosition) {

        }
    });

    // Listview on child click listener
    expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
            return false;
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    context=PendingExpandableActivity.this;
    callDeadLead();
}

private void callDeadLead() {
    AppController ctrl = AppController.getInstance();
    DeadListAPI api = new DeadListAPI(this,this);
    api.processDeadLead(ctrl.mEmpId);
}

@Override
protected void onPause() {
    super.onPause();
    Log.e("activity","onPause()");
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        back();
        finish();
    }
    return super.onKeyDown(keyCode, event);
}

private void back() {
    Intent i=new Intent(PendingExpandableActivity.this,ToDoListActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(i);
}

@Override
public void updateScreen(String data, String tag) {
    if(tag.compareTo(ApiUtil.TAG_DEAD_LEAD_API)==0){
        listDataHeader = PendingListParsar.parsePendingBo(data);
    //    makePendingApprovals();
        mSwipeRefreshLayout.setRefreshing(false);
    }
}

public void makePendingApprovals(){
    lLayout = new GridLayoutManager(this,1);

    RecyclerView rView = (RecyclerView)findViewById(R.id.cardList);
    rView.setHasFixedSize(true);
    rView.setLayoutManager(lLayout);
    rView.setItemAnimator(new DefaultItemAnimator());

    PendingViewAdapter rcAdapter = new PendingViewAdapter(this, listDataHeader);
    rView.setAdapter(rcAdapter);
}
}

 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones