1

我正在使用 Android 中的部分列表视图来根据日期显示呼叫详细信息。指在特定日期下的电话号码的详细信息。但是当我在同一日期接到 2 个电话时,最后一个日期仅可见,并且列表不显示该日期的其余电话。

不同日期的通话显示正确,但同一日期的通话显示不正确,只显示最后一次通话。

我正在使用以下代码:

public String response = "{ \"Message\":\"Success\", "
        + "\"Data\":[ { \"ACCOUNT\":\"000014532497\", "
        + "\"DATE\":\"8/6/2006\", \"TIME\":\"15:37:14\", "
        + "\"CH_ITEM\":\"341T\", \"ITEM\":\"TIMEUSED\", "
        + "\"DESCRIPTION\":\"FROM3103475779\", \"DETAIL\":"
        + "\"UnitedKingdom011441980849463\", \"QUANTITY\":84, "
        + "\"RATE\":0.025, \"AMOUNT\":2.1, \"ACTUAL\":83.2, "
        + "\"NODE_NAME\":\"TNT02\", \"USER_NAME\":\"Shailesh Sharma\""
        + ", \"MODULE_NAME\":\"DEBIT\", \"ANI\":\"3103475779\", "
        + "\"DNIS\":\"3103210104\", \"ACCOUNT_GROUP\":\"WEBCC\", "
        + "\"SALES_REP\":\"sascha_d\", \"SALES_REP2\":\"\", \"SALES_REP3"
        + "\":\"\", \"IN_PORT\":\"I10\", \"EXTRA1\":\"RATE\", \"EXTRA2\":"
        + "\"44\", \"EXTRA3\":\"UnitedKingdom\", \"OUT_PORT\":\"I70\", "
        + "\"CRN\":\"WEBCC\", \"CallId\":null, \"ID\":4517734, \"PhoneNumber"
        + "\":\"011441980849463\" }, {\"ACCOUNT\":\"000014532497\",\"DATE\":"
        + "\"8/6/2006\",\"TIME\":\"09:22:57\",\"CH_ITEM\":\"541T\",\"ITEM\":"
        + "\"TIMEUSED\",\"DESCRIPTION\":\"FROM3103475779\",\"DETAIL\":"
        + "\"UnitedKingdom011447914422787\",\"QUANTITY\":1,\"RATE\":0.29,"
        + "\"AMOUNT\":0.29,\"ACTUAL\":0.5,\"NODE_NAME\":\"TNT02\",\"USER_NAME"
        + "\":\"Tusshar\",\"MODULE_NAME\":\"DEBIT\",\"ANI\":\"3103475779\",\"DNIS"
        + "\":\"6173950047\",\"ACCOUNT_GROUP\":\"WEBCC\",\"SALES_REP\":\"sascha_d"
        + "\",\"SALES_REP2\":\"\",\"SALES_REP3\":\"\",\"IN_PORT\":\"I30\",\"EXTRA1"
        + "\":\"RATE\",\"EXTRA2\":\"44\",\"EXTRA3\":\"UnitedKingdom-Special\","
        + "\"OUT_PORT\":\"I90\",\"CRN\":\"WEBCC\",\"CallId\":null,\"ID\":4535675,"
        + "\"PhoneNumber\":\"011447914422787\"}, ], \"NumberOfContacts\":2, "
        + "\"TotalCharges\":4.830000000000001 }";

