当我单击可扩展列表视图时?它显示最后一个列表视图而不是第一个列表?

时间:2015-03-06 11:54:43

标签: android expandablelistview

当我点击一个可展开的列表视图时,屏幕会移动到可展开列表视图中的最后一个列表视图。我想显示第一个列表视图并向下滚动以查看最后一个列表视图。

expandableList=(ExpandableListView) findViewById(R.id.existcustdet);
    expandableList.setDividerHeight(2);
    expandableList.setGroupIndicator(null);
    expandableList.setClickable(true);
    //expandableList.setBackgroundColor(Color.parseColor("#C6DFEF"));
    expandableList.setCacheColorHint(Color.parseColor("#00000000"));
    expandableList.setDivider(getResources().getDrawable(
            R.drawable.policy_expandable_row));


    <ExpandableListView
        android:id="@+id/existcustdet"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:groupIndicator="@null"
        android:transcriptMode="alwaysScroll" 
        android:/>




QuicksearchAdapter quickadapter = new QuicksearchAdapter(QuickQuote.this, parentItems,myPolicyList1,getpolorciv);
            expandableList.setAdapter(quickadapter);

QuicksearchAdapter.Java

package com.assuretech.ku.quickquote;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import com.assuretech.ku.R;
import com.assuretech.ku.WebserviceHelper;
import com.assuretech.ku.policies.Policy;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.TextView;

public class QuicksearchAdapter extends BaseExpandableListAdapter {



private ArrayList<ArrayList<Quote>> childtems;
private LayoutInflater inflater;
private ArrayList<String> parentItems;
private Activity mActivity;
String language;
SharedPreferences sharedPreference;
String formattedDate;
SimpleDateFormat dateFormat;
Date exp, currentDate;
String civilid;

// constructor
public QuicksearchAdapter(Activity activity, ArrayList<String> parents,
        ArrayList<ArrayList<Quote>> myPolicyList1,String cvid) {
    this.mActivity = activity;
    this.parentItems = parents;
    this.childtems = myPolicyList1;
    inflater = (LayoutInflater) mActivity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    sharedPreference = activity.getSharedPreferences(
            WebserviceHelper.PREFS_NAME, 0);
    language = sharedPreference.getString("language", "");
    civilid=cvid;
}


public QuicksearchAdapter(Activity activity, ArrayList<String> parents,
        ArrayList<ArrayList<Quote>> myPolicyList1) {
    this.mActivity = activity;
    this.parentItems = parents;
    this.childtems = myPolicyList1;
    inflater = (LayoutInflater) mActivity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    sharedPreference = activity.getSharedPreferences(
            WebserviceHelper.PREFS_NAME, 0);
    language = sharedPreference.getString("language", "");

}



// method getChildView is called automatically for each child view.
// Implement this method as per your requirement
@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    TextView txtPolicyNum = null;
    TextView txtchasisno = null;
    TextView txtchasis_ar = null;
    TextView txtVhlinfodetails= null;
    TextView txtar_chasis_Cap=null;
    TextView txtar_reg_no=null;
    TextView Expirydt = null;
    TextView Vehpltno = null;
    TextView Vehpla_Cap_ar=null;
    TextView Veh_details_ar=null;
    TextView txtassurdname_eng=null;
    TextView txtassurdname_arb=null;
    Button btnrepclaim;

    Calendar c = Calendar.getInstance();

    dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    formattedDate = dateFormat.format(c.getTime());

    if ((language.contentEquals("")) || (language.contentEquals("en"))) {
        convertView = inflater.inflate(R.layout.quickqote_existcustdet , null);
    }


    else if (language.contentEquals("ar"))

    {
        convertView = inflater.inflate(R.layout.quickqote_existcustdet_ar, null);
    }


    // get the textView reference and set the value

     Button btnreqforqot=(Button)convertView.findViewById(R.id.req_for_quot);
     TextView txtcvid=(TextView)convertView.findViewById(R.id.txtqq_ec_cvilid);
     TextView txtchno=(TextView)convertView.findViewById(R.id.txtqq_ec_chasno);
     TextView polnum=(TextView)convertView.findViewById(R.id.txtqq_ec_polno);
     TextView vehdtls=(TextView)convertView.findViewById(R.id.txtqq_ec_vehdet);
    // TextView vehdtls_ar=(TextView)convertView.findViewById(R.id.txtqq_ec_vehdet_ar);
     TextView expdt=(TextView)convertView.findViewById(R.id.txtqq_ec_expdate);
     TextView vhlplatno=(TextView)convertView.findViewById(R.id.txtqq_ec_platno);
     TextView assrdname=(TextView)convertView.findViewById(R.id.txtqq_ec_assurname);


