[Q] Mass change the object access Android Studio - Android Studio

I've been using Android Studio for a while, and it's one of the (if not the) most powerful IDE's I've used.
There is one thing I can't find though.
If you have a list like this:
Code:
private String username = null;
private String userAvatarURL = null;
private String bio = null;
private String birhtdate = null;
private String gender = null;
private String location = null;
private String twitter = null;
private String externalURL = null;
private String urlIFLprofile = null;
private String urlIFLwallpapers = null;
private List<Integer> wallpaperIdArray = null;
How do you change private to public without changing them one by one?
Thanks for the tip!
Tim

tim687 said:
I've been using Android Studio for a while, and it's one of the (if not the) most powerful IDE's I've used.
There is one thing I can't find though.
If you have a list like this:
Code:
private String username = null;
private String userAvatarURL = null;
private String bio = null;
private String birhtdate = null;
private String gender = null;
private String location = null;
private String twitter = null;
private String externalURL = null;
private String urlIFLprofile = null;
private String urlIFLwallpapers = null;
private List<Integer> wallpaperIdArray = null;
How do you change private to public without changing them one by one?
Thanks for the tip!
Tim
Click to expand...
Click to collapse
Code:
public String username = null;
public String userAvatarURL = null;
public String bio = null;
public String birhtdate = null;
public String gender = null;
public String location = null;
public String twitter = null;
public String externalURL = null;
public String urlIFLprofile = null;
public String urlIFLwallpapers = null;
public List<Integer> wallpaperIdArray = null;
Use Edit -> Find... -> Replace
Rember shortcut "Replace" .
Bye

cristaccio85 said:
Code:
public String username = null;
public String userAvatarURL = null;
public String bio = null;
public String birhtdate = null;
public String gender = null;
public String location = null;
public String twitter = null;
public String externalURL = null;
public String urlIFLprofile = null;
public String urlIFLwallpapers = null;
public List<Integer> wallpaperIdArray = null;
Use Edit -> Find... -> Replace
Rember shortcut "Replace" .
Bye
Click to expand...
Click to collapse
I knew that one, but what if your Object names contain public or private?
such as
Code:
private boolean visibleToPublic = false;

Related

[Q] Setting restriction on editText

