-1

everyone.

In my application, there were one public string variable (route_directory) and two button(downloadFolderButton,button_import_csv ).

downloadFolderButton will be pressed first and get data from database. The data will be put into string route_directory. button_import_csv will be pressed afterwards. It will get route_directory to perform other function. The following is my code(Some logic is omitted). However, the eclipse consider the route_directory in second buttion was a undefined class.

import class_1...
import class_2...

public class project extends Activity{
public String A;
public void onCreate(Bundle savedInstanceState){
Button downloadFolderButton = (Button) findViewById(R.id.button_download_folder_id);
    downloadFolderButton.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v) 
        {
            Cursor cur=db.rawQuery("SELECT route as _id,ftp_server_ip,ftp_folder,csv_local_folder,upload_folder,download_folder FROM pda WHERE default_route =?",new String [] {"Y"});
            if (cur.moveToFirst()){
                default_route = cur.getString(cur.getColumnIndex("_id"));
                ftp_server_ip = cur.getString(cur.getColumnIndex("ftp_server_ip"));
                ftp_folder = cur.getString(cur.getColumnIndex("ftp_folder"));
                csv_local_folder = cur.getString(cur.getColumnIndex("csv_local_folder"));
                upload_folder =  cur.getString(cur.getColumnIndex("upload_folder"));
                download_folder = cur.getString(cur.getColumnIndex("download_folder"));

                File directory = Environment.getExternalStorageDirectory();


                route_folder= directory.getPath()+csv_local_folder+"/"+default_route;
                File route_directory = new File(route_folder);

                DeleteRecursive(route_directory);

                route_directory.mkdir();


                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_PICK);

                Uri ftpUri = Uri.parse("ftp://"+ftp_server_ip);
                intent.setDataAndType(ftpUri, "vnd.android.cursor.dir/lysesoft.andftp.uri");
                intent.putExtra("ftp_username", "oracle9i");
                intent.putExtra("ftp_password", "liaixlau");
                intent.putExtra("ftp_pasv", "true");
                intent.putExtra("command_type", "download");
                intent.putExtra("progress_title", "Downloading folder ...");
                intent.putExtra("remote_file1", ftp_folder);        
                intent.putExtra("local_folder", route_folder);
                startActivityForResult(intent, DOWNLOAD_FOLDER_REQUEST);
            }
            cur.close();

        }
    }); 

    Button button_import_csv = (Button) findViewById(R.id.button_import);
    button_import_csv.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){

            db.delete(tableName[0], null, null );
            db.delete(tableName[1], null, null );
            db.delete(tableName[2], null, null );
            db.delete(tableName[3], null, null );
            db.delete(tableName[4], null, null );
            db.delete(tableName[5], null, null );
            db.delete(tableName[6], null, null );
            db.delete(tableName[7], null, null );
            db.delete(tableName[8], null, null );
            db.delete(tableName[9], null, null );
            db.delete(tableName[10], null, null );
            db.delete(tableName[11], null, null );
            db.delete(tableName[12], null, null );
            db.delete(tableName[13], null, null );
            db.delete(tableName[14], null, null );
            db.delete(tableName[15], null, null );
            db.delete(tableName[16], null, null );
            db.delete(tableName[17], null, null );
            db.delete(tableName[18], null, null );
            db.delete(tableName[19], null, null );

            try{

                File[] csvfile = route_directory.listFiles();
                csvfile_string = Arrays.toString(csvfile);

                Log.d("Importcsv",csvfile_string);


                for(int j=0;j<20;j++){

                    InputStreamReader isr = new InputStreamReader(new FileInputStream(csvfile[j]),"BIG5");

                    BufferedReader buffer = new BufferedReader(isr);
                    ContentValues contentValues=new ContentValues();
                    String line = buffer.readLine();            //read first line to get the column
                    String[] cols = line.split("\t");           

                    while ((line = buffer.readLine()) != null) {
                        String[] str = line.split("\t");
                        for (int i = 0; i < cols.length; i++) {
                            str[i] = str[i].replaceAll("\"", "");
                            contentValues.put(cols[i], str[i]);
                            Log.d("ice",cols[i] + "= " + str[i]);
                        }
                        ret = db.insert(tableName[j], null, contentValues);
                        }
                    buffer.close();
                    }
                }catch (IOException e){

            }

        }
    });
}
Helloheyyyy
  • 63
  • 1
  • 1
  • 11
  • dat empty catch block, at least if there was something like `Log.(getClass().getSimpleName(), "Caught IOException in button_import onClick", e);` or something similar. – EpicPandaForce Aug 21 '14 at 09:19
  • @Zhuinden Thank you.How about route_directory value? How can I set route_directory like a Global variable in C program?? – Helloheyyyy Aug 21 '14 at 09:29
  • whoops, that was planning to be `Log.d`. Forgot the important letter that's the actual method call. Global variables in Java are the variables that have the `static` modifier before them. But think about whether you actually need it to be global... because you usually don't. – EpicPandaForce Aug 21 '14 at 09:38
  • @Zhuinden Even I change to static modifier for the variable, the error is still here. – Helloheyyyy Aug 22 '14 at 09:22

1 Answers1

0

You have route_directory defined in an anonymous inner class (for button dowloadFolderButton's onClickListener.onClick(). If you were to define it as a private variable in the Activity you will have visiblity to it in both onClick() functions.

David M
  • 2,511
  • 1
  • 15
  • 18
  • Sorry, how can I achieve this if I want to define the value of route_directory after the dowloadFolderButton is pressed??? – Helloheyyyy Aug 22 '14 at 02:37
  • what difference would it make where it was defined? you should define it with initial value of `null`. give it a value whenever you wish, and grab that value in the other function. good luck – David M Aug 25 '14 at 11:13