如何将动态数据添加到ExpandableListView?

时间:2015-07-13 07:07:39

标签: android expandablelistview

鉴于静态数据,如何使用JSON和REST添加动态数据以及如何在服务器上获取数据请给我任何解决方案

http://examples.javacodegeeks.com/android/core/ui/expandablelistview/android-expandablelistview-example/

1 个答案:

答案 0 :(得分:0)

将动态数据填充到可扩展列表视图几乎与静态数据类似。只需收集动态数据并将其存储在ArrayList中,并以类似方式加载静态数据

要从服务器获取数据,请按照此处的步骤进行操作

首先在build.gradle文件

中编译依赖项
compile 'com.mcxiaoke.volley:library-aar:1.0.0'

现在创建一个名为Appcontroller的类,它扩展Application并添加它并导入那些显示红线的api

public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();


    private RequestQueue mRequestQueue;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;


    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());


        }

        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        int socketTimeOut = 60000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeOut, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        req.setRetryPolicy(policy);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }

    public static String getDate() {
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        Date date = new Date();
        String formatedDate = dateFormat.format(date);
        return formatedDate;
    }
}

之后,从服务器获取数据使用此方法 在课程级别添加这两个

Response.Listener<String> jsonResponse;
Response.ErrorListener errorListener;

现在,在onCreate

中初始化它们
jsonResponse = new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Log.d("Response", response);

        //parseData(response); and store in arraylist to show it in expandablelistview


    }
};

errorListener = new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError volleyError) {
        Toast.makeText(getApplicationContext(), "Unable to fetch Data from server", Toast.LENGTH_SHORT).show();
        finish();
    }
};
if (ServerConfig.isNetworkOnline(SplashScreen.this)) {
    StringRequest strReq = new StringRequest(Request.Method.POST, "your server link", serverResponse, errorListener) {
    };
    AppController.getInstance().getRequestQueue().add(strReq);
} else {
    Toast.makeText(getApplicationContext(), "Please check your network connection", Toast.LENGTH_SHORT).show();
    finish();
}

如果问题让我知道,不要忘记将Appcontroller添加到您的 清单

<application
    android:name=".AppController"..../>