ListItems的排序

时间:2013-10-30 12:36:13

标签: android listview sorting

大家好我是android的新手。你能帮我一个ListView。我在这里分享我所有的代码......我有一个微调器......还有一个升序按钮......根据微调器项目的升序按钮必须对列表项进行排序..

public class LeadStatusActivity extends Activity {

Spinner leadno;
ListView listitems;
Intent it;
CustomArrayAdapter customadapter;
Button ascending;
String label;

final String[] numberarr = { "LEAD003", "LEAD001", "LEAD002" };
final String[] datearr = { "1/10/2013", "2/10/2013", "3/10/2013" };
final String[] typearr = { "Home Loan", "Home Loan", "Auto Loan" };
final String[] locationarr = { "Pune", "Pune", "Pune" };
final String[] stagearr = { "Hot", "Hot", "Cold" };
final String[] namearr = { "ChandraSekhar MuthhuSwami Iyer",
        "Poorvi Bhatiya", "Rohan Khanna" };
final String[] statusarr = { "Open", "Open", "Closed" };
final String[] lastdatearr = { "4/10/2013", "4/10/2013", "2/10/2013" };

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lead_status);
    ascending = (Button) findViewById(R.id.ascending);
    leadno = (Spinner) findViewById(R.id.leadno);
    listitems = (ListView) findViewById(R.id.listitems);
    addItemsOnSpinner2();

}

public void addItemsOnSpinner2() {
    ArrayList<String> list = new ArrayList<String>();
    list.add("Lead Number");
    list.add("Lead Date");
    list.add("Product Type");
    list.add("Location");
    list.add("Client Name");
    list.add("Current Stage");
    list.add("Status");
    list.add("Last Updated Date");

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
    dataAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    leadno.setAdapter(dataAdapter);
    ascending.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (leadno.getSelectedItem().toString()
                    .equalsIgnoreCase("Lead Number")) {
                String[] strings = { "LEAD003", "LEAD001", "LEAD002" };

                Ascendingsort(LeadStatusActivity.this, strings);

                customadapter = new CustomArrayAdapter(
                        getApplicationContext(), strings, datearr, typearr,
                        locationarr, namearr, stagearr, statusarr,
                        lastdatearr);
                listitems.setAdapter(customadapter);

            } else if (leadno.getSelectedItem().toString()
                    .equalsIgnoreCase("Lead Date")) {
                String[] strings = { "1/10/2013", "2/10/2013", "3/10/2013" };

                Ascendingsort(LeadStatusActivity.this, strings);
                customadapter = new CustomArrayAdapter(
                        getApplicationContext(), numberarr, strings,
                        typearr, locationarr, namearr, stagearr, statusarr,
                        lastdatearr);
                listitems.setAdapter(customadapter);

            } else if (leadno.getSelectedItem().toString()
                    .equalsIgnoreCase("Product Type")) {
                String[] strings = { "Home Loan", "Home Loan", "Auto Loan" };

                Ascendingsort(LeadStatusActivity.this, strings);
                customadapter = new CustomArrayAdapter(
                        getApplicationContext(), numberarr, datearr,
                        strings, locationarr, namearr, stagearr, statusarr,
                        lastdatearr);
                listitems.setAdapter(customadapter);

            } else if (leadno.getSelectedItem().toString()
                    .equalsIgnoreCase("Location")) {
                String[] strings = { "Pune", "Pune", "Pune" };

                Ascendingsort(LeadStatusActivity.this, strings);
                customadapter = new CustomArrayAdapter(
                        getApplicationContext(), numberarr, datearr,
                        typearr, strings, namearr, stagearr, statusarr,
                        lastdatearr);
                listitems.setAdapter(customadapter);

            } else if (leadno.getSelectedItem().toString()
                    .equalsIgnoreCase("Current Stage")) {
                String[] strings = { "Hot", "Hot", "Cold" };

                Ascendingsort(LeadStatusActivity.this, strings);
                customadapter = new CustomArrayAdapter(
                        getApplicationContext(), numberarr, datearr,
                        typearr, locationarr, namearr, strings, statusarr,
                        lastdatearr);
                listitems.setAdapter(customadapter);

            } else if (leadno.getSelectedItem().toString()
                    .equalsIgnoreCase("Client Name")) {
                String[] strings = { "ChandraSekhar MuthhuSwami Iyer",
                        "Poorvi Bhatiya", "Rohan Khanna" };

                Ascendingsort(LeadStatusActivity.this, strings);
                customadapter = new CustomArrayAdapter(
                        getApplicationContext(), numberarr, datearr,
                        typearr, locationarr, namearr, stagearr, strings,
                        lastdatearr);
                listitems.setAdapter(customadapter);

            } else if (leadno.getSelectedItem().toString()
                    .equalsIgnoreCase("Last Updated Date")) {
                String[] strings = { "4/10/2013", "4/10/2013", "2/10/2013" };

                Ascendingsort(LeadStatusActivity.this, strings);
                customadapter = new CustomArrayAdapter(
                        getApplicationContext(), numberarr, datearr,
                        typearr, locationarr, namearr, stagearr, statusarr,
                        strings);
                listitems.setAdapter(customadapter);

            }

        }

    });

    customadapter = new CustomArrayAdapter(getApplicationContext(),
            numberarr, datearr, typearr, locationarr, namearr, stagearr,
            statusarr, lastdatearr);
    listitems.setAdapter(customadapter);

    System.out.println("PPPPPPP " + numberarr + datearr + typearr
            + locationarr + namearr + stagearr + statusarr + lastdatearr);

    listitems.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> av, View v, int position,
                long id) {
            // it = new Intent(LeadStatusActivity.this,
            // ScheduleCalculatorActivity.class);
            // startActivity(it);

        }
    });

}

