如何在android中展开和折叠listview?

时间:2013-06-11 12:59:04

标签: android android-listview android-ui

我已在我的应用中实现了代码,用于展开和折叠此链接的列表视图https://github.com/tjerkw/Android-SlideExpandableListView但是给我一个例外

Java.lang.NullPointerException
>  E/AndroidRuntime(826):   at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:353)
 E/AndroidRuntime(826):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
 E/AndroidRuntime(826):     at  com.ojaswitech.bookingscape.library.ActionSlideExpandableListView$1.getView(ActionSlideExpandableListView.java:39)
 E/AndroidRuntime(826):     at com.ojaswitech.bookingscape.library.AbstractSlideExpandableListAdapter.getView(AbstractSlideExpandableListAdapter.java:54)

我尝试了很多解决方案,但没有实现我的目标。我附上我的代码。请有人有解决方案。如果有人有另一种方法来完成这项任务,请告诉我。

以下是可扩展listview的代码

public class DetailActivity extends SherlockActivity {

private String URL = "http://10.0.2.2:80/project";  
private String[] bungLowList;
private static final String BANGLOW_NAME = "banglow_name";

private  ListAdapter bunglowAdapter;

ActionSlideExpandableListView listSlide;
private ListView listVw;
String unitStatus;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bungalows);

    // get listview id in ActionSlideExpandableListView
    listSlide  = (ActionSlideExpandableListView)this.findViewById(R.id.bangDetlist);

    .....       
    dataAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
            R.array.booking_status,android.R.layout.simple_spinner_item);

    dataAdapter.setDropDownViewResource(
            android.R.layout.simple_spinner_dropdown_item);
    availSpnr.setAdapter(dataAdapter);

    availSpnr.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View arg1,
                int pos, long arg3) {
            lnrLayForTxtNoRec.setVisibility(View.GONE);
            unitStatus = parent.getItemAtPosition(pos).toString();

            new LoadDataFrmSrvr().execute();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }

    });

    //this listener for listview to expand toolbar
    listSlide.setItemActionListener(new ActionSlideExpandableListView.OnActionClickListener() {

        @Override
        public void onClick(View listView, View buttonview, int position) {

            String actionName = "";
            if(buttonview.getId() == R.id.bangDetbuttonA) {
                actionName = "buttonA";
            } else {
                actionName = "ButtonB";
            }

            Toast.makeText(
                DetailActivity.this,
                "Clicked Action: "+actionName+" in list item "+position,
                Toast.LENGTH_LONG
            ).show();
        }

    // this is needed in order for the listview to discover the buttons
    }, R.id.bangDetbuttonA, R.id.bangDetbuttonB);
}
private class LoadDataFrmSrvr extends AsyncTask<String,Void, Void> {

    protected Void doInBackground(String... params) {
        Log.v("In Bunglows", "Background");


        String jsonpBuildingList = dataFromSrvr.makeHttpReqToSrvr(URL, "POST",
                projBuildingDetail);

        .......................... 
        try {
            JSONArray jsonArray = new JSONArray(jsonpBuildingList);
            bungLowList  = new String[jsonArray.length()];
            if(jsonArray.length()>0) {
                for(int i =0; i < jsonArray.length();i++) {

                    bungLowList[i] = jsonArray.getString(i);    

                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        bunglowAdapter = new ArrayAdapter<String>(getApplicationContext(), 
                R.layout.expandable_list_item, R.id.bangDetlist, bungLowList);

        listSlide.setAdapter(bunglowAdapter);
        super.onPostExecute(result);
    }
}

以下布局适用于listview

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bangDetMainLnr"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:background="@color/White"
    android:orientation="vertical" >

        <Spinner
            android:id="@+id/bangDetSpnrAvl"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_marginRight="8dp"
            android:layout_weight="0.34"
            android:background="@drawable/spinner_background_ab_new_theme_bs"
            android:prompt="@string/select_mode"
            android:spinnerMode="dropdown" />


    <com.ojaswitech.bookingscape.library.ActionSlideExpandableListView
        android:id="@+id/bangDetlist"
        android:layout_width="300dp"
        android:layout_height="fill_parent"
        android:layout_marginTop="17dp"
        android:background="@color/White"
        android:divider="@color/white_gray"
        android:dividerHeight="4.0dp"
        android:listSelector="@drawable/list_selector" />

</LinearLayout>

以下是listview展开时的布局

?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">
    <RelativeLayout
            android:id="@+id/bangDetitem"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/bangDetTitle"
                android:textIsSelectable="false"
                android:textSize="40sp"
                android:textColor="@color/gray"/>
        <ImageView
            android:id="@+id/bangDetexpandable_toggle_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:contentDescription="@string/todo"
            android:src="@android:drawable/arrow_down_float"
            />

    </RelativeLayout>   

    <!--*** TOOLBAR LAYOUT ****-->

    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/bangDetToolBar"
            android:background="@color/white_gray_dark">

        <Button
                android:id="@+id/bangDetbuttonA"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:layout_weight="0.5"
                android:text="Keep on Hold"
                android:textSize="12sp"/>

        <Button
                android:id="@+id/bangDetbuttonB"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="0.5"
                android:text="Book"
                android:textSize="12sp"/>

    </LinearLayout>
</LinearLayout>

提前致谢

0 个答案:

没有答案