Android - 来自 JSON 的可点击项目

Android - clickable items from JSON

我知道这里有很多类似的问题和答案,但对我的情况没有任何帮助。

我使用片段创建了一个简单的 JSON 列表 - 根据本教程 http://androidbeginnerstutorial.blogspot.sk/2015/06/json-parsing-using-fragment-with.html

它工作正常,但我想让列表中的所有项目都可以点击,所以点击后会出现另一个页面,其中包含来自同一 JSON 的更多信息。

我尝试在ActorAdapter.java里面实现

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

我创建了类似

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

但不确定还有什么可以让它发挥作用。

我已经创建了 SingleitemView class。

你能帮帮我吗?谢谢。

这是我来自 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);
        }

    }
}

和 SingleitemView.java - 这应该是点击后的新 activity

    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);

}

}

您似乎想在项目点击时打开 activity:

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

详细了解 Intent Here

试试这个

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);
        }
    });

这终于解决了我的问题

intent.putExtra("name", actorList.get(position).getName());