Java – Extensible ListView is not extended

Extensible ListView is not extended… here is a solution to the problem.

Extensible ListView is not extended

Hello, you need to prepare the screen like this

enter image description here

Here is the code for my extensible ListView

Adapter

class: NewAdapter.java

public class NewAdapter extends BaseExpandableListAdapter {

public ArrayList<ParentBean> groupItem;
    ArrayList<String> tempChild;
    public ArrayList<Object> Childtem = new ArrayList<Object>();
    public LayoutInflater minflater;
    public Activity activity;

public NewAdapter(ArrayList<ParentBean> grList, ArrayList<Object> childItem) {
        groupItem = grList;
        this. Childtem = childItem;
    }

public void setInflater(LayoutInflater mInflater, Activity act) {
        this.minflater = mInflater;
        activity = act;
    }

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

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

@Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        tempChild = (ArrayList<String>) Childtem.get(groupPosition);
        TextView text = null;
        if (convertView == null) {
            convertView = minflater.inflate(R.layout.childrow, null);
        }

text = (TextView) convertView.findViewById(R.id.textView1);
        text.setText(tempChild.get(0));
        text = (TextView) convertView.findViewById(R.id.textView2);
        text.setText(tempChild.get(1));
        text = (TextView) convertView.findViewById(R.id.textView3);
        text.setText(tempChild.get(2));
        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(activity, tempChild.get(childPosition),
                        Toast.LENGTH_SHORT).show();
            }
        });
        return convertView;
    }

@Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

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

@Override
    public int getGroupCount() {
        return groupItem.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 View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView == null) {
            convertView = minflater.inflate(R.layout.grouprow, null);
            holder = new ViewHolder();
            holder.name=(TextView)convertView.findViewById(R.id.playername);
            holder.team = (TextView) convertView.findViewById(R.id.vs);
            holder.salary = (TextView) convertView.findViewById(R.id.salary);
            convertView.setTag(holder);

} else {
            holder = (ViewHolder) convertView.getTag();
        }
        ParentBean playerdetails = groupItem.get(groupPosition);
        holder.name.setText(playerdetails.getPlayername());
        holder.team.setText(playerdetails.getPlayerteam());
        holder.salary.setText(playerdetails.getPlayersalary());
        return convertView;
    }

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

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

private class ViewHolder {
        TextView team;
        TextView name;
        TextView salary;
    }
    class ParentBean{
        String playername,playerteam,playersalary;

public ParentBean(String playername,String playerteam,String playersalary) {
            this.playername=playername;
            this.playersalary=playersalary;
            this.playerteam=playerteam;
        }

/**
         * @return the playername
         */
        public String getPlayername() {
            return playername;
        }

/**
         * @param playername the playername to set
         */
        public void setPlayername(String playername) {
            this.playername = playername;
        }

/**
         * @return the playerteam
         */
        public String getPlayerteam() {
            return playerteam;
        }

/**
         * @param playerteam the playerteam to set
         */
        public void setPlayerteam(String playerteam) {
            this.playerteam = playerteam;
        }

/**
         * @return the playersalary
         */
        public String getPlayersalary() {
            return playersalary;
        }

/**
         * @param playersalary the playersalary to set
         */
        public void setPlayersalary(String playersalary) {
            this.playersalary = playersalary;
        }

}
}

This is .java my activity: MainActivity

public class MainActivity extends ExpandableListActivity implements
        OnChildClickListener {
    Context context;
    ArrayList<ParentBean> groupItem = new ArrayList<ParentBean>();
    ArrayList<Object> childItem = new ArrayList<Object>();
    NewAdapter mNewAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ExpandableListView expandbleLis = getExpandableListView();
        expandbleLis.setDividerHeight(2);
        expandbleLis.setGroupIndicator(null);
        expandbleLis.setClickable(true);
        expandbleLis.setFocusable(true);
        context=this;
        getData();
        mNewAdapter = new NewAdapter(groupItem, childItem);
        mNewAdapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),this);
        getExpandableListView().setAdapter(mNewAdapter);

expandbleLis.setOnChildClickListener(this);
    }

public void getData() {
        JsonDataCallback callback=new JsonDataCallback(MainActivity.this) {

@Override
            public void receiveData(Object object) {
                String jsonData=(String)object;
                setData(jsonData);
            }
        };
        callback.execute("http://111.93.7.119:8080/FirstPickService/Players?sportid=fc2e88e6-ac87-4d27-b6b3-863baa9f06ec",null,null);
    }

protected void setData(String jsonData) {
        try {
            JSONObject players=new JSONObject(jsonData);
            final JSONArray playersArray=players.getJSONArray("players");
            for(int i=0; i<playersArray.length(); i++)
            {
                JSONObject jsonObj=playersArray.getJSONObject(i);
                String teamplayer=jsonObj.getString("playerfullname");
                String psalary=jsonObj.getString("playerprice");
                String strTeamVS=jsonObj.getString("teamid")+"@"+jsonObj.getString("awayteam");
                groupItem.add(mNewAdapter.new ParentBean(teamplayer, psalary, strTeamVS));

String strsalary = jsonObj.getString("playerpricelong");
                ArrayList<String> child = new ArrayList<String>();
                child.add(psalary);
                child.add(strTeamVS);
                child.add(strsalary);
                childItem.add(child);
            }
            mNewAdapter.notifyDataSetChanged();

} catch (Exception e) {
            e.printStackTrace();
        }
    }

@Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        Toast.makeText(MainActivity.this, "Clicked On Child",
                Toast.LENGTH_SHORT).show();
        return true;
    }
}

I’m not sure, but I used a button to select because the expandable list loses focus and doesn’t show subrows when clicking on a list item….

Solution

Try making the selection button unfocusable in XML.

Related Problems and Solutions