如何停止跳过1100帧

时间:2018-02-18 19:10:02

标签: android performance android-layout

我在下面有2个运行搜索功能但我跳过200-1100之间的任何地方。

它使用openLibrary Search API以及在AWS上引用我自己的SQL服务器,它功能齐全,但只是跳过这么多帧

package tk.bookpass.bookpass;
import java.sql.*;
import android.content.*;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.*;
import android.os.*;
import java.io.IOException;
import java.util.*;

public class SearchResults extends AppCompatActivity {
    int size=0;
    String ResultISBN1;
    String ResultISBN2;
    String ResultISBN3;
    ArrayList<String> resultsStore = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_results);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
        StrictMode.setThreadPolicy(policy);

        //seekBar Code
        SeekBar seekBar = findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                double pageNum = size;
                pageNum = Math.floor((pageNum / 3));
                TextView pageNumber = findViewById(R.id.pageNum);
                pageNumber.setText("Page: "+ Integer.toString(seekBar.getProgress())+ " Of " +(int)pageNum);


                displayResults(resultsStore);
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });


        //cardView to details
        CardView cardView1 = findViewById(R.id.cardView1);
        cardView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent regIntent = new Intent(SearchResults.this,BookDetails.class);
                SearchResults.this.startActivity(regIntent);
            }
        });
        CardView cardView2 = findViewById(R.id.cardView2);
        cardView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent regIntent = new Intent(SearchResults.this,BookDetails.class);
                SearchResults.this.startActivity(regIntent);
            }
        });
        CardView cardView3 = findViewById(R.id.cardView3);
        cardView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent regIntent = new Intent(SearchResults.this,BookDetails.class);
                SearchResults.this.startActivity(regIntent);
            }
        });

    //run search

        ImageButton imageButton = findViewById(R.id.imageButton);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent regIntent = new Intent (SearchResults.this, SearchResults.class);
         //start
                size=0;
                startSearch();
            }
        });
    }


    private void startSearch(){

        EditText searchText = findViewById(R.id.searchText);
        String searchingTerm = searchText.getText().toString();

        ArrayList<String> searchStore = new ArrayList<>();
        try{
            searchStore =getSearchResults(searchingTerm);
        }
        catch(IOException e)
        {
                    //do nothing
        }
        resultsStore = checkResults(searchStore);
        size = resultsStore.size();

        setUpSeekBar();
        displayResults(resultsStore);
    }

    private ArrayList<String> checkResults(ArrayList<String> searchStore) {
        LinkedHashSet<String> resultsHash = new LinkedHashSet<String>();
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection(//removed);
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery("select ISBN_fk from Book_Stock");
                while (rs.next())
                    resultsHash.add(rs.getString("ISBN_FK"));

                con.close();

            } catch (Exception e) {
        }
        ArrayList<String> resultsStore = new ArrayList<String>(resultsHash);
        ArrayList<String> duplicatevalues = new ArrayList<String>();
        for (String finalval : resultsStore) {
            if (searchStore.contains(finalval)) {
                duplicatevalues.add(finalval);
            }
        }
        return duplicatevalues;
    }

    private void displayResults(ArrayList<String> resultsStore)
    {
        SeekBar seekBar = findViewById(R.id.seekBar);
        int position = seekBar.getProgress();
        fillCardOne(position, resultsStore);
        fillCardTwo(position, resultsStore);
        fillCardThree(position, resultsStore);

    }

    private void setUpSeekBar() {
        SeekBar seekBar = findViewById(R.id.seekBar);
        seekBar.getThumb();
        seekBar.setProgress(0);
        if (size < 3) {

        }
        else {
            double pageNum = size;
            pageNum = Math.floor((pageNum / 3));
            seekBar.setMax((int)pageNum);
            TextView pageNumber = findViewById(R.id.pageNum);
            pageNumber.setText("Page: "+ Integer.toString(seekBar.getProgress())+ " Of " +(int)pageNum);
            seekBar.setVisibility(seekBar.VISIBLE);
        }
    }

    private void fillCardOne(int position, ArrayList<String> resultsStore){
        int resultsNumber = (0+(position*3));
        String Title=null;
        String Author=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(//removed);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from Book_Details where ISBN = "+resultsStore.get(resultsNumber));
            while (rs.next())
            {
                ResultISBN1 = rs.getString("ISBN");

                Title =rs.getString("Title");
                Author =rs.getString("Author");
            }
            con.close();

        } catch (Exception e) {;
        }

        TextView bookName = findViewById(R.id.bookName);
        bookName.setText("Title: "+Title); //title

        TextView authorName = findViewById(R.id.authorName);
        authorName.setText("Author: "+Author);
    }
    private void fillCardTwo(int position, ArrayList<String> resultsStore){
        int resultsNumber = (1+(position*3));
        String Title=null;
        String Author=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(//removed);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from Book_Details where ISBN = "+resultsStore.get(resultsNumber));
            while (rs.next())
            {
                ResultISBN2 = rs.getString("ISBN");

                Title =rs.getString("Title");
                Author =rs.getString("Author");
            }
            con.close();

        } catch (Exception e) {;
        }

        TextView bookName2 = findViewById(R.id.bookName2);
        bookName2.setText("Title: "+Title); //title

        TextView authorName2 = findViewById(R.id.authorName2);
        authorName2.setText("Author: "+Author);

    }
    private void fillCardThree(int position, ArrayList<String> resultsStore){
        int resultsNumber = (2+(position*3));
        String Title=null;
        String Author=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(//removed);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from Book_Details where ISBN = "+resultsStore.get(resultsNumber));
            while (rs.next())
            {
                ResultISBN3 = rs.getString("ISBN");

                Title =rs.getString("Title");
                Author =rs.getString("Author");
            }
            con.close();

        } catch (Exception e) {;
        }

        TextView bookName3 = findViewById(R.id.bookName3);
        bookName3.setText("Title: "+Title); //title

        TextView authorName3 = findViewById(R.id.authorName3);
        authorName3.setText("Author: "+Author);

    }
    private ArrayList<String> getSearchResults(String searchingTerm)throws IOException
    {
        ArrayList<String> searchStore = new ArrayList<String>();
        searchingTerm =searchingTerm.replace(" ", "%20");
        searchStore = search2.reader(searchingTerm);
        return searchStore;
    }
}

