Android:在一个活动中显示两个不同的信息(来自先前活动的数据和来自适配器的信息)

时间:2014-07-20 21:22:37

标签: android listview android-intent adapter

我是Android应用程序的新手,我在这里使用TMDB(Movies API)中的数据。用户可以搜索电影,查看列表结果,然后单击项目以查看有关电影的更多详细信息(例如演员表,故事大纲,图像,标题,评级)其中 - 图像,标题,评级正在起作用,因为它们都来自以前的Web服务请求

https://api.themoviedb.org/3/search/movie?api_key=c47afb8e8b27906bca710175d6e8ba68&query=humpty

但是,然后演员,概要可以通过不同的web请求获得,为此我们需要来自先前请求的字段id(已经得到了这个)。我有关于转换的ArrayList对象中的所有数据(TMDB称为“信用”),

我能够正确显示之前的信息(图片,评级,标题 - 通过调用PutIntentExtra和GetIntentExtra获得),但无法在(标题,图片,评级)下方显示投射信息,而是完全打开新页面。我希望所有数据都在一个视图中显示

1. https://api.themoviedb.org/3/movie/276928/credits?api_key=c47afb8e8b27906bca710175d6e8ba68

2. https://api.themoviedb.org/3/movie/281778?api_key=c47afb8e8b27906bca710175d6e8ba68 

detailed_view.java

public class Detailed_View extends Activity {
// Declare Variables
final String KEY_TITLE = "title";
final String KEY_YEAR = "Year";
final String KEY_RATING = "Rating";
final String KEY_THUMB_URL = "thumb_url";
final String KEY_ID = "id";
final String KEY_CAST = "cast";

ArrayList cast_array_list;

String id;
String cast;
String title;
String rating;
String synopsis;
String credits;
String imgUrl;
ListView list;
LazyDetail adapter;
JSONObject jsonMovieObject;


ImageLoader imageLoader ;

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

    Intent i = getIntent();
    // Get the result of rank
    title = i.getStringExtra("title");
    // Get the result of country
    imageLoader =  new ImageLoader(getApplicationContext());

    synopsis = i.getStringExtra("Rating");
    // Get the result of flag
    credits = i.getStringExtra("credits");
    //get url
    imgUrl = i.getStringExtra("thumb_url");
    id = i.getStringExtra("id");
    Log.d("ids", id );

    TextView txtname = (TextView)findViewById(R.id.name_of_movie);
    TextView txtsynopsis = (TextView)findViewById(R.id.movie_details);
    ImageView bigger_image=(ImageView)findViewById(com.daginge.tmdbsearch.R.id.image_of_movie);         

    // bigger image 

    txtname.setText(title);
    txtsynopsis.setText(synopsis);


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

    new TMDBDetail().execute();

}

public void updateCast(ArrayList<HashMap<String, String>> result) { 
    ListView listView = new ListView(this);
    Log.d("from updateCast function"," "+result.toString()); // got cast

    // Add results to listView.
     adapter = new LazyDetail(this, result);
    listView.setAdapter(adapter);
    // Update Activity to show listView
    setContentView(listView);   

}

public class LazyDetail extends BaseAdapter {
HashMap<String, String> movie = new HashMap<String, String>();

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private  LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyDetail(Activity a, ArrayList<HashMap <String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public LazyDetail(TMDBSearchResultActivity tmdbSearchResultActivity,
        int movie2Result, int rating,
        ArrayList<HashMap<String, String>> result) {
    // TODO Auto-generated constructor stub
}

public int getCount() {
    Log.d("size", data.size()+"");
    return data.size();
}


public long getItemId(int position) {
    return position;
}

public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}



public View getView(final int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(com.daginge.tmdbsearch.R.layout.detailed_view, null);

//      Locate the TextViews in singleitemview.xml
    TextView txtname = (TextView)vi.findViewById(com.daginge.tmdbsearch.R.id.name_of_movie);
    TextView txtsynopsis = (TextView)vi.findViewById(com.daginge.tmdbsearch.R.id.movie_details);
    movie = data.get(position);
  Log.d("movie view detailed", position + movie.toString());
  Log.d("actor", movie.get("cast"));

       // Set results to the TextViews
  txtname.setText(movie.get("cast"));
  txtsynopsis.setText(synopsis);
//      imageLoader.DisplayImage(imgUrl, bigger_image)      

    return vi;
    }
}

