1

我想知道是否有人可以帮助我弄清楚尝试将 JSON 结果中的日期用作节标题时没有得到什么?我正在使用来自http://jsharkey.org/blog/2008/08/18/separating-lists-with-headers-in-android-09/的以下列表适配器代码

package com.example.androidhive;

import java.util.LinkedHashMap;
import java.util.Map;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;

public class SeparatedListAdapter extends BaseAdapter {

public final Map<String, Adapter> sections = new LinkedHashMap<String, Adapter>();
public final ArrayAdapter<String> headers;
public final static int TYPE_SECTION_HEADER = 0;

public SeparatedListAdapter(Context context) {
    headers = new ArrayAdapter<String>(context, R.layout.list_header);
}

public void addSection(String section, Adapter adapter) {
    this.headers.add(section);
    this.sections.put(section, adapter);
}

public Object getItem(int position) {
    for (Object section : this.sections.keySet()) {
        Adapter adapter = sections.get(section);
        int size = adapter.getCount() + 1;

        // check if position inside this section
        if (position == 0)
            return section;
        if (position < size)
            return adapter.getItem(position - 1);

        // otherwise jump into next section
        position -= size;
    }
    return null;
}

public int getCount() {
    // total together all sections, plus one for each section header
    int total = 0;
    for (Adapter adapter : this.sections.values())
        total += adapter.getCount() + 1;
    return total;
}

@Override
public int getViewTypeCount() {
    // assume that headers count as one, then total all sections
    int total = 1;
    for (Adapter adapter : this.sections.values())
        total += adapter.getViewTypeCount();
    return total;
}

@Override
public int getItemViewType(int position) {
    int type = 1;
    for (Object section : this.sections.keySet()) {
        Adapter adapter = sections.get(section);
        int size = adapter.getCount() + 1;

        // check if position inside this section
        if (position == 0)
            return TYPE_SECTION_HEADER;
        if (position < size)
            return type + adapter.getItemViewType(position - 1);

        // otherwise jump into next section
        position -= size;
        type += adapter.getViewTypeCount();
    }
    return -1;
}

public boolean areAllItemsSelectable() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    return (getItemViewType(position) != TYPE_SECTION_HEADER);
}

public View getView(int position, View convertView, ViewGroup parent) {
    int sectionnum = 0;
    for (Object section : this.sections.keySet()) {
        Adapter adapter = sections.get(section);
        int size = adapter.getCount() + 1;

        // check if position inside this section
        if (position == 0)
            return headers.getView(sectionnum, convertView, parent);
        if (position < size)
            return adapter.getView(position - 1, convertView, parent);

        // otherwise jump into next section
        position -= size;
        sectionnum++;
    }
    return null;
}

public long getItemId(int position) {
    return position;
}
}

连同此处的 PHP/MySQL/Sample 代码:http ://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/

通过使用以下方法,我设法使分段列表在某种程度上起作用:

@Override
protected void onPostExecute(String file_url) {
    // dismiss the dialog after getting all products
    pDialog.dismiss();
    // updating UI from Background Thread
    runOnUiThread(new Runnable() {
        public void run() {
        /**
        * Updating parsed JSON data into ListView
        * */
    SeparatedListAdapter listadapter = new SeparatedListAdapter(
                activity);
        String date = null;
        try {
            JSONObject c = products.getJSONObject(1);
            date = c.getString(TAG_DATE);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        listadapter.addSection(date, new SimpleAdapter(activity,
                productsList, R.layout.list_item, new String[] {
                TAG_PID, TAG_NAME, TAG_DATE }, new int[] {
                R.id.pid, R.id.name, R.id.date }));
        listadapter.addSection("Security Test", new SimpleAdapter(
            activity, productsList, R.layout.list_item,
            new String[] { TAG_PID, TAG_NAME, TAG_DATE },
            new int[] { R.id.pid, R.id.name, R.id.date }));

                // updating listview
                setListAdapter(listadapter);
            }
        });

    }

然而,这会导致这样的事情:

在此处输入图像描述

有没有办法从 JSON 查询中解析日期,然后在“for”块中使用它,以便每个部分都是日期(特别是月份),然后只显示每个部分中每个月的值?我正在使用以下内容来获取月份名称,但这无助于过滤列表:

SeparatedListAdapter listadapter = new SeparatedListAdapter(
                    activity);
            java.util.Date datestr = null;
            String month_name = null;

            try {

                JSONObject c = products.getJSONObject(1);

                SimpleDateFormat inputFormatter = new SimpleDateFormat(
                                "yyyy-MM-dd HH:mm:ss"); 
                SimpleDateFormat outputFormatter = new SimpleDateFormat(
                                "MMMM");

                try {
                    datestr = inputFormatter.parse(c
                            .getString(TAG_DATE));
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                month_name = outputFormatter.format(datestr);
4

0 回答 0