I'm trying to display icons for menu items, but I don’t really know how to do it right. The application does the following: There is a json file with objects. Each object consists of three fields: title, url, icon. Through a custom adapter, you need to display data in a ListView. icons are in the drawable folder.

Here is the class code Radio

public class Radio { String title; String subTitle; int image; Radio(String title, String subTitle , int image) { this.title = title; this.subTitle = subTitle; this.image = image; } } 

Here is the RadioAdapter class code

 public class RadioAdapter extends BaseAdapter { Context ctx; LayoutInflater lInflater; ArrayList<Radio> objects; RadioAdapter(Context context, ArrayList<Radio> products) { ctx = context; objects = products; lInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return objects.size(); } @Override public Object getItem(int position) { return objects.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { view = lInflater.inflate(R.layout.item, parent, false); } Radio p = getRadio(position); ((TextView) view.findViewById(R.id.tvDescr)).setText(p.title); ((TextView) view.findViewById(R.id.tvPrice)).setText(p.subTitle + ""); ((ImageView) view.findViewById(R.id.image)).setImageResource(p.image); return view; } Radio getRadio(int position) { return ((Radio) getItem(position)); } } 

MainActivity code

 public class MainActivity extends AppCompatActivity { ArrayList<Radio> products = new ArrayList<>(); RadioAdapter boxAdapter; ListView lvMain; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new ParseTask().execute(); boxAdapter = new RadioAdapter(MainActivity.this, products); lvMain = (ListView) findViewById(R.id.list); lvMain.setAdapter(boxAdapter); } @Override protected void onResume() { super.onResume(); } private class ParseTask extends AsyncTask<Void, Void, String> { BufferedReader reader = null; String resultJson = ""; @Override protected String doInBackground(Void... params) { try { InputStream inputStream = getAssets().open("radios.json"); StringBuffer buffer = new StringBuffer(); reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { buffer.append(line); } resultJson = buffer.toString(); }catch (Exception e) { e.printStackTrace(); } return resultJson; } @Override protected void onPostExecute(String strJson) { super.onPostExecute(strJson); JSONObject dataJsonObj = null; try { dataJsonObj = new JSONObject(strJson); JSONArray radioArray = dataJsonObj.getJSONArray("radio"); for (int i = 0; i < radioArray.length(); i++) { JSONObject radioObject = radioArray.getJSONObject(i); String radioTitle = radioObject.optString("title"); String radioUrl = radioObject.optString("url"); String radioIcon = radioObject.optString("icon"); String img = "R.drawable." + radioIcon; int im = Integer.parseInt(img); products.add(new Radio(radioTitle, radioUrl, im)); } } catch (JSONException e) { e.printStackTrace(); } } } } 

Json content

 { "radio": [ { "title": "Первое", "url": "http://site.ru/1.mp3", "icon": "img1.png" }, { "title": "Второе", "url": "http://site.ru/2.mp3", "icon": "img2.png" }, { "title": "Третье", "url": "http://site.ru/3.mp3", "icon": "img3.png" } ] } 

Stuck here

 String img = "R.drawable." + radioIcon; int im = Integer.parseInt(img); products.add(new Radio(radioTitle, radioUrl, im)); 

I do not know how to slip the icon

  • I understand that so wrong. Tell me how it will be right? - Collective Farmer
  • You need to determine the resource ID by its name. Try Google to solve it, or wait until I get to the computer) - YuriiSPb

1 answer 1

The Radio int image field is a resource identifier (in this case, an image).

You need the resource name to get its ID. This is done as follows:

 Resources resources = context.getResources(); int resId = resources.getIdentifier(name, "drawable", context.getPackageName()); 

where:

  • context - a reference to an activity or to any other object, in the inheritance hierarchy of which there is a class Context .
  • name - the name of the resource.
  • Not really, really. In JSON, it is the file name: "img3.png". A resource will need to search for "img3". - tse
  • in json i can write img3. - Collective Farmer
  • Thanks, it turned out. - Collective Farmer
  • @tse, This is a general solution. In the task of the vehicle, yes, you have to cut the last point and everything after it. - post_zeew