I am building an app that will search an array for two or more strings and return some information related to both strings. I need to access my variable entitled title in my OnClick. Here my code:
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;
}
When I run this the app displays in the emulator however after clicking the button nothing happens also no errors print in the logcat. My results contents log doesnt print out either in the log. Any ideas whats going on?
This is because you haven’t linked your
Button findMovieto the layoutidfor theButton. All you need to do is simply add the following line to your code and it will work:Hope this helps..