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();
}
}