protected String[] Ascendingsort(LeadStatusActivity leadStatusActivity,
        String[] strings) {

    Arrays.sort(strings);
    return strings;
}

}

这是我的customeArrayadapter类

public class CustomArrayAdapter extends ArrayAdapter<String> {
private Context context;
private String[] numberarr, datearr, typearr, locationarr, namearr,
        stagearr, statusarr, lastdatearr;

public CustomArrayAdapter(Context context, String[] numberarr,
        String[] datearr, String[] typearr, String[] locationarr,
        String[] namearr, String[] stagearr, String[] statusarr,
        String[] lastdatearr) {
    super(context, R.layout.leadstatus_row, numberarr);
    this.context = context;
    this.numberarr = numberarr;
    this.datearr = datearr;
    this.typearr = typearr;
    this.locationarr = locationarr;
    this.namearr = namearr;
    this.stagearr = stagearr;
    this.statusarr = statusarr;
    this.lastdatearr = lastdatearr;

}

public CustomArrayAdapter(Context con) {
    super(con, R.layout.lead_status);
    this.context = con;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = convertView;

    if (rowView == null) {
        rowView = inflater.inflate(R.layout.leadstatus_row, parent, false);
    }
    TextView number = (TextView) rowView.findViewById(R.id.number);
    TextView date = (TextView) rowView.findViewById(R.id.date);
    TextView type = (TextView) rowView.findViewById(R.id.type);
    TextView location = (TextView) rowView.findViewById(R.id.locname);
    TextView clientname = (TextView) rowView.findViewById(R.id.name);
    TextView currentstage = (TextView) rowView.findViewById(R.id.stage);
    TextView status = (TextView) rowView.findViewById(R.id.stat);
    TextView lastdate = (TextView) rowView.findViewById(R.id.lastdate);

    if (!numberarr[position].equalsIgnoreCase("")) {
        number.setText(numberarr[position]);
    }
    if (!datearr[position].equalsIgnoreCase("")) {
        date.setText(datearr[position]);
    }
    if (!typearr[position].equalsIgnoreCase("")) {
        type.setText(typearr[position]);
    }
    if (!locationarr[position].equalsIgnoreCase("")) {
        location.setText(locationarr[position]);
    }
    if (!namearr[position].equalsIgnoreCase("")) {
        clientname.setText(namearr[position]);
    }
    if (!stagearr[position].equalsIgnoreCase("")) {
        currentstage.setText(stagearr[position]);
    }
    if (!statusarr[position].equalsIgnoreCase("")) {
        status.setText(statusarr[position]);
    }
    if (!lastdatearr[position].equalsIgnoreCase("")) {
        lastdate.setText(lastdatearr[position]);
    }

    // TextView textView = (TextView) rowView.findViewById(R.id.labels);
    // ImageView imageView = (ImageView) rowView.findViewById(R.id.icons);
    /*
     * if (!options[position].equals("")) {
     * textView.setText(options[position]); } if (images[position] != 0) {
     * imageView.setImageResource(images[position]); } else {
     * imageView.setImageResource(R.drawable.ic_launcher); }
     */
    return rowView;
}

}

请朋友们帮助我。现在问题是某个特定的排序数组已排序,但整个列表项保持不变。

1 个答案:

答案 0 :(得分:0)

首先不要将getApplicationContext()用于上下文。从当前活动(ListActivity.this)获取上下文。

如果要重新排列列表,则需要调用listitems.notifyDatasetChanged();告诉适配器它所拥有的数据已经改变。

如果你无法弄清楚,谷歌是你的朋友,并且有很多教程可供选择