【问题标题】:ExpandableListView getChildAt() return nullExpandableListView getChildAt() 返回 null
【发布时间】:2014-10-02 00:01:58
【问题描述】:

我想将图像添加到 ExpandableList 中的某些单元格中我已经在搜索我的问题的答案并尝试放置

expListView.getLastVisiblePosition();
expListView.getFirstVisiblePosition();

这是行不通的。我不能在适配器的 getView() 中执行此操作,因为我通过从数据库中下载数据来动态创建单元格,然后我可以说哪个单元格需要图像。

我也尝试过: Sometimes listView.getChildAt(int index) returns NULL (Android) Nullpointerexception on getChildAt GridView getChildAt() returns null 没有解决办法。 这是我的代码:

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String,String> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, String> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

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

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

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

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

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

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.spinner, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(Controller.getInstance().getTypeface());
        lblListHeader.setText(headerTitle);

        //I can' do this in this place
        //ImageView iv = (ImageView)convertView.findViewById(R.id.exc);
        //iv.setImageResource(R.drawable.excl);

        return convertView;
    }

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

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

和活动:

public class ExpertZone extends Activity{
    String err;
    Context context;
    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, String> listDataChild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expert_zone);

        context=this;

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

        // preparing list data
        prepareListData();

    }

    /*
     * Preparing the list data
     */
    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, String>();

        DBexpertTask dbTask = new DBexpertTask(getString(R.string.expertServlet)) {
            @Override
            protected void onPostExecute(JSONObject obj) {
                JSONArray jArray = new JSONArray();
                try {
                    JSONArray jsonObject = obj.getJSONArray(getString(R.string.data));
                    for (int i = 0; i < jsonObject.length(); i++) {
                        JSONObject object = jsonObject.getJSONObject(i);
                        err = object.getString(getString(R.string.err));
                        if(err.equals(getString(R.string.no))) {
                            jArray = object.getJSONArray(getString(R.string.data));
                            for (int j = 0; j < jArray.length(); j++) {
                                JSONObject values = jArray.getJSONObject(j);
                                listDataHeader.add(String.valueOf(values.get(getString(R.string.who)).toString()));
                                listDataChild.put(String.valueOf(values.get(getString(R.string.who)).toString()), String.valueOf(values.get(getString(R.string.what)).toString()));
                            }
                        }

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                listAdapter = new ExpandableListAdapter(context, listDataHeader, listDataChild);

                // setting list adapter
                expListView.setAdapter(listAdapter);

               for(int j=0; j<expListView.getCount(); j++){
                    expListView.getLastVisiblePosition();
                    expListView.getFirstVisiblePosition();
                    View v = expListView.getChildAt(j).findViewById(R.id.expListView);
                    ImageView iv = (ImageView) v.findViewById(R.id.exc);
                    iv.setImageResource(R.drawable.excl);
                }

            }
        };
        dbTask.execute();
    }
}

我的 xml - 单元格布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="6">

    <TextView
        android:id="@+id/lblListHeader"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:gravity="center_vertical|center_horizontal"
        android:layout_weight="5"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:textSize="18dp"  />
    <ImageView
        android:id="@+id/exc"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        android:layout_gravity="center_vertical" />
</LinearLayout>

和expert_zone.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_alignParentBottom="true"
    >

    <ExpandableListView
        android:id="@+id/expListView"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>
</LinearLayout>

如何动态更改此适配器中的图像图标? 非常感谢您的建议。

【问题讨论】:

  • 为什么会有这个:View v = expListView.getChildAt(j).findViewById(R.id.expListView);?对我来说这没有任何意义。您将在i 索引处获取孩子。然后,您试图在该子项中找到 ID 等于您的 ExpandableList 的视图?!是这样吗?
  • 对不起,这是 stackoverflow 的答案之一。我忘记删除这部分代码。没有 .findViewById() 它也不起作用。

标签: android android-layout expandablelistadapter


【解决方案1】:

如果子当前不可见,则视图可能为空,因为它已被回收,如果您需要访问数据,您应该获取适配器并从中获取对象。请检查这个答案:

Android: Access child views from a ListView

【讨论】:

    【解决方案2】:

    尝试改变这一行:

    View v = expListView.getChildAt(j).findViewById(R.id.expListView);
    

    到这个:

    View v = expListView.getChildAt(j);
    

    【讨论】:

    • 对不起,这是 stackoverflow 的答案之一。我忘记删除这部分代码。没有 .findViewById() 它也不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    相关资源
    最近更新 更多