Hello, I am new to java and having difficulties trying to set a restriction on one of my editText.
I am trying to develop an app that allows user to fill there information in the application and then send through an email client.
So for my phone number section i am trying to make a restriction for only 11 characters to be entered, also for all the fields to be completed to process through the next stage.
here is my coding:
package com.example.rumel.booking2;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
EditText personName, numberGuests, phoneNumber, date, time;
String name, guests, number, dte, tme;
Button sendEmail;
@override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializerVars();
sendEmail.setOnClickListener(this);
}
private void initializerVars() {
personName = (EditText) findViewById(R.id.etName);
numberGuests = (EditText) findViewById(R.id.etGuest);
phoneNumber = (EditText) findViewById(R.id.etPhone);
date = (EditText) findViewById(R.id.etDate);
time = (EditText) findViewById(R.id.etTime);
sendEmail = (Button) findViewById(R.id.button);
}
public void onClick (View v) {
// TODO Auto-generated method stub
convertEditTextVarsIntoStringsAndYesThisIsAMethodWeCreated();
String[] to = new String[]{"[email protected]"};
String message = "Name: "
+ name
+ "\n"
+ " Number of Guests: "
+ guests
+ "\n"
+ " Contact Number: "
+ number
+ "\n"
+ " Date: "
+ dte
+ ", Time: "
+ tme
+ "\n"
+ "\n"
+ "Thank you";
Intent emailIntent = new Intent (Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Reservation");
emailIntent.putExtra(Intent.EXTRA_TEXT, message);
emailIntent.setType("message/rfc822");//rfc822 email protocol
startActivity(Intent.createChooser(emailIntent,"Email"));
}
private void convertEditTextVarsIntoStringsAndYesThisIsAMethodWeCreated() {
// TODO Auto-generated method stub
name = personName.getText().toString();
guests = numberGuests.getText().toString();
number = phoneNumber.getText().toString();
dte = date.getText().toString();
tme = time.getText().toString();
}
@override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
@override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Would appreciate any help, thank you

[Q] How to update a textView every second.

I have a textview that shows your amount of money and i would like the ui to update every second to visually show how much money you have in real time. Would i be able to accomplish this with a timer?, and if so what would it look like.
Hello,
You should do inside your class, outside of any method:
Code:
TextView tv;
String calculatedString;
MyAsyncTask mAsync = null;
Timer timer = null;
TimerTask task = null;
private class MyAsyncTask extends AsyncTask<String, Void, String> {
public MyAsyncTask(){
}
@Override
protected String doInBackground(String... params) {
//Background operation in a separate thread
//Write here your code to run in the background thread
//calculate here whatever you like
calculatedString = ....;
return null;
}
@Override
protected void onPostExecute(String result) {
//Called on Main UI Thread. Executed after the Background operation, allows you to have access to the UI
tv.setText(calculatedString);
}
@Override
protected void onPreExecute() {
//Called on Main UI Thread. Executed before the Background operation, allows you to have access to the UI
}
}
inside the onCreate after super and setContentView:
Code:
tv = (TextView) findViewById(R.id.tv); //your tv id here
final Handler handler = new Handler();
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
MyAsyncTask mAsync = new MyAsyncTask();
mAsync.execute();
}
});
}
};
timer.schedule(task, 0, 1000); //Every 1 second
If you still need help, feel free to ask
I have attached 2 screenshots showing the errors i was given after inputting. Any idea how to fix this?
mmdeveloper10 said:
Hello,
You should do inside your class, outside of any method:
Code:
TextView tv;
String calculatedString;
MyAsyncTask mAsync = null;
Timer timer = null;
TimerTask task = null;
private class MyAsyncTask extends AsyncTask<String, Void, String> {
public MyAsyncTask(){
}
@Override
protected String doInBackground(String... params) {
//Background operation in a separate thread
//Write here your code to run in the background thread
//calculate here whatever you like
calculatedString = ....;
return null;
}
@Override
protected void onPostExecute(String result) {
//Called on Main UI Thread. Executed after the Background operation, allows you to have access to the UI
tv.setText(calculatedString);
}
@Override
protected void onPreExecute() {
//Called on Main UI Thread. Executed before the Background operation, allows you to have access to the UI
}
}
inside the onCreate after super and setContentView:
Code:
tv = (TextView) findViewById(R.id.tv); //your tv id here
final Handler handler = new Handler();
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
MyAsyncTask mAsync = new MyAsyncTask();
mAsync.execute();
}
});
}
};
timer.schedule(task, 0, 1000); //Every 1 second
If you still need help, feel free to ask
Click to expand...
Click to collapse
Hello,
For the first image:
You have to import the AsyncTask.
add this
Code:
import android.os.AsyncTask;
with the other imports in your java file.
For your second image:
You haven't wrote the line
Code:
setContentView(R.layout.activity_main);
under your super.onCreate(savedInstanceState); and then the code I said above (I said that on my post )
inside onCreate, where activity_main is your xml layout. And you should have inside that layout, a TextView with an id "tv" (or whatever you like)
and then:
Code:
tv = (TextView) findViewById(R.id.tv); //your tv id here
R.id.tv must much the id you have in your layout. Replace it with the actual id of your TextView. If your id is "tv", write R.id.tv, if it is "mytv" write R.id.mytv. ( I said that on my post also)
Can you show your layout file? (XML - your activity_main.xml). You said that you have a TextView Where is your TextView?
Ok I fixed the problems stated and now only have these two errors remaining
mmdeveloper10 said:
Hello,
For the first image:
You have to import the AsyncTask.
add this
Code:
import android.os.AsyncTask;
with the other imports in your java file.
For your second image:
You haven't wrote the line
Code:
setContentView(R.layout.activity_main);
under your super.onCreate(savedInstanceState); and then the code I said above (I said that on my post )
inside onCreate, where activity_main is your xml layout. And you should have inside that layout, a TextView with an id "tv" (or whatever you like)
and then:
Code:
tv = (TextView) findViewById(R.id.tv); //your tv id here
R.id.tv must much the id you have in your layout. Replace it with the actual id of your TextView. If your id is "tv", write R.id.tv, if it is "mytv" write R.id.mytv. ( I said that on my post also)
Can you show your layout file? (XML - your activity_main.xml). You said that you have a TextView Where is your TextView?
Click to expand...
Click to collapse
Have you imported this?
Code:
import java.util.logging.Handler;
If so, change it to
Code:
import android.os.Handler;
Im not at the computer but I think that should solve my issue I will keep you updated
Sent from my HTC6525LVW using XDA Free mobile app
Hey thanks so much its working perfectly now :good:

Help with grid View

