我正在构建一个应用程序,它将在数组中搜索两个或多个字符串并返回与这两个字符串相关的一些信息。我需要在我的 OnClick 中访问名为 title 的变量。这是我的代码:
package com.rottenapi.applogictest;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
public class Main extends Activity {
private static String url = "http://api.rottentomatoes.com/api/public/v1.0/movies/770672123/cast.json?apikey=3p9ehnhzbxwpbd6mk8fncf67";
private static String TAG_CAST = "cast";
private static String TAG_LINKS = "links";
private static String TAG_NAME = "name";
public static String TAG_REL = "rel";
private static String TAG_TITLE = "Title";
EditText enterName;
EditText enterNameTwo;
EditText result;
Button findMovie;
String searchOne;
String searchTwo;
public String title;
List<String> castMembers = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
JSONArray cast = null;
JSONArray rel = null;
JSONArray links = null;
final JSONParser jParser = new JSONParser();
final JSONObject jSon = jParser.getJSONFromUrl(url);
enterName = (EditText) findViewById(R.id.enterName);
enterNameTwo = (EditText) findViewById(R.id.enterNameTwo);
result = (EditText) findViewById(R.id.result);
searchOne = enterName.getText().toString();
searchTwo = enterNameTwo.getText().toString();
try{
cast = jSon.getJSONArray(TAG_CAST);
for(int i=0; i < cast.length(); i++){
JSONObject c = cast.getJSONObject(i);
String name = c.getString(TAG_NAME);
castMembers.add(name);
}
if (castMembers.contains(searchOne) && castMembers.contains(searchTwo)){
links = jSon.getJSONArray(TAG_LINKS);
for(int i=0; i < links.length(); i++){
JSONObject d = links.getJSONObject(i);
String movieInfoLink = d.getString(TAG_REL);
JSONObject jSonMovie = jParser.getJSONFromUrl(movieInfoLink);
rel = jSonMovie.getJSONArray(TAG_REL);
}for(int i =0; i < rel.length(); i++){
JSONObject e = rel.getJSONObject(i);
title = e.getString(TAG_TITLE);
}
findMovie.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("Result contents", title);
result.setText(title);
}
});
}
}
catch(Exception e){
Log.e("Error", e.toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainlayout, menu);
return true;
}
当我运行此应用程序时,应用程序会显示在模拟器中,但是在单击按钮后什么也没有发生,logcat 中也不会打印任何错误。我的结果内容日志也不会在日志中打印出来。有什么想法吗?