Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

At the "if(a==2)" part of my code below, the android emulator takes 8 seconds to process and display some data in a list. I need to reduce this time to 1 second or less. I am accessing a 50 KB .txt file with 3200 lines, comparing each line with a string passed by another function, and whichever line matches the string, I am printing that in a list. The format of data in the .txt file is like this:

0,1>Autauga;     
0,2>Baldwin;      
0,3>Barbour;         
1,69>Aleutians East;      
1,68>Aleutians West;

etc... and it goes on for 3200 lines. The number before the comma is compared to the string I passed from another function. If they match, I print the line. Here is the code:

package com.example.countylists;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ListViewA extends Activity{

    List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
    int a=1;//stores instance number of the list
    /** Called when the activity is first created. */
    @Override    
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView lv= (ListView)findViewById(R.id.listview);
        String newString;//item clicked in previous list
        int instanceposition;//tells me which instance number is running
        int position=0;//position of the click
        Bundle extras;
        if (savedInstanceState == null) {
            extras = getIntent().getExtras();
            if(extras == null) {
                newString= null;
                instanceposition=1;
                Log.d(Integer.toString(instanceposition),"value of instanceposition");
                a=instanceposition;
                position=0;

            } else {
                newString= extras.getString("Key");
                instanceposition=extras.getInt("instpos");
                Log.d(Integer.toString(instanceposition),"value of instanceposition");
                a=instanceposition;
                position=extras.getInt("position");

            }
        } else {
            newString= (String) savedInstanceState.getSerializable("Key");
        }

        // create the grid item mapping
        String[] from = new String[] {"col_1"};
        int[] to = new int[] {R.id.item2};
        String[] array;
        Log.d(Integer.toString(a),"value of a");
//list of states
        if(a==1)
        {   String z="";
            try{
                InputStream is = getAssets().open("USSTATES.txt");
                InputStreamReader iz=new InputStreamReader(is);
                BufferedReader bis = new BufferedReader(iz);

            int v=0;
            v=count("USSTATES.txt");
            Log.d(Integer.toString(v),"value of v");
            array=new String[v];
        for(int i=0;i<v;i++){
            z=bis.readLine();
            array[i]=z;
        }
     // prepare the list of all records
        for(int q = 0; q <v; q++){
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("col_1", array[q]);
            fillMaps.add(map);
            lv.setOnItemClickListener(onListClick);
        }
        }catch(Exception E){E.printStackTrace();
}
        }
        else if(a==2){
            String z="";
            try{
                InputStream is = getAssets().open("USCOUNTIES.txt");
                InputStreamReader iz=new InputStreamReader(is);
                BufferedReader bis = new BufferedReader(iz);

                int v=0;
                //finding no. of counties to be displayed
                v=count("USCOUNTIES.txt");

                int counter=0;
                String pos;
                pos=Integer.toString(position);
                try{
                for(int i=0;i<v;i++){
                    z=bis.readLine();
                    //int x=pos.length();
                    boolean a;
                    //using stringtokenizer
                    StringTokenizer st = new StringTokenizer(z, ","); 
                    String substring;
                    substring=(String) st.nextElement();
                    a=substring.equals(pos);
                    if(a==true){

                        counter=counter+1;

                    }
                }}catch(Exception e){e.printStackTrace();}
                String array1[]=new String[counter];

                try{
                    InputStream ig = getAssets().open("USCOUNTIES.txt");
                    InputStreamReader ia=new InputStreamReader(ig);
                    BufferedReader bos = new BufferedReader(ia);
                int j=0;
                for(int i=0;i<v;i++){
                    z=bos.readLine();
                    Log.d(z,"Value of zyo");
                    String[] split = z.split(",");
                    if(split[0].equals(pos)){
                        array1[j]=split[1];
                        j=j+1;
                    }

                }}
                catch(Exception e){e.printStackTrace();}

            Log.d(Integer.toString(v),"value of v(when a is 1)");

     // prepare the list of all records
        for(int q = 0; q <v; q++){
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("col_1", array1[q]);
            fillMaps.add(map);
            lv.setOnItemClickListener(onListClick);
        }
        }catch(Exception E){E.printStackTrace();
}   
        }

        a=a+1;
        // fill in the grid_item layout
        SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
        lv.setAdapter(adapter);
    }
    private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener(){
    public void onItemClick(AdapterView<?> parent,View view, int position, long id)
    {
        Intent i=new Intent(ListViewA.this,ListViewA.class);
        i.putExtra("Key",fillMaps.get(position));
        i.putExtra("instpos", a);
        i.putExtra("position",position);
        startActivity(i);
    }
};
public int count(String filename) throws IOException {
    InputStream is = getAssets().open(filename);
    BufferedInputStream bis = new BufferedInputStream(is);
    try {
        byte[] c = new byte[1024];
        int count = 0;
        int readChars = 0;
        boolean empty = true;
        while ((readChars = bis.read(c)) != -1) {
            empty = false;
            for (int i = 0; i < readChars; ++i) {
                if (c[i] == '\n') {
                    ++count;
                }
            }
        }
        return (count == 0 && !empty) ? 1 : count+1;
    } finally {
        bis.close();
    }
}
share|improve this question
onCreate could really do with some splitting up into separate methods. – Daniel Imms Mar 17 at 4:09
@Tyriar Will that give it a time advantage or just improve the cosmetic looks of the code? – ankit rawat Mar 17 at 9:00
No impact on performance, but it's more modular and therefore maintainable and readable. Currently your code says onCreate "do a bunch of stuff", calling methods name that stuff. – Daniel Imms Mar 17 at 9:42
It looks like you are going to read the same file 3 times. Please provide a small and clear code (the relevant part of the code) and an written example, so one could be able to see the problem without spending a lot of time figuring out what is happening or should happen. – tb- Mar 20 at 2:03
as @tb- said please provide code with more comments and state your requirement clearly. – minhaz Apr 24 at 4:01

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.