     String chasino=childtems.get(groupPosition)
                .get(childPosition).Chasisno;
     String polno=childtems.get(groupPosition)
                .get(childPosition).policy_number;
     String vhdet=childtems.get(groupPosition)
                .get(childPosition).Pol_Veh_Dtls_Eng;
     String vhdet_ar=childtems.get(groupPosition)
                .get(childPosition).Pol_Veh_Dtls_Ar;

     String expdate=childtems.get(groupPosition)
                .get(childPosition).policy_exp_date;
     String vhplno=childtems.get(groupPosition)
                .get(childPosition).Vehicleplateno;
     String assurdname=childtems.get(groupPosition)
                .get(childPosition).assuredname_en;













    /*if (isRenewal.equalsIgnoreCase("N")) {
        btnRenew.setEnabled(false);
        btnRenew.setBackgroundDrawable(mActivity.getResources()
                .getDrawable(R.drawable.renew_button));
    }*/


    if ((language.contentEquals("")) || (language.contentEquals("en"))) {

         txtcvid.setText(civilid);
         txtcvid.setGravity(Gravity.LEFT);

         txtchno.setText(chasino);
         txtchno.setGravity(Gravity.LEFT);

         polnum.setText(polno);
         polnum.setGravity(Gravity.LEFT);

         vehdtls.setText(vhdet);
         vehdtls.setGravity(Gravity.LEFT);

         expdt.setText(expdate);
         expdt.setGravity(Gravity.LEFT);

         vhlplatno.setText(vhplno);
         vhlplatno.setGravity(Gravity.LEFT);

         assrdname.setText(assurdname);
         assrdname.setGravity(Gravity.LEFT);




    } 















    //commented 22


    else if (language.contentEquals("ar"))
    {


        txtcvid.setText(civilid);

          txtcvid.setGravity(Gravity.RIGHT);

         txtchno.setText(chasino);
         txtchno.setGravity(Gravity.RIGHT);

         polnum.setText(polno);
         polnum.setGravity(Gravity.RIGHT);

         vehdtls.setText(vhdet_ar);
         vehdtls.setGravity(Gravity.RIGHT);

         expdt.setText(expdate);
         expdt.setGravity(Gravity.RIGHT);

         vhlplatno.setText(vhplno);
         vhlplatno.setGravity(Gravity.RIGHT);

         assrdname.setText(assurdname);
         assrdname.setGravity(Gravity.RIGHT);    

    }






    //commented 22

    /*btnMoreInfo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent policyViewInte = new Intent(mActivity,
                    MyPolicyView.class);
            Bundle bundle = new Bundle();
            bundle.putString("policy_num", policynum);
            bundle.putString("expiry_date", expiry_date);
            bundle.putString("policyStatus_en", policyStatus_en);
            bundle.putString("assuredNameEn", assuredNameEn);
            bundle.putString("policy_status_ar", policy_status_ar);
            bundle.putString("assured_name_ar", assured_name_ar);
            bundle.putString("flag", "1");
            policyViewInte.putExtras(bundle);
            // mActivity.finish();
            mActivity.startActivity(policyViewInte);
        }
    });
    */

btnreqforqot.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
        //  Intent in =new Intent(context,MainWindow.class);
            //Toast.makeText(context, "Quotation Request sent successfully. Our representative will contact you soon", Toast.LENGTH_LONG).show();
            //context.startActivity(in);
        }
    });


    /*btnRenew.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intentRenew = new Intent(mActivity, PolicyRenew.class);
            Bundle bundle = new Bundle();
            bundle.putString("policy_num", policynum);
            // Toast.makeText(mActivity, "btnRenew policyId: " +policyId,
            // Toast.LENGTH_LONG).show();
            bundle.putString("policyId", policyId);
            bundle.putString("assuredNameEn", assuredNameEn);
            bundle.putString("total_premium", total_premium);
            bundle.putString("assured_name_ar", assured_name_ar);
            intentRenew.putExtras(bundle);
            mActivity.startActivity(intentRenew);

        }
    });*/

    // }

    return convertView;
}

