Solved

Having Big Problems With Android Project

Posted on 2011-09-24
5
260 Views
Last Modified: 2012-06-22
This is my first Android project, and I'm having quite a bit of difficulty with it. First I will attach the code, then ask my questions:

package com.jaylefler.restaurants;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import android.util.Log;


public class RestaurantList extends Activity {
	

	//private ArrayAdapter<String> dataAdapter;
	
	//final ArrayList<String> restaurantArray = new ArrayList<String>();
	
	//ListView myList = (ListView) findViewById(R.id.restaurants);
		
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final EditText restaurantName = (EditText) findViewById(R.id.name);
    	final EditText restaurantAddress = (EditText) findViewById(R.id.addr);
    	final RadioGroup restaurantType = (RadioGroup) findViewById(R.id.types);
    	final Button saveRestaurantInfo = (Button) findViewById(R.id.save);
        
        saveRestaurantInfo.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				
				String restName = restaurantName.getText().toString();
				String restAddr = restaurantAddress.getText().toString();
				String deliveryType = "";
				
				int foodDeliveryType = restaurantType.getCheckedRadioButtonId();
				
				// If text fields are blank or radio button isn't selected clear all fields. 
				if (restName == "" || restAddr == "" || foodDeliveryType == -1) {
					restaurantName.setText("");
					restaurantName.setFocusable(true);
					restaurantAddress.setText("");
					restaurantType.clearCheck();
				}
				// Determine which radio group button has been selected based on the id. 
				else {
					if (foodDeliveryType == 0)
						deliveryType = "Take-Out";
					else if (foodDeliveryType == 1)
						deliveryType = "Sit-Down";
					else 
						deliveryType = "Delivery";
					
					String restaurantInfo = restName + " - " + restAddr + " - " + deliveryType;
					//restaurantArray.add(restaurantInfo);
					Log.d("JAY", "Restaurant Info reads: " + restaurantInfo + ".");
				}
			}
        	
        });
        
                
        
        //dataAdapter = new ArrayAdapter<String>(this, R.id.restaurants, restaurantArray);
        
    }
       // myList.setAdapter(dataAdapter);
}

Open in new window


My first problem is, in my "if (restName == "" || ...." condition, for some reason the program is always performing this condition, even when restName and restAddr aren't blank).

Also, during the debugger phase, on the "restaurantName.setFocusable(true);" line I get a Class File Editor - Source not found error. I have no idea what this means.

I have more then just these problems, but they are keeping me from going further with this project.

Thanks,
AppDevRookie
0
Comment
Question by:InquisitiveProgrammer
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:themrrobert
ID: 36594232
Well your first answer is most likely because you are doing 3 OR comparissons, so if any are true, the code will execute.

If either is blank, OR if foodDeliveryType == -1 then the code runs, so my guess is that one of those is true, the only logical answer.

Check the values of all 3 with the debugger or output each into the log before (and maybe after) running the if check to verify this.

As far as the second error, i've encountered it myself and it's usually due to a configuration error. Only experimenting and becoming very familiar with the plethora of options available to android developers will give you the knowledge needed to figure this out on your quickly, but here are a few sources which could point you in the right direction: (ie, many causes)

http://www.groupsrv.com/computers/about618983.html

Try cleaning/updating the project (under "Android Tools")

Also, ensure that all of the source files have no X icon, and be sure your includes are complete, but most likely its a linking /config error.
0
 
LVL 1

Author Comment

by:InquisitiveProgrammer
ID: 36594247
I have checked the properties while the debugger was running and when none of the conditions were met, the code still ran.

As for the link you provided, that was dated 2009 and the "help" provided was outdated.
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 36594250
I think the logic for the if clause should be:

if ("".equals(restName) || "".equals(restAddr) || foodDeliveryType == -1) {
0
 
LVL 13

Accepted Solution

by:
themrrobert earned 500 total points
ID: 36596093
pk's logic  should work, the java-like framework should let his code work.

However the issue you are facing with the code is the same issue that has existed with eclipse and android sdk since 2009. There isn't one magic solution to make it work.

Even if you posted your entire code in a zip file, I could try to get it to work, and i may or may not have any trouble getting it to work, and if i do encounter your issue, my fix may not be the same fix for you.

if you want to upload your project I am sure i can trace the cause of your #2 issue and get it working on my end, but unless i'm actually working on the project i can only point you in the direction of places to look.

Best of luck :) hope you get it solved, the information is out there, you shouldn't be stuck for long
0
 
LVL 1

Author Comment

by:InquisitiveProgrammer
ID: 36601456
Well, I'm not sure what I did, but I reinstalled Eclipse and the Android SDK and then imported the project and it was working fine.

In order to give points to someone, I hope that someone can answer this question:

I have a RadioGroup called restaurantType that has three buttons inside of it.

I know I can use restaurant.getCheckedRadioButtonId() in order to see if the radio buttons are checked

 
int foodDeliveryType = restaurantType.getCheckedRadioButtonId();
			
				// If any user requested info is missing, clear fields. 
				if (restName.isEmpty() || restAddr.isEmpty() || foodDeliveryType == -1) 
					clearFields();
				else {
					// Determine which radio group button has been selected.
					if (takeOut.isChecked())
						deliveryType = "Take-Out";
					else if (sitDown.isChecked())
						deliveryType = "Sit-Down";
					else 
						deliveryType = "Delivery"; // only other available option

Open in new window


I want to know if I can reference the text fields of the radio buttons without having to reference the buttons themselves. takeOut and sitDown are references to two of the Buttons in the RadioGroup.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question