try {
    JSONObject jsonObj = new JSONObject(response);
    String message = jsonObj.getString("Message");

    if (message != null && message.equalsIgnoreCase("Success")) {

        JSONArray dataArray = jsonObj.getJSONArray("Data");
        System.out.println(dataArray.length());

        for (int i = 0; i < dataArray.length(); i++) {
            JSONObject history = dataArray.getJSONObject(i);
            _date = history.getString("DATE");

            String updatedDate = createDateFormat(_date);
            // notes =new ArrayList<String>();
            itemList = new ArrayList<Object>();

            // ADDING DATE IN THE ARRAYLIST<String>
            days.add(updatedDate);
            _username = history.getString("USER_NAME");
            _number = history.getString("PhoneNumber");
            _time = history.getString("TIME");
            _amount = history.getString("AMOUNT");
            _duration = history.getString("QUANTITY");

            /*
             * notes.add(_username); notes.add(_number);
             * notes.add(_time);
             */
            AddObjectToList(_username, _number, _time, _amount,
                    _duration);

            // listadapter = new <String>(this, R.layout.list_item,
            // notes);
            listadapter = new ListViewCustomAdapter(this, itemList);
            adapter.addSection(days.get(i), listadapter);
        }
    }
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

public class SeparatedListAdapter extends BaseAdapter {

    /*
     * public final Map<String, Adapter> sections = new
     * LinkedHashMap<String, Adapter>();
     */
    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);
    }

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

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

这是我的实际要求:

这就是现在正在发生的事情。 在此处输入图像描述

4

1 回答 1

1

SectionListExampleActivity 是我从 JSON Web 服务获得响应的主类。在 getJSONResposne 方法中,我调用了EntryAdaptor

每个标题的 SECTION HEADER 和 ITEM ENTRY 有两个单独的 geter setter 类。

public class SectionListExampleActivity extends Activity implements OnClickListener, OnItemSelectedListener, IServerResponse {
    /** Called when the activity is first created. */

    private ArrayList<Item> items = new ArrayList<Item>();
    boolean firstTime = true;

    private Spinner _spinner=null;
    private ArrayAdapter _amountAdaptor = null;
    private ArrayList<String> _monthList =new ArrayList<String>();
    private ListView _list=null;

