2

I want to display a list of member from a JSON file. Member information like Name, Location, Contact number, image etc. All information is showing fine but Member Image is not showing. Here is given the necessary file and coding for your checking.

JSON file

{"names":[
  {
    "serialno":"1. ",
    "memberimage":"@drawable/tojib",
    "membername":"শেখ তজিবুল ইসলাম",
    "farmacyname":"Farmacy1",
    "mobileno":"01942717067",
    "address":"Digholia, Lohagara, Narial"
  },
  {
    "serialno":"2. ",
    "memberimage":"@drawable/tojib",
    "membername":"শেখ তজিবুল ইসলাম",
    "farmacyname":"Farmacy2",
    "mobileno":"01823987654",
    "address":"Kumri, Lohagara, Narial"
  },
  {
    "serialno":"3. ",
    "memberimage":"@drawable/tojib",
    "membername":"শেখ তজিবুল ইসলাম",
    "farmacyname":"Farmacy3",
    "mobileno":"01782345678",
    "address":"Baira, Lohagara, Narial"
  },
  {
    "serialno":"4. ",
    "memberimage":"@drawable/tojib",
    "membername":"শেখ তজিবুল ইসলাম",
    "farmacyname":"Farmacy4",
    "mobileno":"01943876543",
    "address":"Lutia, Lohagara, Narial"
  }
]
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;
    ListView listview;    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = findViewById(R.id.toolBarId);
        setSupportActionBar(toolbar);
       
        listview=findViewById(R.id.listViewId);

       try {
           JSONObject jsonObject=new JSONObject(loadJsonFile());
           JSONArray jsonArray=jsonObject.getJSONArray("names");
           HashMap<String,String> listItem;           
           ArrayList<HashMap<String,String>> listItems=new ArrayList<>();           
           for(int i=0; i<jsonArray.length();i++)
            {
                JSONObject obj=jsonArray.getJSONObject(i);
                String serialno=obj.getString("serialno");
                String memberimage=obj.getString("memberimage");
                String farmacyname=obj.getString("farmacyname");
                String membername=obj.getString("membername");
                String mobileno=obj.getString("mobileno");
                String address=obj.getString("address");

                listItem=new HashMap<>();
                listItem.put("serialno",serialno);
                listItem.put("memberimage",memberimage);                
                listItem.put("farmacyname",farmacyname);
                listItem.put("membername",membername);
                listItem.put("mobileno",mobileno);
                listItem.put("address",address);
                listItems.add(listItem);                

            }

            ListAdapter adapter=new SimpleAdapter(this,listItems,R.layout.main_list_item_layout,
                    new String[]{"serialno","memberimage","membername","farmacyname","mobileno","address"},
                    new int[]{R.id.serialTextViewId,R.id.imageViewId,R.id.nameTextViewId,
                            R.id.shopNameTextViewId,R.id.mobileNoTextViewId,R.id.addressTextViewId});

            listview.setAdapter(adapter);

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String loadJsonFile() throws IOException {
        String json=null;
        InputStream inputStream=this.getAssets().open("member.json");
        int size=inputStream.available();
        byte[] byteArray=new byte[size];
        inputStream.read(byteArray);
        inputStream.close();
        json=new String(byteArray, "UTF-8");
        return json;
    }
}

Problem: The image is not showing in list. Other information is showing good but Image is blank. Can anyone please help me if I do mistake anywhere.

1 Answer 1

1

When adding the image to the list you want to add the drawble id and not the drawable string i.e "@drawable/tojib" like below:

String memberImageDrawable = obj.getString("memberimage");
memberImageDrawable = memberImageDrawable.substring(memberImageDrawable.indexOf("/")); //extract the String after @drawable/
String memberimage = Integer.toString(getApplicationContext().getResources().getIdentifier(memberImageDrawable, "drawable", getApplicationContext().getPackageName()));
Sign up to request clarification or add additional context in comments.

5 Comments

Dear David, Thank you very much for answering. Unfortunately the issue remaining same!!. Can you please check the JSON file if I am correct to set the value of memberimage it should be @drawable/tojib or R.drawable.tojib . I think somewhere I am doing a little mistake.
It should be the drawable ID which I tried to show a solution to get it from a String, if you try String memberimage = Integer.toString(getApplicationContext().getResources().getIdentifier("tojib:, "drawable", getApplicationContext().getPackageName())); does it work
Dear David, showing syntax error for getIdentifier parameter. Can you please check
Sorry never closed quotes String memberimage = Integer.toString(getApplicationContext().getResources().getIdentifier("tojib", "drawable", getApplicationContext().getPackageName()));
Excellent David !! It is working fine, the issue is resolved. I passed a couple of days for this problem. Now got solution. Thank you very much for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.