String not accessible in same class - java

I have code to show a toast:
public void checkchallenge(View v) {
String algo = null;
if(algo == "123")
{
Context context = getApplicationContext();
CharSequence text = "You have " + messages ;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
...
the error in eclipse that I am getting is:
messages cannot be resolved to a variable
The variable "messages" is being called in some code above it:
String messages = c.getString(TAG_MESSAGES);
final TextView messages1 = (TextView)findViewById(R.id.envelope);
Maybe it's because I don't know Java too well but why isn't my string variable "messages" or "messages1" getting recognized in my code? I have a feeling it has to do with permissions of the code but when I remove the "final" part off of the messages 1 TextView I get the same error.
Confused!
Here is the entire code to the class:
public class Homepage extends Activity {
//URL to get JSON Arrays
public static String url = "http://10.0.2.2/android/SQL.php?username='";
public static String usernamefromlogin;
public static TextView errorchecking;
//JSON Node Names
private static final String TAG_USER = "users";
private static final String TAG_WINS = "wins";
private static final String TAG_MESSAGES = "messages";
private static final String TAG_NAME = "fullname";
private static final String TAG_DISPLAY = "displayname";
private static final String TAG_EMAIL = "email";
private static final String TAG_PW = "password";
private static final String TAG_CREATED = "created_at";
private static final String TAG_UPDATED = "updated_at";
JSONArray user = null;
//disable back button
#Override
public void onBackPressed() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reshomepage);
//Get login name from EditText in login screen and concatenate it to PHP user-name for _GET command in 3 steps.
//Step 1: Get intent from previous activity
Intent intent = getIntent();
getIntent().getExtras();
//Step 2: convert intent (intent) to string called "usernamefromlogin" //error checking in log cat to see value of "usernamefromlogin"
usernamefromlogin = intent.getExtras().getString("username2"); Log.d("log of usernamefromlogin", usernamefromlogin);
//Step 3: take the string "url" and add string "usernamefromlogin" after it
String url5 = url.concat(usernamefromlogin);
String url6 = url5.concat("'");
//find TextView "errorchecking" and send the string "url6" to it so it can display in log cat
Log.d("log of URL6 in it's final state", url6);
// Creating new JSON Parser
JSONParser jParser = new JSONParser();
// Getting JSON from URL from the final string "url6"
JSONObject json = jParser.getJSONFromUrl(url6);
//Logcat check value for TAG_USER
try {
// Getting JSON Array
user = json.getJSONArray(TAG_USER);
JSONObject c = user.getJSONObject(0);
// Storing JSON item in a String Variable
String name = c.getString(TAG_NAME);
String messages = c.getString(TAG_MESSAGES);
String wins = c.getString(TAG_WINS);
String display = c.getString(TAG_DISPLAY);
String email = c.getString(TAG_EMAIL);
String pw = c.getString(TAG_PW);
String created = c.getString(TAG_CREATED);
String updated = c.getString(TAG_UPDATED);
//Importing TextView
final TextView name1 = (TextView)findViewById(R.id.tvfullname);
TextView messages1 = (TextView)findViewById(R.id.envelope);
final TextView wins1 = (TextView)findViewById(R.id.wins);
final TextView created1 = (TextView)findViewById(R.id.tvcreated_at);
final TextView updated1 = (TextView)findViewById(R.id.tvupdated_at);
//Set JSON Data in its respectable TextView
name1.setText("Hello " + name);
updated1.setText("Your last login was " + updated);
// print error if applicable.
} catch (JSONException e) {
e.printStackTrace();
}
}
public void checkchallenge(View v) {
String algo = null;
if(algo == "123")
{
// display pop up message (toast)
Context context = getApplicationContext();
CharSequence text = "You have " + messages1 ;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}else
{
// display pop up message (toast)
Context context = getApplicationContext();
CharSequence text = "You have no new Challenges";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
}

I suspect the code is making the String messages and TextView local variables inside a method. If you want to access these objects within the entire class they should be declared as fields.
public SomeClass{
String messages;
final TextView messages1;
public void checkchallenge(View v) {
//Method implementation
}
public void someOtherMethod(){
this.messages = c.getString(TAG_MESSAGES);
this.messages1 = (TextView)findViewById(R.id.envelope);
}
}

If your messages variable is defined inside other method it won't be seen at the point you showed.

Related

My application crashes after SQLite databases are accessed multiple times. UNIQUE Constraint error

I'm new to Android development and I have been following a tutorial which creates a Twitter application that shows the users timeline and gives them to ability to reply and retweet Tweets for people they follow.
I have completed that tutorial, and I am now trying to implement Direct Messaging into the application. So far I have got my application to display the users timeline and a list of direct messages they have received, but when the user clicks between the main activity which displays their timeline, and their inbox eventually the application crashes.
The error message is as follows:
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: messageInbox._id (code 1555 SQLITE_CONSTRAINT_PRIMARYKEY[1555])
I've searched for the error code and solutions online and so far have no luck with it. I'd be grateful for any help in resolving this, and also any other pointers related to the code I have here as I am new to Java.
It's running on Android 9 and besides some deprecated methods, I have used AndroidX.
The error occurs when the insertOrThrow method below is called in my MessageService class
try{
int count = 50;
DirectMessageList directMessages = messageTwitter.getDirectMessages(count);
for(DirectMessage message : directMessages){
ContentValues messageValues = MessageDataHelper.getValues(message);
messageDB.insertOrThrow("messageInbox", null, messageValues);
messagesChanges = true;
}
} catch (TwitterException te) {
String LOG_TAG = "MessageService";
Log.e(LOG_TAG, "Exception: " + te);
MessageService Class
public class MessageService extends Service {
//twitter authentication key
public final static String TWIT_KEY = "XXXXXXXXXX";
//twitter secret
public final static String TWIT_SECRET = "XXXXXXXXXX";
//app preferences
SharedPreferences bioPrefs;
//twitter object
private Twitter messageTwitter;
//database helper object
private MessageDataHelper messageHelper;
//timeline database
private SQLiteDatabase messageDB;
//handler for updater
public Handler messageHandler;
//delay between fetching new tweets
//private static int mins = 1;//alter to suit
//private static final long FETCH_DELAY = mins * (60*1000);
private static final long FETCH_DELAY = 30000;//Update timeline every 30 seconds
//updater thread object
private MessageUpdater messageUpdater;
#Override
public void onCreate(){
super.onCreate();
//Setting up the class
//get preferences
//shared preferences for user details
bioPrefs = getSharedPreferences("bioPrefs", 0);
//get user preferences
String userToken = bioPrefs.getString("user_token", null);
String userSecret = bioPrefs.getString("user_secret", null);
//get database helper
//database helper object
messageHelper = new MessageDataHelper(this);
//get the database
messageDB = messageHelper.getWritableDatabase();
//create new configuration
Configuration messageConf = new ConfigurationBuilder()
.setOAuthConsumerKey(TWIT_KEY)
.setOAuthConsumerSecret(TWIT_SECRET)
.setOAuthAccessToken(userToken)
.setOAuthAccessTokenSecret(userSecret)
.build();
//instantiate new twitter
messageTwitter = new TwitterFactory(messageConf).getInstance();
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
private class MessageUpdater implements Runnable{
public void run(){
boolean messagesChanges = false;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try{
int count = 50;
DirectMessageList directMessages = messageTwitter.getDirectMessages(count);
for(DirectMessage message : directMessages){
ContentValues messageValues = MessageDataHelper.getValues(message);
messageDB.insertOrThrow("messageInbox", null, messageValues);
messagesChanges = true;
}
} catch (TwitterException te) {
String LOG_TAG = "MessageService";
Log.e(LOG_TAG, "Exception: " + te);}
if(messagesChanges){
sendBroadcast(new Intent("MESSAGE_UPDATES"));
}
messageHandler.postDelayed(this, FETCH_DELAY);
}
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStart(intent, startId);
//get handler
messageHandler = new Handler();
//create an instance of the updater class
messageUpdater = new MessageUpdater();
//add to run queue
messageHandler.post(messageUpdater);
//return sticky
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
//stop the updating
messageHandler.removeCallbacks(messageUpdater);
messageDB.close();
}
}
MessageDataHelper Class
public class MessageDataHelper extends SQLiteOpenHelper {
//Variable Declarations
//db version
private static final int DB_VERSION = 1;
//database name
private static final String DATABASE_NAME = "messageInbox.db";
//ID column
private static final String Home_COL = BaseColumns._ID;
//tweet text
private static final String Update_COL = "message_text";
//twitter screen name
private static final String User_COL = "user_screen";
//time tweeted
private static final String Time_COL = "message_time";
//user profile image
//private static final String User_IMG = "user_img";
//database creation string
private static final String DATABASE_CREATE = "CREATE TABLE messageInbox (" + Home_COL + " INTEGER PRIMARY KEY AUTOINCREMENT, " + Update_COL + " TEXT, "
+ User_COL + " TEXT, " + Time_COL + " INTEGER);";
//" , " +
//+ User_IMG + " TEXT);";
MessageDataHelper(Context context){
super(context, DATABASE_NAME, null, DB_VERSION);
}
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(DATABASE_CREATE);
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS messageInbox");
sqLiteDatabase.execSQL("VACUUM");
onCreate(sqLiteDatabase);
}
static ContentValues getValues(DirectMessage message){
ContentValues messageValues = new ContentValues();
//get the values
try {
//get each value from the table
messageValues.put(Home_COL, message.getId());
messageValues.put(Update_COL, message.getText());
messageValues.put(User_COL, message.getSenderId());
messageValues.put(Time_COL, message.getCreatedAt().getTime());
//noinspection StringOperationCanBeSimplified
//messageValues.put(User_IMG, message.getSender().getProfileImageURL().toString());
}
catch(Exception te) { Log.e("MessageDataHelper", te.getMessage()); }
//return the values
return messageValues;
}
}
MessageActivity
public class MessageActivity extends AppCompatActivity implements View.OnClickListener {
//Variable declarations
//app url
public final static String TWIT_URL = "bioauth://";
//Twitter instance
private Twitter bioMessaging;
//request token for accessing user account
private RequestToken messageRequestToken;
//shared preferences to store user details
private SharedPreferences messagePrefs;
//main view for the inbox
private ListView messageInbox;
//update database
private SQLiteDatabase inboxDB;
//cursor for handling data
private Cursor inboxCursor;
//adapter for mapping data
private MessageAdapter messageAdapter;
//for error logging
private String LOG_TAG = "MessageActivity";
//Broadcast receiver for when new updates are available
private BroadcastReceiver messageStatusReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Sets the inbox layout style using inbox_layout.xml
setContentView(R.layout.inbox_layout);
//setup onclick listener for tweet button
LinearLayout newMessageClicker = findViewById(R.id.newmessagebutton);
newMessageClicker.setOnClickListener(this);
LinearLayout timelineClicker = findViewById(R.id.timelineButton);
timelineClicker.setOnClickListener(this);
//get the preferences for the app
messagePrefs = getSharedPreferences("bioPrefs", 0);
//get user token and secret for authentication
//String userToken = messagePrefs.getString("user_token", null);
//String userSecret = messagePrefs.getString("user_secret", null);
}
//Click listener handles sign in and tweet button presses
public void onClick(View v) {
//find view
switch(v.getId()) {
//user has pressed tweet button
case R.id.newmessagebutton:
//launch tweet activity
startActivity(new Intent(this, DirectMessageClass.class));
break;
case R.id.timelineButton:
//Return to timeline
finish();
//startActivity(new Intent(this, MainActivity.class));
break;
default:
break;
}
}
//setupInbox displays the user's Twitter messages
private void setupInbox() {
//setContentView(R.layout.inbox_layout); //Sets the inbox layout style using inbox_layout.xml
/*//setup onclick listener for tweet button
LinearLayout newMessageClicker = findViewById(R.id.newmessagebutton);
newMessageClicker.setOnClickListener(this);
LinearLayout timelineClicker = findViewById(R.id.timelineButton);
timelineClicker.setOnClickListener(this);*/
//Error catching
try {
//get the inbox
//get reference to the list view
messageInbox = findViewById(R.id.messageList);
//instantiate database helper
//database helper for update data
MessageDataHelper messageHelper = new MessageDataHelper(this);
//get the database
inboxDB = messageHelper.getReadableDatabase();
//query the database, most recent tweets first
inboxCursor = inboxDB.query
("messageInbox", null, null, null, null, null, "message_time DESC");
//manage the updates using a cursor
startManagingCursor(inboxCursor);
//instantiate adapter
messageAdapter = new MessageAdapter(this, inboxCursor);
//this will make the app populate the new update data in the timeline view
messageInbox.setAdapter(messageAdapter);
//instantiate receiver class for finding out when new updates are available
messageStatusReceiver = new TwitterUpdateReceiver();
//register for updates
registerReceiver(messageStatusReceiver, new IntentFilter("MESSAGE_UPDATES"));
//start the Service for updates now
this.getApplicationContext().startService(new Intent(this.getApplicationContext(), MessageService.class));
}
catch(Exception te) {
Log.e(LOG_TAG, "Failed to fetch inbox: " + te.getMessage());
notifyUser("Failed to fetch inbox");
}
}
//Class to implement Broadcast receipt for new updates
class TwitterUpdateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int rowLimit = 50;
if(DatabaseUtils.queryNumEntries(inboxDB, "messageInbox")>rowLimit) {
String deleteQuery = "DELETE FROM messageInbox WHERE "+ BaseColumns._ID+" NOT IN " +
"(SELECT "+BaseColumns._ID+" FROM messageInbox ORDER BY "+"message_time DESC " +
"limit "+rowLimit+")";
inboxDB.execSQL(deleteQuery);
}
inboxCursor = inboxDB.query("messageInbox", null, null, null, null, null, "message_time DESC");
startManagingCursor(inboxCursor);
messageAdapter = new MessageAdapter(context, inboxCursor);
messageInbox.setAdapter(messageAdapter);
}
}
#Override
public void onDestroy() {
super.onDestroy();
try {
//stop the updater Service
stopService(new Intent(this, MessageService.class));
//remove receiver register
unregisterReceiver(messageStatusReceiver);
//close the database
inboxDB.close();
}
catch(Exception se) { Log.e(LOG_TAG, "unable to stop Service or receiver"); }
}
//Method to output inbox_layout to user
private void notifyUser(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
MessageAdapter Class
public class MessageAdapter extends SimpleCursorAdapter {
//Twitter developer key
//private final static String TWIT_KEY = "XXXXXXXXXX";
//Twitter developer secret
//private final static String TWIT_SECRET = "XXXXXXXXXX";
//strings representing database column names to map to views
private static final String[] from = { "message_text", "user_screen",
"message_time" };
//view item IDs for mapping database record values to
private static final int[] to = { R.id.messageText, R.id.userScreen,
R.id.messageTime,};
//constructor sets up adapter, passing 'from' data and 'to' views
MessageAdapter(Context context, Cursor c) {
super(context, R.layout.direct_message, c, from, to);
}
//Bind the data to the visible views
#Override
public void bindView(View row, Context context, Cursor cursor) {
super.bindView(row, context, cursor);
//get the update time
long createdAt = cursor.getLong(cursor.getColumnIndex("message_time"));
//get the update time view
TextView textCreatedAt = row.findViewById(R.id.messageTime);
//adjust the way the time is displayed to make it human-readable
textCreatedAt.setText(DateUtils.getRelativeTimeSpanString(createdAt)+" ");
//get the status ID
long messageID = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));
//get the user name
String messageName = cursor.getString(cursor.getColumnIndex("user_screen"));
//create a StatusData object to store these
StatusData messageData = new StatusData(messageID, messageName);
//set the status data object as tag for reply button in this view
row.findViewById(R.id.messageReply).setTag(messageData);
//setup onclick listeners for the retweet and reply buttons
row.findViewById(R.id.messageReply).setOnClickListener(messageListener);
//setup onclick for the user screen name within the tweet
row.findViewById(R.id.userScreen).setOnClickListener(messageListener);
}
// tweetListener handles clicks of reply and retweet buttons
// also handles clicking the user name within a tweet
private final View.OnClickListener messageListener = new View.OnClickListener() {
//onClick method
public void onClick(View v) {
//which view was clicked
switch (v.getId()) {
//message reply button pressed
case R.id.messageReply:
//implement reply
//create an intent for sending a new tweet
Intent messageReplyIntent = new Intent(v.getContext(), DirectMessageClass.class);
//get the data from the tag within the button view
StatusData messageData = (StatusData) v.getTag();
//pass the status ID
messageReplyIntent.putExtra("tweetID", messageData.getID());
//pass the user name
messageReplyIntent.putExtra("tweetUser", messageData.getUser());
//go to the tweet screen
v.getContext().startActivity(messageReplyIntent);
break;
case R.id.homebutton:
//Add return to timeline method
break;
default:
break;
}
}
};
}
Resolved the issue by using UNIQUE(" + Home_COL +") ON CONFLICT REPLACE when creating the table and insertWithOnConflict instead of insertOrThrow method.