    private Button _monthButton=null;
    private ImageButton _backImageButton=null;
    private ImageButton _headerImageButton=null;
    private String _token;
    private String _account;
    private Point p=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_history);

        String response = this.getIntent().getExtras().getString("history_resp");
        _token = Constant.AUTH_TOKEN;
        _account = Constant.ACCOUNT_NUM;

        _list = (ListView)findViewById(R.id.listview);
        getJSON_Response(response,Constant.PID_ACCOUNT_HISTORY);

        EntryAdapter adapter = new EntryAdapter(this, items);

        _list.setAdapter(adapter);

        _monthList.add("Months");
        _monthList.add("January");
        _monthList.add("February");
        _monthList.add("March");
        _monthList.add("April");
        _monthList.add("May");
        _monthList.add("June");
        _monthList.add("July");
        _monthList.add("August");
        _monthList.add("September");
        _monthList.add("October");
        _monthList.add("November");
        _monthList.add("December");

        _spinner = (Spinner)findViewById(R.id.month_spinner);
        _amountAdaptor = new ArrayAdapter(this,
                android.R.layout.simple_spinner_dropdown_item,
                _monthList);
        _spinner.setAdapter(_amountAdaptor);
        _spinner.setOnItemSelectedListener(this);

        _monthButton = (Button)findViewById(R.id.monthSpinner_button);
        _monthButton.setOnClickListener(this);

        _backImageButton = (ImageButton)findViewById(R.id.back_ImageButton);
        _backImageButton.setOnClickListener(this);
        _headerImageButton =(ImageButton)findViewById(R.id.header_ImageButton);
        _headerImageButton.setOnClickListener(this);
    }


    private void getJSON_Response(String response,int pid) {

        switch (pid) {
        case Constant.PID_ACCOUNT_HISTORY:
            try {
                JSONObject jsonObj = new JSONObject(response);
                String message = jsonObj.getString("Message");

                if(message!=null && message.equalsIgnoreCase("Success")){

                    JSONArray dataArray = jsonObj.getJSONArray("Data");
                    System.out.println(dataArray.length());
                    String lastAddedDate = null;
                    for (int i = 0; i <dataArray.length(); i++) {
                        JSONObject history = dataArray.getJSONObject(i);
                        String date  = history.getString("DATE");


                        if(firstTime || !(date.equalsIgnoreCase(lastAddedDate))){
                            firstTime=false;
                            lastAddedDate = date;
                            items.add(new SectionItem(date));
                        }
                        String username= history.getString("USER_NAME");
                        String number = history.getString("PhoneNumber");
                        String time = history.getString("TIME");
                        String amount=history.getString("AMOUNT");
                        String duration =history.getString("QUANTITY");

                        items.add(new EntryItem(username,duration,amount,number,time));
                    }
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;

        default:
            break;
        }
    }

    @Override
    public void onClick(View v) {
        if(v==_monthButton){
            _spinner.performClick();
        }else if(v==_backImageButton){
            SectionListExampleActivity.this.finish();
        }else if(v== _headerImageButton){
            if (p != null)
                showPopup(SectionListExampleActivity.this, p);
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int position,
            long arg3) {
        if(position!=0){
            switch (parent.getId()) {
            case R.id.month_spinner:
                String selectedItem =  _spinner.getSelectedItem().toString();
                _monthButton.setBackgroundResource(R.drawable.month_blank);
                _monthButton.setText(selectedItem);

                final Calendar c = Calendar.getInstance();
                int year = c.get(Calendar.YEAR);
                int month = c.get(Calendar.MONTH);
                int day = c.get(Calendar.DAY_OF_MONTH);
                String _historyURL = Constant.prodORdevUrl + "GetAccountHistory?token="+_token+"&account="+_account+"&month="+month+"&year="+year;
                getHistory(_historyURL,true);
                break;
            default:
                break;
            }
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }           
}



public class EntryAdapter extends ArrayAdapter<Item> implements IServerResponse {

private Context context;
private ArrayList<Item> items;
private LayoutInflater vi;
private String _token;
private String _account;

public EntryAdapter(Context context,ArrayList<Item> items) {
    super(context,0, items);
    this.context = context;
    this.items = items;
    vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    _token = Constant.AUTH_TOKEN;
    _account = Constant.ACCOUNT_NUM;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    final Item i = items.get(position);
    if (i != null) {
        if(i.isSection()){
            SectionItem si = (SectionItem)i;
            v = vi.inflate(R.layout.list_item_section, null);

            v.setOnClickListener(null);
            v.setOnLongClickListener(null);
            v.setLongClickable(false);

            final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
            String date  =createDateFormat(si.getTitle());
            sectionView.setText(date);
        }else{
            EntryItem ei = (EntryItem)i;
            v = vi.inflate(R.layout.list_item_entry, null);
            final RelativeLayout relay = (RelativeLayout)v.findViewById(R.id.account_history_item_relay);
            final TextView username = (TextView)v.findViewById(R.id.user_name_textview);
            final TextView amount = (TextView)v.findViewById(R.id.amount_textview);
            final TextView duration = (TextView)v.findViewById(R.id.duration_textview);
            final TextView phone = (TextView)v.findViewById(R.id.phone_no_textview);
            final TextView time = (TextView)v.findViewById(R.id.time_textview);

            relay.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    makeCall(phone.getText().toString());
                }
            });

            if (username != null) 
                username.setText(ei.username);
            if(amount != null)
                amount.setText(ei.duration + "min");
            if(duration != null)
                duration.setText("$"+ ei.amount);
            if(phone != null)
                phone.setText(ei.number);
            if(time != null)
                time.setText(ei.time);
        }
    }
    return v;
}

void makeCall(String destination) {
    if(_token!=null && _account!=null){

        if(destination!=null && !destination.equals("")){
            String phoneNumber = Constant.getPhoneNumber(this.context.getApplicationContext());
            if(phoneNumber!=null && phoneNumber.length()>0){
                String callURL =WebService.WEB_SERVICE_URL+"PlaceLongDistanceCall?token="+_token +
                        "&phonenumber="+phoneNumber+"&destinationNumber="+destination+"&authtoken="+_token;

                getCall(callURL,true);
            }else{
                Constant.showToast(this.context, Constant.INSERT_SIM);
            }
        }else{
            Constant.showToast(this.context, "In valid destination number.");
        }
    }
}

}
于 2013-03-08T08:37:24.370 回答