的Class2

package tk.bookpass.bookpass;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import android.util.Log;

public class search2 {
    public static ArrayList<String> reader(String searchingTerm) throws IOException {
        int switches = 0;
        ArrayList<String> searchStore = new ArrayList<String>();
        URL search = new URL("http://openlibrary.org/search.json?q=" + searchingTerm);//search string
        BufferedReader in = new BufferedReader(
                new InputStreamReader(search.openStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) //read file
        {
            if (switches == 0) {
                if (inputLine.contains("\"isbn\": [")) {
                    switches = 1;
                }
            } else if (switches == 1) {
                switches = 0;
                inputLine = inputLine.replace("\"", "");
                inputLine = inputLine.replace(",", "");
                inputLine = inputLine.replace(" ", "");
                String ISBN = "0";
                try {
                        searchStore.add(inputLine);
                } catch (Exception e) {
                    switches = 1; //if the ISBN is of the number letter format run again on the next line
                }
            }
        }
        return searchStore;
    }
}

任何帮助将不胜感激我需要保持搜索功能和相同的基本布局。

1 个答案:

答案 0 :(得分:0)

步骤1:摆脱这些界限:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);

步骤2:通过将网络I / O移动到后台线程来修复出现的NetworkOnMainThreadException崩溃。有关各种解决方案,请参阅this Stack Overflow question,以及有关Android应用开发的任何体面的书籍或课程。

一路上,请重新考虑您对JDBC的使用。这不适用于移动客户端。将数据库包装在Web服务中,然后从Android应用程序中使用该Web服务。