如何通过单击列标题对多列listView进行排序?安卓系统

时间:2019-04-19 13:30:05

标签: java android sorting listview

我有一个multicolumn listView,它从两个String数组和两个int数组动态填充。我还添加了一个标头(使用addHeaderView),它基本上是ConstraintLayout中的4个textView,与rowView相同。

例如,第一列的名称为“ nameAndSurname”,我需要能够单击它,并按字母顺序对listView中的所有名称和姓氏进行排序。与其他列相同:“工作”,“工资”,“年龄”。

现在,当我单击标题时,整个标题将突出显示,并且什么也没有发生。

非常感谢您的所有反馈!

我的代码如下:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_people);

    ListView peopleList = findViewById(R.id.peopleList);
    ViewGroup headerView=(ViewGroup)getLayoutInflater().inflate(R.layout.people_header, peopleList,false);
    peopleList.addHeaderView(headerView, null, true);

    MyAdapter adapter = new MyAdapter(this, namesAndSurnames, jobs, salaries, ages);
    peopleList.setAdapter(adapter);
    peopleList.setOnItemClickListener(this);
}

//and MyAdapter class

class MyAdapter extends ArrayAdapter<String> {
    Context context;
    String[] myNamesAndSurnames;
    String[] myJobs;
    String[] myAges;
    String[] mySalaries;

    MyAdapter(Context c, String[] namesAndSurnames, String[] jobs, String[] salaries, String[] ages) {
        super(c, R.layout.people_row, R.id.rowName, namesAndSurnames);
        this.context = c;
        this.myNamesAndSurnames = namesAndSurnames;
        this.myJobs = jobs;
        this.mySalaries = salaries;
        this.myAges = ages;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View peopleRow = layoutInflater.inflate(R.layout.people_row, parent, false);
        TextView rowName = peopleRow.findViewById(R.id.rowName);
        TextView rowJob = peopleRow.findViewById(R.id.rowJob);
        TextView rowSalary = peopleRow.findViewById(R.id.rowSalary);
        TextView rowAge = peopleRow.findViewById(R.id.rowAge);

        rowName.setText(myNamesAndSurnames[position]);
        rowJob.setText(myJobs[position]);
        rowSalary.setText(mySalaries[position]);
        rowAge.setText(myAges[position]);
        return peopleRow;
    }
}

0 个答案:

没有答案