private class TMDBDetail extends AsyncTask {

private final String TMDB_API_KEY = "c47afb8e8b27906bca710175d6e8ba68";
private static final String DEBUG_TAG = "TMDBQueryManager";

@Override
protected ArrayList<HashMap <String, String>> doInBackground(Object... params) {
  try {
    return searchMoreDetail ();
  } catch (IOException e) {
      return null;
  }
}

  @Override
  protected void onPostExecute(Object results_Cast) {
//      Log.d("cast - detailed view"," "+results_Cast.toString()); // got cast
cast_array_list = (ArrayList) results_Cast;

updateCast((ArrayList<HashMap<String, String>>) results_Cast);
};

public ArrayList<HashMap<String, String>> searchMoreDetail() throws IOException {

  StringBuilder stringBuilder = new StringBuilder();
  stringBuilder.append("https://api.themoviedb.org/3/movie/"+id+"/credits");
  stringBuilder.append("?api_key=" + TMDB_API_KEY);
  URL url = new URL(stringBuilder.toString());
//      Log.d("urlstring",stringBuilder.toString() );

  InputStream stream = null;
  try {
      // Establish a connection
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setReadTimeout(10000 /* milliseconds */);
      conn.setConnectTimeout(15000 /* milliseconds */);
      conn.setRequestMethod("GET");
      conn.addRequestProperty("Accept", "application/json"); // Required to get TMDB      
      conn.setDoInput(true);
      conn.connect();

      int responseCode = conn.getResponseCode();
Log.d(DEBUG_TAG, "The response code is: " + responseCode + " " +    conn.getResponseMessage());

      stream = conn.getInputStream();
          return parseDetail(stringify(stream));
  } finally {
      if (stream != null) {
          stream.close();
      }
  }
}

private ArrayList<HashMap<String, String>> parseDetail(String result) {
  String streamAsString = result;        


  ArrayList<HashMap<String, String>> results_Cast = new ArrayList<HashMap<String, String>>();
  try {
      JSONObject jsonObject = new JSONObject(streamAsString);
      JSONArray array = (JSONArray) jsonObject.get("cast");
      for (int i = 0; i < array.length(); i++) {
         HashMap <String, String> map = new HashMap <String, String>();
        jsonMovieObject = array.getJSONObject(i);
        map.put(KEY_CAST, jsonMovieObject.getString("name"));                     

          results_Cast.add(map);
      }
  } catch (JSONException e) {
      Log.d("e" , e.toString());
      Log.d(DEBUG_TAG, "Error parsing JSON. String was: " + streamAsString);
  }
Log.d("resulted", results_Cast.toString());
  return results_Cast;
}

public String stringify(InputStream stream) throws IOException, UnsupportedEncodingException {
  Reader reader = null;
  reader = new InputStreamReader(stream, "UTF-8");        
  BufferedReader bufferedReader = new BufferedReader(reader);
  return bufferedReader.readLine();
  }
 }  
}

detailed_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

 <TextView
    android:id="@+id/name_of_movie"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:textSize="20dp"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/image_of_movie"
    android:layout_width="125dp"
    android:layout_height="125dp"
    android:layout_below="@id/name_of_movie"
    android:layout_centerVertical="true"
    android:padding="5dp"
    android:contentDescription="@string/image_of_movie" />

<TextView
    android:id="@+id/movie_details"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/image_of_movie"
    android:layout_below="@id/name_of_movie"  />

<TextView
    android:id="@+id/cast_details"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/image_of_movie"
    android:layout_below="@id/movie_details"  />

</RelativeLayout>

0 个答案:

没有答案