// method getGroupView is called automatically for each parent item
// Implement this method as per your requirement
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    CheckedTextView checkedText = null;
    if ((language.contentEquals("")) || (language.contentEquals("en"))) {
        convertView = inflater.inflate(R.layout.policy_parent, null);

        checkedText = (CheckedTextView) convertView
                .findViewById(R.id.textViewGroupName);
        checkedText.setText("  " + parentItems.get(groupPosition));

        Drawable dwn_arr = mActivity.getResources().getDrawable(
                R.drawable.dwn_arr);

        Drawable rt_arr = mActivity.getResources().getDrawable(
                R.drawable.rt_arr);

        final DisplayMetrics metrics = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay()
                .getMetrics(metrics);
        switch (metrics.densityDpi) {
        case DisplayMetrics.DENSITY_LOW:
            dwn_arr.setBounds(0, 0, 30, 30);
            rt_arr.setBounds(0, 0, 30, 30);
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            if ((mActivity.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
                dwn_arr.setBounds(0, 0, 30, 30);
                rt_arr.setBounds(0, 0, 30, 30);

            } else if ((mActivity.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
                dwn_arr.setBounds(0, 0, 25, 25);
                rt_arr.setBounds(0, 0, 25, 25);

            } else if ((mActivity.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
                dwn_arr.setBounds(0, 0, 30, 30);
                rt_arr.setBounds(0, 0, 30, 30);
            } else if ((mActivity.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
                dwn_arr.setBounds(0, 0, 30, 30);
                rt_arr.setBounds(0, 0, 30, 30);

            } else {
                dwn_arr.setBounds(0, 0, 25, 25);
                rt_arr.setBounds(0, 0, 25, 25);
            }
            break;

        case DisplayMetrics.DENSITY_HIGH:
            dwn_arr.setBounds(0, 0, 30, 30);
            rt_arr.setBounds(0, 0, 30, 30);
            break;

        case DisplayMetrics.DENSITY_XHIGH:
            dwn_arr.setBounds(0, 0, 30, 30);
            rt_arr.setBounds(0, 0, 30, 30);
            break;
        default:
            dwn_arr.setBounds(0, 0, 30, 30);
            rt_arr.setBounds(0, 0, 30, 30);
            break;
        }

        if (isExpanded == true) {
            checkedText.setCompoundDrawables(dwn_arr, null, null, null);
        } else {
            checkedText.setCompoundDrawables(rt_arr, null, null, null);
        }
    } else if (language.contentEquals("ar")) {
        convertView = inflater.inflate(R.layout.policy_parent, null);

        checkedText = (CheckedTextView) convertView
                .findViewById(R.id.textViewGroupName);

        checkedText.setGravity(Gravity.RIGHT);
        checkedText.setText(parentItems.get(groupPosition) + " ");

        Drawable dwn_arr = mActivity.getResources().getDrawable(
                R.drawable.dwn_arr);
        dwn_arr.setBounds(0, 0, 30, 30);
        Drawable rt_arr = mActivity.getResources().getDrawable(
                R.drawable.rt_arr);
        rt_arr.setBounds(0, 0, 30, 30);

        if (isExpanded == true) {
            checkedText.setCompoundDrawables(null, null, dwn_arr, null);
        } else {
            checkedText.setCompoundDrawables(null, null, rt_arr, null);
        }
    }

    switch (groupPosition) {
    case 0:
        checkedText.setBackgroundResource(R.drawable.green_bar);

        break;
    case 1:
        checkedText.setBackgroundResource(R.drawable.blue_bar);

        break;
    case 2:
        checkedText.setBackgroundResource(R.drawable.brownbar);

        break;

    default:
        break;
    }

    return convertView;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return null;
}

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

@Override
public int getChildrenCount(int groupPosition) {
    return childtems.get(groupPosition).size();
}

@Override
public Object getGroup(int groupPosition) {
    return null;
}

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

@Override
public void onGroupCollapsed(int groupPosition) {
    super.onGroupCollapsed(groupPosition);
}

@Override
public void onGroupExpanded(int groupPosition) {
    super.onGroupExpanded(groupPosition);
}

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

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

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

}

1 个答案:

答案 0 :(得分:0)

我看到你的适配器里的东西我看不清楚。尝试修改你的适配器,更像是这样:

这部分是关于我的项目,所以你必须适应你的:

private Activity context;
    private Map<String, List<RosterEntry>> groupsCollections;
    private List<String> groups;

    public ExpandableListContactsAdapter(Activity conxtext, List<String> groups,
            Map<String, List<RosterEntry>> groupsCollections) {
        this.context = conxtext;
        this.groupsCollections = groupsCollections;
        this.groups = groups;
    }

从这一部分你需要检查方法。

你有一些方法返回null,或者0.尝试修复它

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

@Override
public int getChildrenCount(int groupPosition) {
    return groupsCollections.get(groups.get(groupPosition)).size();
}

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

@Override
public Object getChild(int groupPosition, int childPosition) {

    return groupsCollections.get(groups.get(groupPosition)).get(childPosition);
}

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

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

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

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

    String groupName = (String) getGroup(groupPosition);

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

    TextView item = (TextView) convertView.findViewById(R.id.user_Group);
    item.setTypeface(null, Typeface.BOLD);
    item.setText(groupName);

    return convertView;
}

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

    final RosterEntry contact = (RosterEntry) getChild(groupPosition, childPosition);

    LayoutInflater inflater = context.getLayoutInflater();

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.user_contacts_contact, null);
    }

    TextView item_name = (TextView) convertView.findViewById(R.id.expandable_user_name);
    TextView item_email = (TextView) convertView.findViewById(R.id.expandable_user_email);

    item_name.setText(contact.getName());
    item_email.setText(contact.getUser());
    return convertView;
}

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