1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help: Java Generic Types

Discussion in 'General Programming Chat' started by RazeByte, Mar 15, 2014.

  1. RazeByte

    RazeByte Newbie

    Joined:
    Mar 13, 2014
    Messages:
    14
    Likes Received:
    3
    So I've just started on Java generic types and I feel like I understand them pretty well. The concept is clear. I've just been recently learning Android Development and I've been taking a look at this piece of code for Files... and I don't understand why the generic type: <String, Integer, String> was used. Can someone help me out?

    How is the AsyncTask<String, Integer, String> being used?
    What is being passed in?

    Code:
    
    other code...
    case R.id.bLoad:
                new loadSomeStuff().execute(FILENAME);
                break;
            }
        }
    
    public class loadSomeStuff extends AsyncTask[U][B]<String, Integer, String>[/B][/U]{        
            protected void onPreExecute(String f) {
                // example of setting up something
                f = "whatever";
            }
    
            //override
            protected String doInBackground(String... arg0) {
                String collected = null;
                FileInputStream fis = null;
                try {
                    fis = openFileInput(FILENAME);
                    byte[] dataArray = new byte[fis.available()];
                    while (fis.read(dataArray) != -1) {
                        collected = new String(dataArray);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        fis.close();
                        return collected;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }
            //... means array
            protected void onProgressUpdate(Integer...progress){
                
            }
            
            protected void onPostExecute(String result) {
                dataResults.setText(result);
            }
            
        }
     
    Last edited: Mar 15, 2014
  2. Chris22

    Chris22 Regular Member

    Joined:
    Sep 29, 2010
    Messages:
    400
    Likes Received:
    1,059