Hello!
i need some help, i am creating a Grid View with this java code:
MainActivity
public class MainActivity extends AppCompatActivity {
private GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new GridAdapter(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Click to expand...
Click to collapse
Product
public class Product {
private static String nombrePizza;
private static String ingredientesPizza;
private static int imagenPizza;
private static int idThumbnail;
public Product(String nombrePizza, String ingredientesPizza, int imagenPizza){
this.nombrePizza = nombrePizza;
this.ingredientesPizza = ingredientesPizza;
this.imagenPizza = imagenPizza;
}
public String getNombrePizza(){return nombrePizza;}
public String getIngredientesPizza(){return ingredientesPizza;}
public int getImagenPizza(){return imagenPizza;}
public int getId(){return nombrePizza.hashCode();}
public static Product[] Pizzas={
new Product(
"Proscuito",
"Jamon York y queso",
R.drawable.imagenprueba),
new Product(
"Tropical",
"Jamon York, queso y piña",
R.drawable.imagenprueba),
new Product(
"Barbacoa",
"Carne picada, queso y salsa Barbacoa",
R.drawable.imagenprueba),
new Product(
"Romana",
"Jamon York, champiñones y queso",
R.drawable.cubo),
};
public static Product getItem(int id) {
for (Product item : Pizzas) {
if (item.getId() == id) {
return item;
}
}
return null;
}
}
Click to expand...
Click to collapse
Grid Adapter
public class GridAdapter extends BaseAdapter {
private final Context mContext;
public GridAdapter(Context c){
this.mContext = c;
}
@Override
public int getCount(){return Product.Pizzas.length;}
@Override
public Product getItem(int position){return Product.Pizzas[position];}
@Override
public long getItemId(int position){return 0;}
@Override
public View getView(int position, View ConvertView, ViewGroup viewGroup){
if(ConvertView == null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ConvertView = inflater.inflate(R.layout.grid_item, viewGroup, false);
}
TextView name = (TextView)ConvertView.findViewById(R.id.grid_mainText);
ImageView image = (ImageView) ConvertView.findViewById(R.id.grid_image);
TextView descripcion = (TextView) ConvertView.findViewById(R.id.grid_subText);
final Product item = getItem(position);
image.setImageResource(item.getImagenPizza());
name.setText(item.getNombrePizza());
descripcion.setText(item.getIngredientesPizza());
return ConvertView;
}
}
Click to expand...
Click to collapse
It work fine except for one thing, the app only shows the last product of the java code (Pizza romana)four times
I dont know why my app do this, can somebody help me??
Thank you! :fingers-crossed:

Handler android

Dear People,
I´m working on a app to control my robot with bluetooth.
So i made a thread to handle the bluetooth device, and a handler to read the information in the Gui thread.
(See code below)
The problem is that i´m not able to print the information to the textview in the handler.
When i print somthing to the textview in de onCreate function, everythings works fine, but when i print something to the textview in the handler, nothing happens.
There are also no errors or something like that.
I know that the handler function is called, because the receive messages are printed well in the log.d.
I hope that there is anyone who can help me.
thanks in advance.
Tom
public class InteractionActivity extends Activity {
private ConnectThread mConnectThread;
private interface MessageConstants
{
public static final int MESSAGE_READ = 0;
public static final int MESSAGE_WRITE = 1;
public static final int MESSAGE_TOAST = 2;
}
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interaction);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
BluetoothDevice device = getIntent().getExtras().getParcelable("btdevice");
((TextView) findViewById(R.id.textView5)).setText("Hallo"); // works fine, print correct to the screen
mConnectThread = new ConnectThread(device);
mConnectThread.start();
}
private final Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
byte[] writeBuf = (byte[]) msg.obj;
int begin = (int)msg.arg1;
int end = (int)msg.arg2;
String writeMessage = new String(writeBuf);
writeMessage = writeMessage.substring(begin, end);
((TextView) findViewById(R.id.textView5)).setText("Hallo1111"); //Nothing happens
Log.d(TAG, writeMessage); //is printed fine in the log
}
};
private class ConnectThread extends Thread {
etc ............

Help with db manipulation

Hello everyone, I have a problem, i made an app in android studio and i want to use an existing db which i already transferred in assets and project them into a list where every table will be a "category" and every record will be able to have its values manipulated i.e. "Name" "Value" [Item1,1] [Item2,2] Item1(value)+Item2(value)=3. I don't know if i'm being understandable...thanks anyway below i will attach my DatabaseHelper
Code:
import android.content.Context;
import android.database.sqlite.SQLiteAbortException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DatabaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "";
private static String DB_NAME = "my.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null,1);
this.myContext = context;
DB_PATH= myContext.getDatabasePath(DB_NAME).toString();
}
//Create an empty db to overwrite your own db on it
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//does nothing cause db already exists
}else{
//Overwrites db
this.getWritableDatabase();
try{
copyDataBase();
} catch(IOException e){
throw new Error("Error copying database");
}
}
}
// Check if db exists every time app opens
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH;
checkDB = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READONLY);
}catch (SQLiteException e){
//db doesn't exist yet
}
if (checkDB!=null){
checkDB.close();
}
return checkDB != null ? true : false;
}
//Copies your db from assets to the new db
private void copyDataBase() throws IOException{
//open your local db as input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
//Path to the empty db
String outFileName = DB_PATH;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from I to O
byte[] buffer = new byte[1024];
int length;
while((length=myInput.read(buffer))>0){
myOutput.write(buffer,0,length);
}
//Close streams
myOutput.flush();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}

Categories

Resources