0

大家好,我是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;
}

}

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

4

1 回答 1

0

首先不要使用 getApplicationContext() 作为上下文。从当前 Activity (ListActivity.this) 中获取上下文。

当你想重新排列你的列表时,你需要调用 listitems.notifyDatasetChanged(); 告诉您的适配器它保存的数据已更改。

如果您无法弄清楚,谷歌是您的朋友,并且有大量的教程

于 2013-10-30T12:48:29.723 回答