1

I know there are many similar questions and answers here, but nothing worked for my case.

I created a simple JSON list using fragment - according to this tutorial http://androidbeginnerstutorial.blogspot.sk/2015/06/json-parsing-using-fragment-with.html

It is working fine, but I want to have all items from the list clickable, so after click another page appears with some more info from the same JSON.

I tried to implement to ActorAdapter.java inside

public View getView(int position, View convertView, ViewGroup parent) {

I created something like

 itemView.setOnClickListener(new OnClickListener() {
Intent intent = new Intent(context, SingleitemView.class);
})

but not sure what else to put there to get it work.

I have already created the SingleitemView class.

Can you please help me? Thank you.

Here is my code from ActorAdapter.java:

import java.io.InputStream;
import java.util.ArrayList;

import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ListView;

public class ActorAdapter extends ArrayAdapter<Actors> {
    ArrayList<Actors> actorList;
    LayoutInflater vi;
    int Resource;
    ViewHolder holder;


    public ActorAdapter(Context context, int resource, ArrayList<Actors> objects) {
        super(context, resource, objects);
        vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Resource = resource;
        actorList = objects;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // convert view = design
        View v = convertView;
        if (v == null) {
            holder = new ViewHolder();
            v = vi.inflate(Resource, null);
            holder.imageview = (ImageView) v.findViewById(R.id.ivImage);
            holder.tvName = (TextView) v.findViewById(R.id.tvName);
            holder.tvDescription = (TextView) v.findViewById(R.id.tvDescriptionn);
            holder.tvDOB = (TextView) v.findViewById(R.id.tvDateOfBirth);
            holder.tvCountry = (TextView) v.findViewById(R.id.tvCountry);
            holder.tvHeight = (TextView) v.findViewById(R.id.tvHeight);
            holder.tvSpouse = (TextView) v.findViewById(R.id.tvSpouse);
            holder.tvChildren = (TextView) v.findViewById(R.id.tvChildren);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        holder.imageview.setImageResource(R.drawable.ic_launcher);
        new DownloadImageTask(holder.imageview).execute(actorList.get(position).getImage());
        holder.tvName.setText(actorList.get(position).getName());
        holder.tvDescription.setText(actorList.get(position).getDescription());
        holder.tvDOB.setText("B'day: " + actorList.get(position).getDob());
        holder.tvCountry.setText(actorList.get(position).getCountry());
        holder.tvHeight.setText("Height: " + actorList.get(position).getHeight());
        holder.tvSpouse.setText("Spouse: " + actorList.get(position).getSpouse());
        holder.tvChildren.setText("Children: " + actorList.get(position).getChildren());



   v.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        Intent intent = new Intent(view.getContext(), SingleitemView.class);
        intent.putExtra("name", "XXXXXX");
        view.getContext().startActivity(intent);
    }
});

        return v;

    }



    static class ViewHolder {
        public ImageView imageview;
        public TextView tvName;
        public TextView tvDescription;
        public TextView tvDOB;
        public TextView tvCountry;
        public TextView tvHeight;
        public TextView tvSpouse;
        public TextView tvChildren;

    }

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {

            bmImage.setImageBitmap(result);
        }

    }
}

and the SingleitemView.java - that is supposed to be the new activity after click

    import android.app.Activity;
        import android.content.Intent;
        import android.os.Bundle;
        import android.widget.TextView;


public class SingleitemView extends Activity {
// Declare Variables
String name;

@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
    name = i.getStringExtra("name");


    // Locate the TextViews in singleitemview.xml
    TextView txtname = (TextView) findViewById(R.id.name);


    // Set results to the TextViews
    txtname.setText(name);

}

}

Darksymphony
  • 2,155
  • 30
  • 54
  • Consider using an established image loader library like Glide instead of using an AsyncTask to download images – EpicPandaForce Nov 15 '17 at 17:39
  • not sure if it is necessary for such small images. They are loading for no time, so why to use another library to increase the app size and then the images will be cached = more space/memory used. However I tried to use an ImageLoader class with MemoryCache etc., but after it only temp images are showing, not those from json. I'll try to investigate, maybe then I will open another theme for this issue. – Darksymphony Nov 16 '17 at 12:21
  • If you want to keep it small, then you can use Picasso library – EpicPandaForce Nov 16 '17 at 12:25

3 Answers3

0

It looks like You want to open an activity on item click :

itemView.setOnClickListener(new OnClickListener() {
Intent intent = new Intent(context, SingleitemView.class);
context.startActivity(intent);
});

Read more about Intent Here.

ADM
  • 20,406
  • 11
  • 52
  • 83
0

Try this

itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(view.getContext(), SingleitemView.class);                               
            intent.putExtra("name", actorList.get(position).getName());
            view.getContext().startActivity(intent);
        }
    });
Goku
  • 9,102
  • 8
  • 50
  • 81
  • 1
    seems to be on a good way, but I got an error - can't resolve itemView and can't resolve symbol context. Then I replaced itemView with 'v' , as the 'v' is defined above in the public statement View v = convertView; It seems now ok, but the context error is still there – Darksymphony Nov 15 '17 at 11:35
  • this now seems to be without errors, but after I click the item, the app suddenly crashes.Can't be because I use fragments and this starts an activity? Or probably I have wrong SingleitemView.java file. Not sure how to pass the json data to the new activity, without parsing the whole JSON again – Darksymphony Nov 15 '17 at 11:53
  • I updated my question with the code of ActorAdapter and the new activity SingleitemView.java. For now I am trying to display only the Name value from JSON, just to make it work – Darksymphony Nov 15 '17 at 12:05
  • well, I investigated a little bit more, and find out that I didn't have registered the SingleitemView activity in the manifest. Also I made some changes in the code, now it works! After clicking the item it shows me another page with the text XXXXX - because I sent only that text for testing. Now only to figure out, how to pass some strings from JSON: Intent intent = new Intent(view.getContext(), SingleitemView.class); intent.putExtra("name", "XXXXXX"); view.getContext().startActivity(intent); – Darksymphony Nov 15 '17 at 17:38
  • 1
    Finally i found out - intent.putExtra("name", actorList.get(position).getName()); This is working as expected. Thank you very much for helping with the first steps, that helped me very much to proceed! – Darksymphony Nov 15 '17 at 17:56
0

this fixed my issue finally

intent.putExtra("name", actorList.get(position).getName());
Darksymphony
  • 2,155
  • 30
  • 54