2014-06-09 14 views
10

sto scrivendo un'app che ha bisogno di ottenere qualche json dal mio DB, ricevo i dati, ma ora sto cercando di visualizzare anche un'icona accanto alle informazioni mostrate in una lista..execute non può essere risolto in un tipo - AsyncTask (Android)

La linea che sta facendo problema è:

mChart.setTag(URL); 
new DownloadImagesTask.execute(mChart); <------ 

MainActivity:

public class MainActivity extends Activity { 

    ListView list; 
    TextView icon; 
    TextView name; 
    TextView developer; 
    TextView size; 

    Button Btngetdata; 

    ArrayList<HashMap<String, String>> mList = new ArrayList<HashMap<String, String>>(); 

    private static String url = "http://appwhittle.com/getdata.php"; 

    private static final String TAG_ITEM = "app_item"; 
    private static final String TAG_ICON = "app_icon"; 
    private static final String TAG_NAME = "app_name"; 
    private static final String TAG_DEVELOPER = "app_developer"; 
    private static final String TAG_SIZE = "app_size"; 

    JSONArray mJsonArray = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mList = new ArrayList<HashMap<String, String>>(); 

     new JSONParse().execute(); 
    } 

    private class JSONParse extends AsyncTask<String, String, JSONObject> { 
     private ProgressDialog pDialog; 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 

      icon = (TextView)findViewById(R.id.icon); 
      name = (TextView)findViewById(R.id.name); 
      size = (TextView)findViewById(R.id.size); 
      developer = (TextView)findViewById(R.id.developer); 

      pDialog = new ProgressDialog(MainActivity.this); 
      pDialog.setMessage("Getting Data ..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     @Override 
     protected JSONObject doInBackground(String... args) { 
     JSONParser jParser = new JSONParser(); 
     // Getting JSON from URL 
     JSONObject json = jParser.getJSONFromUrl(url); 

     return json; 
     } 
     @Override 
     protected void onPostExecute(JSONObject json) { 
     pDialog.dismiss(); 
     try { 
      mJsonArray = json.getJSONArray(TAG_ITEM); 
      for(int i = 0; i < mJsonArray.length(); i++){ 
      JSONObject c = mJsonArray.getJSONObject(i); 

      String name = c.getString(TAG_NAME); 
      String size = c.getString(TAG_SIZE); 
      String developer = c.getString(TAG_DEVELOPER); 
      String icon = c.getString(TAG_ICON); 

      HashMap<String, String> map = new HashMap<String, String>(); 
      map.put(TAG_ICON, icon); 
      map.put(TAG_NAME, name); 
      map.put(TAG_DEVELOPER, "Developer: " + developer); 
      map.put(TAG_SIZE, size + " MB");     


      mList.add(map); 
      list=(ListView)findViewById(R.id.list); 

      ListAdapter adapter = new SimpleAdapter(MainActivity.this, mList, 
       R.layout.list_v, 
       new String[] {TAG_NAME, TAG_DEVELOPER, TAG_SIZE }, new int[] { 
        R.id.name, R.id.developer, R.id.size}); 

      ImageView mChart = (ImageView) findViewById(R.id.icon); 
      String URL = "http://www...anything ..."; 

      mChart.setTag(URL); 
      new DownloadImagesTask.execute(mChart); 

      list.setAdapter(adapter); 
      list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, 
              int position, long id) { 
         Toast.makeText(MainActivity.this, "You Clicked at "+ mList.get(+position).get(TAG_NAME), Toast.LENGTH_SHORT).show(); 
        } 
       }); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     } 
    }   
} 

DownloadImagesTask:

public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> { 

    ImageView imageView = null; 

    @Override 
    protected Bitmap doInBackground(ImageView... imageViews) { 
     this.imageView = imageViews[0]; 
     return download_Image((String)imageView.getTag()); 
    } 

    @Override 
    protected void onPostExecute(Bitmap result) { 
     imageView.setImageBitmap(result); 
    } 


    private Bitmap download_Image(String url) { 
     return null; 
     } 
    } 

Qualsiasi aiuto è molto apprezzato! Grazie in anticipo!

risposta

38

Sostituire

new DownloadImagesTask.execute(mChart); 

con

new DownloadImagesTask().execute(mChart); 

Prova questo. Funzionerà.

+1

Ah, grazie, tipo stupido non ho visto. haha –

0

Cambio:

new DownloadImagesTask.execute(mChart); 

a:

new DownloadImagesTask(mChart).execute(); 
+0

Fornire sempre una (piccola) descrizione della modifica. Questo è contrassegnato dal sistema come un possibile post di bassa qualità. –

0

Non hai inizializzato l'oggetto della vostra DownloadImagesTask chiamando il costruttore.

new DownloadImagesTask().execute(mChart); utilizzare in questo modo. Chiama costruttore predefinito inserendo ().

2

è necessario modificare

new DownloadImagesTask.execute(mChart); 

a

new DownloadImagesTask().execute(mChart);. 

Controllare dopo aver cambiato, spero che funziona bene. Felice codifica :)

0
//Suppose you have a class "DataFetcher" which extends "AsyncTask<Void,Void,Void>{}" 

//to execute this class write the code as follows 

new DataFetcher().execute(); //done