JAVA,ANDROID:setImageResource(int)不显示图像

时间:2018-02-08 12:18:00

标签: java android android-fragments android-asynctask drawable

我有一个代码,应该在银行中显示不同的图像中的每个项目,如果它找不到一个:显示默认值。 我有一切工作,除了问题是它不显示drawables。我该如何解决?

我的代码:

  public class BankViewFragment extends OSRSFragment {
    private static final String TAG = "BankViewFragment";

    private static Account account;
    private ListView lv;
    Handler handler;

    ArrayList<HashMap<String, String>> ItemList;

    public static BankViewFragment newInstance(final Account account) {
        BankViewFragment fragment = new BankViewFragment();
        Bundle b = new Bundle();
        fragment.setArguments(b);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.bank_view, null);
        ItemList = new ArrayList<>();
        new GetItems().execute();
        lv = view.findViewById(R.id.list);
        handler = new Handler(Looper.getMainLooper());
        return view;
    }

    public static int getResId(String resourceName, Class<?> c) {
        try {
            Field idField = c.getDeclaredField(resourceName);
            return idField.getInt(idField);
        } catch (Exception e) {
            throw new RuntimeException("No resource ID found for: "
                    + resourceName + " / " + c, e);
        }
    }

    private class GetItems extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();
            SharedPreferences sharedpreferences = getContext().getSharedPreferences("minescape", Context.MODE_PRIVATE);
            String nikas = sharedpreferences.getString("bankname",  "null");
            String url = "https://api.minesca.pe/game/classic/stats?username=" + nikas;
            String jsonStr = sh.makeServiceCall(url);

            Log.e(TAG, "NIKAS: " + nikas);
            Log.e(TAG, "ACCOUNT: " + account);
            Log.e(TAG, "Response from url: " + jsonStr);
            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    JSONObject items = jsonObj.getJSONObject("bank");
                    Iterator keys = items.keys();
                    while(keys.hasNext()) {
                        String dynamicKey = (String)keys.next();
                        JSONObject line = items.getJSONObject(dynamicKey);
                        String item = line.getString("item");
                        //Integer image = getResId(item, Drawable.class);
                        final Integer image = getResources().getIdentifier(item, "drawable", getActivity().getPackageName());
                        String amount = line.getString("amount");
                        Log.e(TAG, "DAIKTAS: " + item);
                        Log.e(TAG, "KIEKIS: " + amount);
                        HashMap<String, String> contact = new HashMap<>();
                        String itembank = item.replaceAll("i_", "");
                        String itembanks = itembank.replaceAll("_", " ");
                        contact.put("name", itembanks);
                        contact.put("email", amount);
                        LayoutInflater inflater = LayoutInflater.from(getContext());
                        View view = inflater.inflate(R.layout.list_item, null);
                      //  lv = view.findViewById(R.id.list);
                       // iv = (ImageView) view.findViewById(R.id.logo);
                        final ImageView ims = (ImageView) view.findViewById(R.id.logo);
                        handler.post(new Runnable() {
                            public void run() {
                                if(image != null) {
                                    if(image == 0) {
                                        ims.setImageResource(R.drawable.i_noted);
                                        Log.e(TAG, "rokas?: " + image);
                                    } else {
                                        Log.e(TAG, "drawable ID ID: " + image);
                                        ims.setImageResource(image);
                                    }
                                } else {
                                    Log.e(TAG, "null?: " + image);
                                }
                            }
                        });
                        ItemList.add(contact);
                    }
                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }
                    };

                }

            } else {
                Log.e(TAG, "Couldn't get json from server.");
                new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getContext(),
                                "Couldn't get json from server!",
                                Toast.LENGTH_LONG).show();
                    }
                };
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            ListAdapter adapter = new SimpleAdapter(getContext(), ItemList,
                    R.layout.list_item, new String[]{ "email","name"},
                    new int[]{R.id.email, R.id.name});
            lv.setAdapter(adapter);
        }
    }
    }

Logcat不断向我发送消息: 02-08 14:11:12.331 17584-17584/com.infonuascape.osrshelper W/Resources: Converting to string: TypedValue{t=0x5/d=0x601 a=1 r=0x10500d7}

1 个答案:

答案 0 :(得分:0)

你应该选择正确的二传手:

  • imageView.setImageDrawable(drawable) - 设置您可以获得的drawable 资源使用getResource().getDrawable(R.drawable.drawable_id)

  • imageView.setImageResource(id) - 使用资源ID设置图片。

  • imageView.setImageBitmap(bitmap) - 设置您可以使用以下内容从drawable创建的位图:Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.drawable_id)

如果您使用getResources().getIdentifier(...);,我建议您使用:

int imageResource = getResources().getIdentifier(item, "drawable", getActivity().getPackageName());
imageview= (ImageView)findViewById(R.id.imageView);
Drawable drawable = getResources().getDrawable(imageResource);
imageView.setImageDrawable(drawable );  

像这样......