Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
89 views
in Technique[技术] by (71.8m points)

java - How to group items from database and display them - Android

I have receipt and logs model in android app. Receipt hasMany logs.

I made query for group_by logs by sortID and grade.

//recieve RecepitID and query to group logs
final long forwardedId = (long) getIntent().getExtras().get(String.valueOf("recepitID"));
List<Logs> logsList = new Select().from(Logs.class).where("Receipt = " + forwardedId).groupBy("SortID, Grade").execute();

This grouping works fine. Problem is next. I need to have output like this (part in red circle): enter image description here but I get it like this:

enter image description here

And this is part of code how I done this.

public class LogsRecapitulation extends AppCompatActivity {

    private ListView mainListView;
    private BaseAdapter listAdapter;
    private TextView logsCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recapitulation_listview);
        mainListView = (ListView) findViewById(R.id.ListViewItem);

        //recieve RecepitID and query to group logs
        final long forwardedId = (long) getIntent().getExtras().get(String.valueOf("recepitID"));
        List<Logs> logsList = new Select().from(Logs.class).where("Receipt = " + forwardedId).groupBy("SortID, Grade").execute();

        TextView result = (TextView) findViewById(R.id.LogMassResult);
        double sum = 0.0;
        for (int i = 0; i < logsList.size(); i++) {
            sum += logsList.get(i).getM3();
        }
        result.setText(String.format("%.2f m3", sum));

        for (int i = 0; i < logsList.size(); i++) {
            if (logsList.get(i).receipt.priceType.equals("Na panju")) {
                TextView stumpPriceKN = (TextView) findViewById(R.id.sumPriceKN);
                double sumPricekn = 0.0;

                for (int j = 0; j < logsList.size(); j++) {
                    sumPricekn += logsList.get(j).price.stumpPrice_kn * logsList.get(j).getM3();
                }
                stumpPriceKN.setText(String.format("%.2f KN", sumPricekn));

            } else {
                TextView roadKN = (TextView) findViewById(R.id.sumPriceKN);
                double roadPrKn = 0.0;
                for (int j = 0; j < logsList.size(); j++) {
                    roadPrKn += logsList.get(j).price.roadPrice_kn * logsList.get(j).getM3();
                }
                roadKN.setText(String.format("%.2f KN", roadPrKn));
            }
        }

        for (int i = 0; i < logsList.size(); i++) {
            if (logsList.get(i).receipt.priceCorrection > 0 && logsList.get(i).receipt.priceType.equals("Na panju")) {
                TextView corecctionPriceKn = (TextView) findViewById(R.id.correctionPriceKN);
                double correcSumKN = 0.0;
                for (int j = 0; j < logsList.size(); j++) {
                    correcSumKN += (logsList.get(j).price.stumpPrice_kn * logsList.get(j).getM3()) + ((logsList.get(j).price.stumpPrice_kn * logsList.get(j).getM3()) * logsList.get(j).receipt.priceCorrection / 100);
                }
                corecctionPriceKn.setText(String.format("%.2f KN", correcSumKN));
            } else if (logsList.get(i).receipt.priceCorrection > 0 && logsList.get(i).receipt.priceType.equals("?umska cesta")) {
                TextView corecctionPriceKn = (TextView) findViewById(R.id.correctionPriceKN);
                double correcSumKN = 0.0;
                for (int j = 0; j < logsList.size(); j++) {
                    correcSumKN += (logsList.get(j).price.roadPrice_kn * logsList.get(j).getM3()) + ((logsList.get(j).price.roadPrice_kn * logsList.get(j).getM3()) * logsList.get(j).receipt.priceCorrection / 100);
                }
                corecctionPriceKn.setText(String.format("%.2f KN", correcSumKN));
            } else {
                TextView priceHolder = (TextView) findViewById(R.id.KorekcijaCijene);
                TextView corecctionPriceKn = (TextView) findViewById(R.id.correctionPriceKN);
                priceHolder.setText("");
                corecctionPriceKn.setText("");
            }
        }

        listAdapter = new RecapitulationArrayAdapter(logsList);
        mainListView.setAdapter(listAdapter);

        //display logs count
        logsCount = (TextView) findViewById(R.id.logsCount);
        logsCount.setText(String.valueOf(logsList.size()));
    }

    private class RecapitulationArrayAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private List<Logs> logsList;

        public RecapitulationArrayAdapter(List<Logs> logsList) {
            inflater = LayoutInflater.from(LogsRecapitulation.this);
            this.logsList = logsList;
        }

        @Override
        public int getCount() {
            return logsList.size();
        }

        @Override
        public Object getItem(int position) {
            return logsList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return logsList.get(position).getId();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.logs_recapitulation, parent, false);
            }
            Logs log = logsList.get(position);
            ((TextView) convertView.findViewById(R.id.rec_log_sort)).setText(log.sort_id);
            ((TextView) convertView.findViewById(R.id.rec_log_class)).setText(log.grade);
            ((TextView) convertView.findViewById(R.id.rec_log_count)).setText(String.valueOf(logsList.size()));
            ((TextView) convertView.findViewById(R.id.rec_logs_mass)).setText(String.format("%.2f m3", log.getM3()));

            if (log.receipt.priceType.equals("Na panju")) {
                ((TextView) convertView.findViewById(R.id.rec_log_price_default)).setText(String.valueOf(log.price.stumpPrice_kn));
            } else {
                ((TextView) convertView.findViewById(R.id.rec_log_price_default)).setText(String.valueOf(log.price.roadPrice_kn));
            }

            if (log.receipt.priceType.equals("Na panju")) {
                ((TextView) convertView.findViewById(R.id.rec_calculated_price)).setText(String.format("%.2f KN", log.price.stumpPrice_kn * log.getM3()));
            } else {
                ((TextView) convertView.findViewById(R.id.rec_calculated_price)).setText(String.format("%.2f KN", log.price.roadPrice_kn * log.getM3()));
            }

            return convertView;
        }
    }

}

I'm using ActiveAndroid and displaying this in listview.

PROBLEM is with displaying these grouped items. I’m displaying them in BaseAdapter listView and it shows me only one item per group but it needs to show me multiple items (because it has more than 4 items in each group).

Can anyone give me advice what should I do to get output like on first image?

question from:https://stackoverflow.com/questions/37529619/how-to-group-items-from-database-and-display-them-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like you are trying to get results similar to the following SQL:

select sort, grade, count(grade), sum(mass), price from logs where receiptid = forwardedId group by sort, grade;

You show the displayed count computed as logList.size(). Instead, if you are going to have the database group the records, you will want to get the count from the database as well.

A couple of things may also help. 1. Verify the SQL query directly against the database to verify you are starting with the intended result set. You can use the sqlite3 client to test assuming you are using an SQLite db. 2. Enable ActiveAndroid logging in ActiveAndroid.initialize

Also you might double check the parameter spelling to make sure you are referencing the intended intent parameter.

Once you verify that the query results are correct, you can focus on whether you are rendering it in the list as you wish.

Finally be wary of doing work on the UI thread.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...