How to send data of radio buttons/radio group using volley when checked

I am doing a register form for an app I'm creating. It includes first name, last name, email, phone number, password, birth day and gender. The gender in a radio group, obviously using radio buttons. I am using Volley for networking data. How do I pass the checked radio button value using volley?
Here is my java code so far:
public class B_Register extends AppCompatActivity implements View.OnClickListener {
public static final String REGISTER_URL = "http://10.0.0.245/register_alex's_app.php";
public static final String FNAME_REGISTER = "first_name";
public static final String LNAME_REGISTER = "last_name";
public static final String EMAIL_REGISTER = "email_register";
public static final String USERNAME_REGISTER = "username_register";
public static final String PHONE_NUMBER_REGISTER = "phone_register";
public static final String PASSWORD_REGISTER = "password_register";
public static final String BDAY_REGISTER = "bday_register";
private EditText fname;
private EditText lname;
private EditText email;
private EditText phone_number;
private EditText new_username;
private EditText new_password;
private EditText bday;
private RadioGroup gender;
private Button register_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b_register);
fname = (EditText)findViewById(R.id.first_name_et);
lname = (EditText)findViewById(R.id.last_name_et);
email = (EditText)findViewById(R.id.email_et);
phone_number = (EditText)findViewById(R.id.phone_et);
new_username = (EditText)findViewById(R.id.username_et);
new_password = (EditText)findViewById(R.id.password_et);
bday = (EditText)findViewById(R.id.bday_et);
gender = (RadioGroup)findViewById(R.id.gender_register);
register_btn = (Button)findViewById(R.id.register_btn);
register_btn.setOnClickListener(this);
}
public void onTermsOfUseClick(View view) {
}
#Override
public void onClick(View view) {
final String firstName = fname.getText().toString().trim();
final String lastName = lname.getText().toString().trim();
final String new_email = email.getText().toString().trim();
final String newPhoneNumber = phone_number.getText().toString().trim();
final String userName = new_username.getText().toString().trim();
final String newPassword = new_password.getText().toString().trim();
final String bDay = bday.getText().toString().trim();
if(TextUtils.isEmpty(firstName)) {
fname.setError("Please enter your first name.");
return;
}else if(TextUtils.isEmpty(lastName)) {
lname.setError("Please enter your last name.");
return;
}else if(TextUtils.isEmpty(new_email)) {
email.setError("Please enter your email.");
return;
}else if(TextUtils.isEmpty(newPhoneNumber)) {
phone_number.setError("Please enter your phone number.");
return;
}else if(TextUtils.isEmpty(userName)) {
new_username.setError("Please enter a valid username, 5-25 characters.");
return;
}else if (TextUtils.isEmpty(newPassword)) {
new_password.setError("Please enter valid password, 5-15 characters.");
}else if (TextUtils.isEmpty(bDay)) {
bday.setError("Please enter your birth day.");
}
}
}
To know the selected RadioButton id check:
// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);
String radio_value = radioButton.getText(); //by this you will get the male or female text which will be used in sending to server.
To send the data using Volley, add dependencies:
compile 'com.android.volley:volley:1.0.0'
Volley method to send data to server:
try {
// initialize request to connect the server, via POST method and Server URl
StringRequest stringRequest = new StringRequest(Request.Method.POST, your_url,
new Response.Listener<String>() {
// catch JSON response from server
#Override
public void onResponse(final String response) {
// response from server
}
},
// catch error response from server
new Response.ErrorListener() {
#Override
public void onErrorResponse(final VolleyError error) {
// trace error message from server
}
}){
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
// map the string value, to send in server
params.put("name",string_value_of_name);
params.put("sex",string_value_of_sex);
.... //like more
return params;
}
};
// execute the request
RequestQueue requestQueue = Volley.newRequestQueue(your_activity_context.this);
requestQueue.add(stringRequest);
}catch (final Exception e){
//catch exception
}
Must visit link for more details Here
Note: This example is just for demonstration purpose and does not contains exact answer asked by user, must apply your idea using shown process/logic.
access the radio button directly, instead of the radiogroup.
RadioButton male = (RadioButton) findViewById(R.id.male);
boolean isMale = male.isChecked();
remember to label your radiobuttons.
<RadioGroup
android:id="#+id/gender_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_male"
android:checked="true" />
<RadioButton
android:id="#+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_female" />
</RadioGroup>

