.execute 无法解析为某种类型 - AsyncTask (Android)

2022-09-05 00:02:49

我正在编写一个应用程序,需要从我的数据库中获取一些json,我收到数据,但现在我尝试在列表视图中显示的信息旁边查看一个图标。

制造麻烦的线路是:

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

主要活动:

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

下载图像任务:

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

任何帮助都非常感谢!提前致谢!


答案 1

取代

new DownloadImagesTask.execute(mChart); 

new DownloadImagesTask().execute(mChart); 

试试这个。它会起作用。


答案 2

你需要改变

new DownloadImagesTask.execute(mChart);

new DownloadImagesTask().execute(mChart);.

更改后检查,希望它工作正常。快乐编码:)