排序与ArrayAdapter℃的ListView控件

排序与ArrayAdapter℃的ListView控件

本文介绍了排序与ArrayAdapter℃的ListView控件;弦乐>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的ListView,每个项目有四个TextViews显示银行名称,金额,日期和时间。这个数据被存储在数据库中。这个想法是对的活动有是在点击排序按钮会打开一个快速行动对话框。对话已在列表顶部的三个选项为排序的银行名称为升序按日期排序最新的第一个和排序量数额较大。我没有对如何与排序任务进行任何想法在onItemClick(INT POS)被写入。任何人都可以请帮助我在这?

 公共类TransactionMenu扩展活动实现OnItemClickListener,OnActionItemClickListener {
    的String [] TRANSID;
    的String [] mBankName;
    的String [] mAmount;
    的String [] mDate;
    的String [] MTIME;
    按钮SortButton;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.transaction_screen);        SortButton =(按钮)findViewById(R.id.sortKey);        //银行名称行动项目
        ActionItem BNAME =新ActionItem();        bName.setTitle(银行名称);
        bName.setIcon(getResources()getDrawable(R.drawable.bank_256));        //金额行动项目
        ActionItem AMT =新ActionItem();        amt.setTitle(数量);
        amt.setIcon(getResources()getDrawable(R.drawable.cash));        //日期行动项目
        ActionItem日期=新ActionItem();        date.setTitle(日期);
        date.setIcon(getResources()getDrawable(R.drawable.calender));        //创建quickaction
        最后QuickAction quickAction =新QuickAction(本);        quickAction.addActionItem(BNAME);
        quickAction.addActionItem(AMT);
        quickAction.addActionItem(日期);        quickAction.setOnActionItemClickListener(本);        SortButton.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                quickAction.show(五);
                //quickAction.setAnimStyle(QuickAction.ANIM_REFLECT);
            }
        });        DBAdapter lDBAdapter =新DBAdapter(本);
        lDBAdapter.open();        / * getTransDetails()返回存储在事务表中的所有detials * /
        光标mCursor = lDBAdapter.getAllTransDetails();
        的System.out.println(CUR ...........+ mCursor);
        lDBAdapter.close();
        如果(mCursor!= NULL){
            INT大小= mCursor.getCount();
            如果(mCursor.moveToFirst()){
                TRANSID =新的String [大小]
                mAmount =新的String [大小]
                mBankName =新的String [大小]
                mDate =新的String [大小]
                Mtime时光网=新的String [大小]                的for(int i = 0; I<大小;我++,mCursor.moveToNext()){
                    TRANSID [I] = mCursor.getString(0);
                    mAmount [I] = mCursor.getString(1);
                    mBankName [I] = mCursor.getString(3);
                    mDate [I] = mCursor.getString(2);
                    MTIME [I] = mCursor.getString(4);
                }
            }
        }
        的for(int i = 0; I< mCursor.getCount();我++){
            的System.out.println(TRANSID是++++++++++++++++ TRANSID [I]);
            的System.out.println(量++++++++++++++++ mAmount [I]);
            的System.out.println(BANKNAME是++++++++++++++++ mBankName [I]);
            的System.out.println(日期是++++++++++++++++ mDate [I]);
            的System.out.println(时间就是++++++++++++++++ MTIME [I]);
        }        ListView控件myListView =(ListView控件)findViewById(R.id.transactionListView);
        MyBaseAdapter myAdapterObj =新MyBaseAdapter(TransactionMenu.this,R.layout.list_item,TRANSID);
        myListView.setAdapter(myAdapterObj);
        myListView.setOnItemClickListener((OnItemClickListener)本);
    }    私有类MyBaseAdapter扩展ArrayAdapter<串GT; {
        公共MyBaseAdapter(上下文的背景下,INT textViewResourceId,字符串[] TRANSID){
            超(背景下,textViewResourceId,TRANSID);
        }        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
            LayoutInflater吹气= getLayoutInflater();
            查看排= inflater.inflate(R.layout.list_item,父母,假);
            TextView的标签=(TextView中)row.findViewById(R.id.textview1);
            label.setText(金额+ mAmount [位置]);
            TextView的LABEL1 =(TextView中)row.findViewById(R.id.textview2);
            label1.setText(银行名称:+ mBankName [位置]);
            TextView的LABEL2 =(TextView中)row.findViewById(R.id.textview3);
            label2.setText(日期:+ mDate [位置]);
            TextView的LABEL3 =(TextView中)row.findViewById(R.id.textview4);
            label3.setText(时间:+ MTIME [位置]);
            返回行;
        }
    }    公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){
        的System.out.println(ARG2是+++++++++++++++ ARG2);
        INT lRowId =的Integer.parseInt(TRANSID [ARG2]);
    }    公共无效onItemClick(INT POS){
        MyBaseAdapter myAdapterObj =新MyBaseAdapter(TransactionMenu.this,R.layout.list_item,TRANSID);
        如果(POS == 0){
            Toast.makeText(TransactionMenu.this,银行名称项选择,Toast.LENGTH_SHORT).show();
        }
        否则如果(POS == 1){
            Toast.makeText(TransactionMenu.this,量项目入选,Toast.LENGTH_SHORT).show();
        }
        其他{
            Toast.makeText(TransactionMenu.this,日期项目中选择Toast.LENGTH_SHORT).show();
        }
    }
}


解决方案

