0

I want to load an image from a JSON file after clicking on an item of list loaded from this file into an activity. To show the image and it's description.

It loads the activity and shows the image but not it's description. there is nthing in the Logcat.

east.java

@SuppressLint("NewApi")  
public class east extends Fragment {

    ListView list;
    TextView id;
    TextView name;
    Button Btngetdata;
    east adaptor;

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



    //URL to get JSON Array
    private static String url = "http://ra.com/s/east.php";

    //JSON Node Names
    private static final String TAG_ARRAY = "coffee";
    private static final String TAG_ID = "ID";
    private static final String TAG_NAME = "name";
    private static final String TAG_adress = "adress";
    private static final String TAG_IMAGE = "image";

    JSONArray coffee = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.east_fragment, container, false);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        oslist = new ArrayList<HashMap<String, String>>();
        Btngetdata = (Button)getView().findViewById(R.id.getdata);
        Btngetdata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new JSONParse().execute();  
                Btngetdata.setVisibility(View.GONE);
            }
        });

    }

    private class JSONParse extends AsyncTask<String, String, JSONObject> {

        private ProgressDialog pDialog;
        JsonParser jParser = new JsonParser();
        private JSONObject json;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            name = (TextView)getView().findViewById(R.id.name);
            id = (TextView)getView().findViewById(R.id.id);

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


        }

        @Override
        protected JSONObject doInBackground(String... args) {

            // Getting JSON from URL
            json = jParser.getJSONFromUrl(url);

            return json;
        }



        @Override
        protected void onPostExecute(JSONObject json) {

            pDialog.dismiss();
            try {
                 // Getting JSON Array from URL
                 coffee = json.getJSONArray(TAG_ARRAY);

                 for(int i = 0; i < coffee.length(); i++){

                     JSONObject c = coffee.getJSONObject(i);

                     // Storing  JSON item in a Variable
                     String ID = null;
                     String name = null;
                     String adress = null;
                     String image = null;
                     try {
                        ID = new String(c.getString(TAG_ID).getBytes("ISO-8859-1"), "utf8");
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                     try {
                        name = new String(c.getString(TAG_NAME).getBytes("ISO-8859-1"), "utf8");
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                     try {
                        adress = new String(c.getString(TAG_adress).getBytes("ISO-8859-1"), "utf8");
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                     try {
                            image = new String(c.getString(TAG_IMAGE).getBytes("ISO-8859-1"), "utf8");
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }


                     // Adding value HashMap key => value
                     HashMap<String, String> map = new HashMap<String, String>();
                     map.put(TAG_ID, ID);
                     map.put(TAG_NAME, name);
                     map.put(TAG_adress, adress);
                     map.put(TAG_IMAGE, image);

                     oslist.add(map);

                     list = (ListView)getView().findViewById(R.id.listView1);

                     ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
                         R.layout.listview,
                         new String[] { TAG_ID, TAG_NAME, TAG_adress }, new int[] {
                             R.id.id, R.id.name, R.id.adress});
                     list.setAdapter(adapter);
                     list.setOnItemClickListener(new AdapterView.OnItemClickListener() {


                        @Override
                         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Intent intent = new Intent(getActivity(), SingleItemView.class); 
                                // Pass all data rank
                                intent.putExtra("rank", oslist.get(+position).get("TAG_ID"));
                                // Pass all data country
                                intent.putExtra("country", oslist.get(+position).get("TAG_NAME"));
                                // Pass all data population
                                intent.putExtra("population", oslist.get(+position).get("TAG_adress"));
                                // Pass all data flag
                                intent.putExtra("flag", oslist.get(+position).get("image"));
                                // Start SingleItemView Class
                                startActivity(intent);
                         }
                     });
                     }
            } catch (JSONException e) {
              e.printStackTrace();
            }
        }
    }
}

SingleItemView.java

public class SingleItemView extends Activity {
// Declare Variables
String rank;
String country;
String population;
String flag;
String position;
ImageLoader imageLoader = new ImageLoader(this);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from singleitemview.xml
    setContentView(R.layout.singleitemview);

    Intent i = getIntent();
    // Get the result of rank
    rank = i.getStringExtra("rank");
    // Get the result of country
    country = i.getStringExtra("country");
    // Get the result of population
    population = i.getStringExtra("population");
    // Get the result of flag
    flag = i.getStringExtra("flag");

    // Locate the TextViews in singleitemview.xml
    TextView txtrank = (TextView) findViewById(R.id.rank);
    TextView txtcountry = (TextView) findViewById(R.id.country);
    TextView txtpopulation = (TextView) findViewById(R.id.population);

    // Locate the ImageView in singleitemview.xml
    ImageView imgflag = (ImageView) findViewById(R.id.flag);

    // Set results to the TextViews
    txtrank.setText(rank);
    txtcountry.setText(country);
    txtpopulation.setText(population);

    int loader = R.drawable.loader;

    // Capture position and set results to the ImageView
    // Passes flag images URL into ImageLoader.class
    imageLoader.DisplayImage(flag, loader, imgflag);
}
}

I'm newbie and it's my first app. I'll be glad if you give me a solution to solve this problem.

1 Answer 1

0

You are passing NULL as context in east.java

2
  • I corrected that and it loads the activity but it doesn't show passed text and image. I have updated question with new Logcat and updated codes. THX
    – alireza
    Apr 8, 2015 at 20:08
  • I changed the image direction in server and it load the image now. But it still doesn't show it's description.
    – alireza
    Apr 8, 2015 at 20:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.