how to request an url with an edittext

i need to find a way to request an url with an edit text and display the json of that url. i have tried to use this code :
// URL to get contacts JSON
private static String id = null;
private static String url = "http://api.ccapp.it/v1/student/" + id + "/schedule/11";
// JSON Node names
private static final String TAG_LESSON = "class";
private static final String TAG_ROOM = "room";
private static final String TAG_TEACHER = "teacher";
// contacts JSONArray
JSONArray contacts = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.roosteralluz);
//number input
final EditText input = (EditText) findViewById(R.id.editText2);
//search button
Button btnSearch = (Button) findViewById(R.id.button34);
btnSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
id = input.getText().toString();
// Calling async task to get json
new GetContacts().execute();
}
});
But when i try that code it returns this error: org.json.JSONException: Value <html><head><title>Slim of type java.lang.String cannot be converted to JSONObject
It is able to parse a link if i change id (look at code) to my own id. but i need to find a user his own id with an edittext.
Instead of using JsonArray, try Gson library to convert from Json to String and vice versa.

Put JSON array data in Hashmap and pass it through Intent Extra

In my app, I have successfully implemented passing JSON Objects via Intent to a new activity by doing find "findviewByid."
Now this is a restaurant finder app, and each restaurant has several menu photos. I was looking all over stackoverflow to find sth like it but couldn't implement.
This is a part of my JSON file:
[
{
login_id: "6",
name: "Urban Spice",
location: "banani",
latitude: "23.790327",
longitude: "90.409007",
address: "House- 119, Road-11, Block-E, Banani",
rating: "4.00",
costfortwopeople: "0",
openingclosingtime: "",
type: "restaurant,ice cream parlour",
perks: "kids zone,home delivery,catering",
cuisine: "indian,indonesian",
phone: "01777899901,2,3,9862672",
image: - [
"http://www.petuuk.com/restaurant_images/img_2146.jpg",
"http://www.petuuk.com/restaurant_images/img_2147.jpg"
],
menu: - [
"http://www.petuuk.com/restaurant_images/.jpg",
"http://www.petuuk.com/restaurant_images/.jpg",
"http://www.petuuk.com/restaurant_images/.jpg",
"http://www.petuuk.com/restaurant_images/.jpg",
"http://www.petuuk.com/restaurant_images/.jpg",
"http://www.petuuk.com/restaurant_images/.jpg",
"http://www.petuuk.com/restaurant_images/.jpg",
"http://www.petuuk.com/restaurant_images/.jpg"
]
},
{
login_id: "7",
name: "The Sky Room Dining",
location: "banani",
latitude: "23.793972",
longitude: "90.403190",
address: "ABC House, 12th Floor, 8 Kemal Ataturk Avenue, Banani",
rating: "4.00",
costfortwopeople: "0",
openingclosingtime: "",
type: "restaurant",
perks: "rooftop view,catering",
cuisine: "thai,indian",
phone: "01675019211,9822017",
image: - [
"http://www.petuuk.com/restaurant_images/img_2204.jpg",
"http://www.petuuk.com/restaurant_images/img_2205.jpg",
"http://www.petuuk.com/restaurant_images/img_2206.jpg"
], etc..................................................................
I'm having a hard time retrieving the JSON array "menu" and "image" from the JSON output as above. I was able to retrieve the other JSON Objects such as login_id, name, location etc.
The main objective I am trying to achieve here is, load all the data in the Listview, where a user can search a restaurant, then when the user clicks on the specific restaurant, all the loaded data should gets into the "Intent.putExtra" for getting viewed in a full restaurant profile view in a new activity.
These are parts of my "SeachAll" activity where I need help. This is the for loop for retrieving data from the JSON file. I need help here retrieving data from "image" and "menu" and then putting it into my hashmap.
protected String doInBackground(String... arg) {
//building parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
//Getting JSON from URL
String json = jsonParser.makeHttpRequest(URL_RESTAURANT_LIST, "GET", params);
//Log Cat Response Check
Log.d("Areas JSON: ", "> " + json);
try {
restaurants = new JSONArray(json);
if (restaurants != null) {
//loop through all restaurants
for (int i = 0; i < restaurants.length(); i++) {
JSONObject c = restaurants.getJSONObject(i);
//Storing each json object in the variable.
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String location = c.getString(TAG_LOCATION);
String rating = c.getString(TAG_RATING);` HashMap<String, String> map = new HashMap<String, String>();
//adding each child node to Hashmap key
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_LOCATION, location);
map.put(TAG_RATING, rating);
//adding HashList to ArrayList
restaurant_list.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}`
This is my onItemClick. Need help in putting the arrays, I dont know if it is alright to pass json array just like json objects i did below.
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), RestaurantProfile.class);
String loginId = ((TextView) view.
findViewById(R.id.login_id)).
getText().toString();
String res_name = ((TextView) view.
findViewById(R.id.restaurant_name)).
getText().toString();
intent.putExtra(TAG_ID, loginId);
intent.putExtra(TAG_NAME, res_name);
startActivity(intent);
}
});
So In brief I need help with two things,
1. Retrieve JSON array "image" and "menu" URLS from the JSON file and put it inside the Hashmap.
2. Put this data to my Intent for being passed to a new activity.
This my full code for "SearchAll" activity.
public class SearchAll extends ListActivity {
ConnectionDetector cd;
AlertDialogManager alert = new AlertDialogManager();
//Progress Dialog
private ProgressDialog pDialog;
//make json parser Object
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> restaurant_list;
//Restaurant Json array
JSONArray restaurants = null;
private static final String URL_RESTAURANT_LIST
= "http://www.petuuk.com/android/allRestaurantList2.php";
//all JSON Node Names
private static final String TAG_ID = "login_id";
private static final String TAG_NAME = "name";
private static final String TAG_LOCATION = "location";
private static final String TAG_LAT = "lattitude";
private static final String TAG_LONG = "longitude";
private static final String TAG_ADDRESS = "address";
private static final String TAG_COST_2 = "costfortwopeople";
private static final String TAG_TYPE = "type";
private static final String TAG_PERKS = "perks";
private static final String TAG_CUISINE = "cuisne";
private static final String TAG_PHONE = "phone";
private static final String TAG_RATING = "rating";
private static final String TAG_IMAGE = "image";
private static final String TAG_MENU = "menu";
private static final String TAG_TIMING = "openingclosingtime";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_all);
cd = new ConnectionDetector(getApplicationContext());
//Check for Internet Connection
if (!cd.isConnectingToInternet()) {
//Internet connection not present
alert.showAlertDialog(SearchAll.this, "Internet Connection Error",
"Please Check Your Internet Connection", false);
//stop executing code by return
return;
}
restaurant_list = new ArrayList<HashMap<String, String>>();
//get ListView
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), RestaurantProfile.class);
String loginId = ((TextView) view.
findViewById(R.id.login_id)).
getText().toString();
String res_name = ((TextView) view.
findViewById(R.id.restaurant_name)).
getText().toString();
intent.putExtra(TAG_ID, loginId);
intent.putExtra(TAG_NAME, res_name);
startActivity(intent);
}
});
lv.setOnScrollListener(new EndlessScrollListener() {
#Override
public void onLoadMore(int page, int totalItemsCount) {
}
});
new LoadRestaurants().execute();
}
class LoadRestaurants extends AsyncTask<String, String, String> {
//Show Progress Dialog
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SearchAll.this);
pDialog.setMessage("Loading All Restaurants...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... arg) {
//building parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
//Getting JSON from URL
String json = jsonParser.makeHttpRequest(URL_RESTAURANT_LIST, "GET", params);
//Log Cat Response Check
Log.d("Areas JSON: ", "> " + json);
try {
restaurants = new JSONArray(json);
if (restaurants != null) {
//loop through all restaurants
for (int i = 0; i < restaurants.length(); i++) {
JSONObject c = restaurants.getJSONObject(i);
//Storing each json object in the variable.
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String location = c.getString(TAG_LOCATION);
String rating = c.getString(TAG_RATING);
String address = c.getString(TAG_ADDRESS);
String latitude = c.getString(TAG_LAT);
String longitude = c.getString(TAG_LONG);
String costfor2 = c.getString(TAG_COST_2);
String timing = c.getString(TAG_TIMING);
String type = c.getString(TAG_TYPE);
String perks = c.getString(TAG_PERKS);
String cuisine = c.getString(TAG_CUISINE);
String phone = c.getString(TAG_PHONE);
JSONArray menuArray = c.getJSONArray("menu");
JSONArray imagesArray = c.getJSONArray("image");
//Creating New Hashmap
HashMap<String, String> map = new HashMap<String, String>();
//adding each child node to Hashmap key
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_LOCATION, location);
map.put(TAG_RATING, rating);
for(int m=0;m<menuArray.length();++m){
map.put("MENU_" + m,menuArray.getString(m));
}//menu for loop
map.put("TOTAL_MENU", menuArray.length());
// map.put(TAG_MENU, String.valueOf(menu));
//adding HashList to ArrayList
restaurant_list.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
//dismiss the dialog
pDialog.dismiss();
//Updating UI from the Background Thread
runOnUiThread(new Runnable() {
#Override
public void run() {
ListAdapter adapter = new SimpleAdapter(
SearchAll.this, restaurant_list,
R.layout.listview_restaurants, new String[]{
TAG_ID, TAG_NAME, TAG_LOCATION, TAG_RATING}, new int[]{
R.id.login_id, R.id.restaurant_name, R.id.location, R.id.rating});
setListAdapter(adapter);
}
});
}
}
}
In short, you don't pass all of your data from one Activity to another. You should just pass a restaurant ID to a new Activity, and it uses that ID to pull data of the restaurant.
You should consider your restaurant list as (part of) Model in an MVC architecture. It should be separated from your Activities (which are Controller). Model is your data expert, it keeps your data in memory, files or a database, and it lives beyound the life-cycle of any particular Activity. You don't pass the Model from one Activity to another. After an Activity is created, it grabs the Model (if the Model is a Singleton) or the Model is injected into that Activity (Dependency Injection, my prefer framework is Dagger). The Activity then can ask for any particular data from the Model and render its View. It can also observe for any further changes within the Model and update its view accordlingly.
not sure of this is exactly what you need, but you might get some ideas out of this
first, to get the image and menu array, from the restaurant, you need this
inside the for loop, where you get the json object (c)
JSONObject c = restaurants.getJSONObject(i);
JsonArray menuArray = c.getJsonArray("menu");
JsonArray imagesArray = c.getJsonArray("image");
and you can loop among menuArray and imagesArray items using a for loop
imagesArray.getString(index);
now, as you have declared your map as < String, String > you can't assign a multiple values (images or menu items) in one string,
so either you find another way to structure your data,
or create another 2 maps, menuPam, imageMap that will have restaurant ID as key, and String as value for menu and image entries.
inside the for loop that read restaurant objects:
for (int i = 0; i < restaurants.length(); i++) {
:
:
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
:
:
JsonArray menuArray = c.getJsonArray("menu");
for(int m=0;m<menuArray.length();++m){
menuMap.add(id,menuArray.getString(m));
}//menu for loop
//another for loop for imageArray...
}//end of restaurants loop
but then you have to add the menuMap and imageMap to an array list, called menus, images...
why don't you create an object to hold all info about restaurant
class restaurant{
private String name="", id =""....
//setters and getters ...
String menuItems[] = null;
String imageItems[] = null;
//setters getters for the arrays.
}
}
EDIT:
this sol does not need a new maps, just add images and menu to same map
using dynamic key name
for(int m=0;m<menuArray.length();++m){
map.add("MENU_" + m,menuArray.getString(m));
}//menu for loop
map.add("TOTAL_MENU", Integer.toString(menuArray.length()));
you can use above code, to add menu items to the map
and same thing to images, "IMAGE_"+m
and TOTAL_IMAGES
now at target activity, read all IMAGE_n and MENU_n in a loop
from 0 to TOTAL_IMAGES, and TOTAL_MENU

