java - 可扩展 ListView 未扩展

标签 java android expandablelistview

你好需要这样准备屏幕

enter image description here

这是我的可扩展 ListView 的代码

适配器类: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;
        }

    }
}

这是我的 Activity :MainActivity.java

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;
    }
}

我不确定,但我使用了一个按钮进行选择,因为当单击列表项时可扩展列表失去焦点并且不显示子行...

最佳答案

尝试在 xml 中使选择按钮不可聚焦

关于java - 可扩展 ListView 未扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14562332/

相关文章:

java - 将 int 的位变成 long

java - 使用 Java 读取和处理带有自定义列和行分隔符的文本文件

android - 如何在 flutter 中获取 android 绘图

android - Android声音池循环不起作用:替代

java - jitsi-android 无法编译构建

android - 可扩展 ListView android中的按钮

android - 我需要在我的 ExpandableListView 中延迟我的组的扩展

java - 服务程序 : Cannot instantiate javaURLContextFactory only if load-on-startup

java - 如何将字节数组转换为 double 并返回?

android - 自定义 View 未使用 ExpandableListView 上的 OnGroupClickListener 进行更新