您可以使用以下code。你需要保持在 BankInfo 对象的银行信息。创建 BankInfo 对象的ArrayList,然后你可以使用这个code。它不是一个好的做法,以保持相关信息到单独的数组。

  Col​​lections.sort(mBankInfoArrayList,新的比较< BankInfo>(){
      INT比较(BankInfo OBJ1,BankInfo OBJ2){
            返回obj1.getBankName()与compareToIgnoreCase(obj2.getBankName())。
      }
});

I have a custom ListView, each list item has four TextViews showing bank name, amount, date and time. This data is stored in a database. The idea is that on the Activity there is a quick action dialog which opens on clicking the sort button. The Dialog has three options as "Sort by bank name" ascending order, "Sort by Date" newest first and "Sort by amount" larger amount in the top of the list. I don't have any idea of how to proceed with the sorting task to be written in onItemClick(int pos). Can anyone please help me on this?

public class TransactionMenu extends Activity implements OnItemClickListener, OnActionItemClickListener {
    String[] TransId ;
    String[] mBankName;
    String[] mAmount;
    String[] mDate;
    String[] mTime;
    Button SortButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.transaction_screen);

        SortButton = (Button)findViewById(R.id.sortKey);

        //Bank Name action item
        ActionItem bName = new ActionItem();

        bName.setTitle("Bank Name");
        bName.setIcon(getResources().getDrawable(R.drawable.bank_256));

        //Amount action item
        ActionItem amt = new ActionItem();

        amt.setTitle("Amount");
        amt.setIcon(getResources().getDrawable(R.drawable.cash));

        //date action item
        ActionItem date = new ActionItem();

        date.setTitle("Date");
        date.setIcon(getResources().getDrawable(R.drawable.calender));

        //create quickaction
        final QuickAction quickAction = new QuickAction(this);

        quickAction.addActionItem(bName);
        quickAction.addActionItem(amt);
        quickAction.addActionItem(date);

        quickAction.setOnActionItemClickListener(this);

        SortButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                quickAction.show(v);
                //quickAction.setAnimStyle(QuickAction.ANIM_REFLECT);
            }
        });

        DBAdapter lDBAdapter = new DBAdapter(this);
        lDBAdapter.open();

        /* getTransDetails() returns all the detials stored in the transaction table*/
        Cursor mCursor =lDBAdapter.getAllTransDetails();
        System.out.println("cur..........."+mCursor);
        lDBAdapter.close();
        if (mCursor != null) {
            int size = mCursor.getCount();
            if (mCursor.moveToFirst()) {
                TransId = new String[size];
                mAmount = new String[size];
                mBankName = new String[size];
                mDate = new String[size];
                mTime = new String[size];

                for (int i = 0; i < size; i++, mCursor.moveToNext()) {
                    TransId[i] = mCursor.getString(0);
                    mAmount[i] = mCursor.getString(1);
                    mBankName[i] = mCursor.getString(3);
                    mDate[i] = mCursor.getString(2);
                    mTime[i] = mCursor.getString(4);
                }
            }
        }
        for (int i = 0; i < mCursor.getCount(); i++) {
            System.out.println("TransId is+++++++++++++++ "+TransId[i]);
            System.out.println("amount is+++++++++++++++ "+mAmount[i]);
            System.out.println("bankName is+++++++++++++++ "+mBankName[i]);
            System.out.println("date is+++++++++++++++ "+mDate[i]);
            System.out.println("time is+++++++++++++++ "+mTime[i]);
        }

        ListView myListView = (ListView) findViewById(R.id.transactionListView);
        MyBaseAdapter myAdapterObj = new MyBaseAdapter(TransactionMenu.this, R.layout.list_item, TransId);
        myListView.setAdapter(myAdapterObj);
        myListView.setOnItemClickListener((OnItemClickListener) this);
    }

    private class MyBaseAdapter extends ArrayAdapter<String>  {
        public MyBaseAdapter(Context context, int textViewResourceId, String[] transId) {
            super(context, textViewResourceId, transId);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View row = inflater.inflate(R.layout.list_item, parent, false);
            TextView label = (TextView)row.findViewById(R.id.textview1);
            label.setText("Amount:  "+mAmount[position]);
            TextView label1 = (TextView) row.findViewById(R.id.textview2);
            label1.setText("Bank Name:  "+mBankName[position]);
            TextView label2 = (TextView) row.findViewById(R.id.textview3);
            label2.setText("Date:  "+mDate[position]);
            TextView label3 = (TextView) row.findViewById(R.id.textview4);
            label3.setText("Time: "+mTime[position]);
            return row;
        }
    }

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        System.out.println("arg2 is++++++++++++++"+arg2);
        int lRowId = Integer.parseInt(TransId[arg2]);
    }

    public  void onItemClick(int pos) {
        MyBaseAdapter myAdapterObj = new MyBaseAdapter(TransactionMenu.this, R.layout.list_item, TransId);
        if (pos == 0) {
            Toast.makeText(TransactionMenu.this, "Bank name item selected", Toast.LENGTH_SHORT).show();
        }
        else if (pos ==1) {
            Toast.makeText(TransactionMenu.this, "amount item selected", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(TransactionMenu.this, "Date item selected", Toast.LENGTH_SHORT).show();
        }
    }
}
解决方案

You can use the following code. You need to maintain the bank info in a BankInfo object. Create an ArrayList of BankInfo objects and then you can use this code. Its not a good practice to keep related info into separate arrays.

Collections.sort(mBankInfoArrayList, new Comparator<BankInfo>() {
      int compare(BankInfo obj1, BankInfo obj2) {
            return obj1.getBankName().compareToIgnoreCase(obj2.getBankName());
      }
});

这篇关于排序与ArrayAdapter℃的ListView控件;弦乐&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:09