Setting outer class variable from anonymous inner class

Hey guys i am trying to export data from a third party app and then set the path of resulting exported file to my textview in main activity.But it isn't working.I searched all over and came to a conclusion that it has something to do with anonymous class,but i am still not able to fix it.Can anyone please guide me.
public class MyActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button btRecieve = (Button) findViewById(R.id.btRecieve);
final Context context = this.getApplicationContext();
final TextView tvFilePath = (TextView) findViewById(R.id.tvFilepath);
final TextView tvFeedBack = (TextView) findViewById(R.id.tvFeedBack);
final String pDateFrom = "2012-07-01";
final String pDateTo = "2012-07-06";
final String pExportType = "e5";
final String pExportFormat = "csv";
btRecieve.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TimeRecordingExport exporter = new TimeRecordingExport(pDateFrom,pDateTo,pExportType,pExportFormat,tvFilePath);
exporter.Export(context);
String path = exporter.getFilePath();
tvFilePath.setText(path);
}
});
}
}
public class TimeRecordingExport{
//private variables
String mDateFrom;
String mDateTo;
String mExportType;
String mExportFormat;
private String mFilepath; //path to the output file
String feedback;
TextView mTv;
File file;
Context mContext;
final String KEY_RESULT_FILE = "com.dynamicg.timerecording.FILE";
//Constructor
public TimeRecordingExport(String pDateFrom,String pDateTo,String pExportType,String pExportFormat,TextView tv){
//Initialize private variables
mDateFrom = pDateFrom;
mDateTo = pDateTo;
mExportFormat = pExportFormat;
mExportType = pExportType;
mTv = tv;
} //End constructor
//Export function
public void Export(Context pContext){
mContext = pContext;
//create a new intent with action export
Intent intent = new Intent("com.dynamicg.timerecording.DATA_EXPORT");
//Add extra values or you could say parameters to this intent.
intent.putExtra("com.dynamicg.timerecording.DATE_FROM",mDateFrom);
intent.putExtra("com.dynamicg.timerecording.DATE_TO",mDateTo);
intent.putExtra("com.dynamicg.timerecording.EXPORT_TYPE",mExportType);
intent.putExtra("com.dynamicg.timerecording.EXPORT_FORMAT",mExportFormat);
//make a broadcast reciever
BroadcastReceiver resultReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent resultIntent) {
Bundle bundle = this.getResultExtras(true);
TimeRecordingExport.this.mFilepath = bundle.getString(KEY_RESULT_FILE); //Path to the created file
//mTv.setText(mFilepath[0]);
file = new File(mFilepath); //New Created file
feedback = "File=["+file+"], canRead=["+file.canRead() //Info about the created file
+"], sizeKB=["+(file.length()/1024)+"]";
//Toast.makeText(mContext, feedback, Toast.LENGTH_LONG).show();
Toast.makeText(context, feedback, Toast.LENGTH_LONG).show();
System.out.println(feedback);
}
};
mContext.sendOrderedBroadcast(intent, null, resultReceiver, null, Activity.RESULT_OK, null, null);
} //End function export
public String getFilePath(){
return mFilepath;
}
public String getFileInfo(){
return feedback;
}
} //End of class
First, have you tried putting a test string in for the path value? Just to be sure it's not your data? Second, guess I've always defined my widgets as class variables of the Activity instead of final variables in the onCreate method.

Categories