无法在自定义数组适配器中传递意图

时间:2016-07-07 12:12:31

标签: java android android-intent

在这种情况下,我无法传递意图。屏幕上没有显示任何内容或 movie_detail.java 中的日志。我想不出帽子有问题,请帮忙!

MainActivityFragment.java

package com.example.coderahul.a9to12;

import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import java.util.Arrays;

public class MainActivityFragment extends Fragment {

private movieListAdapter movieListA;

MovieList[] movieLists = {
        new MovieList("Cupcake - 1.5", "/is6QqgiPQlI3Wmk0bovqUFKM56B.jpg", "368596"),
        new MovieList("Donut - 1.6", "/cGOPbv9wA5gEejkUN892JrveARt.jpg", "209112"),
        new MovieList("Eclair - 2.0-2.1", "/9KQX22BeFzuNM66pBA6JbiaJ7Mi.jpg", "47933"),
        new MovieList("Froyo - 2.2-2.2.3", "/z09QAf8WbZncbitewNk6lKYMZsh.jpg", "127380"),
        new MovieList("GingerBread - 2.3-2.3.7", "/5N20rQURev5CNDcMjHVUZhpoCNC.jpg", "271110"),
        new MovieList("Honeycomb - 3.0-3.2.6", "/jjBgi2r5cRt36xF6iNUEhzscEcb.jpg", "135397"),
        new MovieList("Ice Cream Sandwich - 4.0-4.0.4", "/6FxOPJ9Ysilpq0IgkrMJ7PubFhq.jpg", "258489"),
        new MovieList("Jelly Bean - 4.1-4.3.1", "/tSFBh9Ayn5uiwbUK9HvD2lrRgaQ.jpg", "262504"),
        new MovieList("Honeycomb - 3.0-3.2.6", "/inVq3FRqcYIRl2la8iZikYYxFNR.jpg", "87101"),
        new MovieList("Ice Cream Sandwich - 4.0-4.0.4", "/zSouWWrySXshPCT4t3UKCQGayyo.jpg", "293660"),
        new MovieList("Jelly Bean - 4.1-4.3.1", "/MZFPacfKzgisnPoJIPEFZUXBBT.jpg", "76341"),
        new MovieList("Jelly Bean - 4.1-4.3.1", "/sM33SANp9z6rXW8Itn7NnG1GOEs.jpg", "246655")
};

public MainActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    movieListA = new movieListAdapter(getActivity(), Arrays.asList(movieLists));

    // Get a reference to the ListView, and attach this adapter to it.
    GridView gridView = (GridView) rootView.findViewById(R.id.movies_grid);
    gridView.setAdapter(movieListA);

    final Context context = getActivity();

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            MovieList detail = (MovieList) movieListA.getItem(position);
            Intent intent = new Intent(context, movie_detail.class)
                    .putExtra(Intent.EXTRA_TEXT, detail.title);
            context.startActivity(intent);
            Log.v("Check", detail.title);
        }
    });

    return rootView;
}
}

movie_detail.java

package com.example.coderahul.a9to12;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class movie_detail extends AppCompatActivity {

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

public static class DetailFragment extends Fragment {

    public DetailFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.movie_detail, container, false);
        Log.v("Check", "Inside Intent");
        // The detail Activity called via intent.  Inspect the intent for forecast data.
        Intent intent = getActivity().getIntent();
        if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
            String detail = intent.getStringExtra(Intent.EXTRA_TEXT);
            Log.v("Check", detail);
            ((TextView) rootView.findViewById(R.id.movie_detail))
                    .setText(detail);
        }

        return rootView;
    }
}

}

movieListAdapter.java

package com.example.coderahul.a9to12;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

public class movieListAdapter extends ArrayAdapter<MovieList> {
private static final String LOG_TAG = movieListAdapter.class.getSimpleName();
/**
 * This is our own custom constructor (it doesn't mirror a superclass constructor).
 * The context is used to inflate the layout file, and the List is the data we want
 * to populate into the lists
 *
 * @param context        The current context. Used to inflate the layout file.
A List of AndroidFlavor objects to display in a list
 */
public movieListAdapter(Activity context, List<MovieList> movieList) {
    // Here, we initialize the ArrayAdapter's internal storage for the context and the list.
    // the second argument is used when the ArrayAdapter is populating a single TextView.
    // Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
    // going to use this second argument, so it can be any value. Here, we used 0.
    super(context, 0, movieList);
}

/**
 * Provides a view for an AdapterView (ListView, GridView, etc.)
 *
 * @param position    The AdapterView position that is requesting a view
 * @param convertView The recycled view to populate.
 *                    (search online for "android view recycling" to learn more)
 * @param parent The parent ViewGroup that is used for inflation.
 * @return The View for the position in the AdapterView.
 */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Gets the AndroidFlavor object from the ArrayAdapter at the appropriate position
    MovieList movieList = getItem(position);

    // Adapters recycle views to AdapterViews.
    // If this is a new View object we're getting, then inflate the layout.
    // If not, this view already has the layout inflated from a previous call to getView,
    // and we modify the View widgets as usual.
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(
                R.layout.movie_item, parent, false);
    }

    ImageView iconView = (ImageView) convertView.findViewById(R.id.movie_image);

    Picasso.with(getContext())
            .load("http://image.tmdb.org/t/p/w185//" + movieList.link)
            .resize(200, 200)
            .centerCrop()
            .into(iconView);

    TextView versionNameView = (TextView) convertView.findViewById(R.id.movie_title);
    versionNameView.setText(movieList.title);

    return convertView;
}
}

2 个答案:

答案 0 :(得分:0)

您必须将片段附加到您的活动的布局中,以便让您在活动中看到片段

你的movie_detail活动中的

@Override
 protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.movie_detail);
      Intent intent = getActivity().getIntent();
      if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
          String detail = intent.getStringExtra(Intent.EXTRA_TEXT);
          Log.v("Check", detail);
      }
      Bundle extra = new Bundle();
      extra.putExtra(Intent.EXTRA_TEXT,detail);
      FragmentManager fragmentManager = getFragmentManager();
      FragmentTransaction fragmentTransaction = 
      fragmentManager.beginTransaction();
      DetailFragment fragment = new DetailFragment();
      fragment.setArguments(extra);
     fragmentTransaction.replace(android.R.id.content, fragment);
}

在你的片段中获取参数并在必要时设置细节

答案 1 :(得分:0)

您可以使用活动 onCreate()方法获取您的意图,而不是片段 onCreateView()方法,因为意图会被活动而不是片段接受。 要在movie_detail活动中获得您的意图,请执行以下操作

static String detail="";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.movie_detail);
    Intent intent =getIntent();
        if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
            detail = intent.getStringExtra(Intent.EXTRA_TEXT);

        }

}

执行此操作后,您可以使用detail String在文本视图中设置文字

在你的片段类中

((TextView) rootView.findViewById(R.id.movie_detail))
                    .setText(movie_detail.detail);