how to fill string arraylist with userinput EditText android eclipse - android

i am beginner in android programming and English language :) !!
i want to get names from user and show the names that user inputs randomly by pressing a button.
how can i store a user input names in a array list?
and As regards I don't know how many names he wants to enter and don't show a name twice and after showing the name erase that name in array list.
tanks a LOT.
package farshid.mk.teststringarraylist;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class StringArraylistActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayList<String> inputs = new ArrayList<String>();

Well, there are many ways to do this but one would be:
Create an EditText, a view that allows user input
Create a Button that the user would click to indicate that he/she is done entering the name in the EditText
Create an OnClickHanlder for the button so that you can get the text from the EditText, convert it to a String (EditText returns an Editable object), and add it to your ArrayList<String>
// add the string
myArrayList.add(myEditText.getText().toString().trim());
To remove duplicates from the List you will need to convert it to a Set... google "java arraylist remove duplicates (First Link)
Also, in Java, it's best practice to use Generics:
// psuedocode
List inputs = new ArrayList
There are many ways you could display this list of text to the user in Android, you will need to research that on your own.
You simply clear the ArrayList when you are done with the names.
This should get you going =)

There's a lot of things you'd need to do to achieve this:
Define the EditText in your layout's XML.
Get a reference to it from the Activity by calling findViewById(R.id.your_edittext_id).
From that reference, call getText().toString() to get the value from the EditText.
Add that String to your ArrayList.
That's basically the main idea behind what you're trying to achieve. I could give you the code but then perhaps its best for you to do it yourself (so that the next time you need to do this, you'd understand the concept).
Good luck! :)

Related

Making a simple email application

So the story is that basically, I'm making a very simple app so that my place of employment can send the closing logs every night with ease. I have all of the text boxes to enter the information, and then at the bottom there is a checkbox and submit like so..
There is a template for the email that we edit manually every night, and basically I want to take all of the information received in the text boxes (all have their own ID, etc.) and plug it into the email template that I currently have - and send it to the emails of the employees (6 people).
Can someone lead me in the right direction, or show me how I would go about this problem?
MainActivity.java
package savo.nicholas.bestb;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I've constructed a bunch of these to get the values from the form. I'm looking to plug these values into a pre-formatted email and send them.
public EditText getGm() //$ of GM
{
return (EditText)findViewById(R.id.gm);
}
Thanks!
Nick

Where do I place OnItemClickListener?

I have built a test app that shows few records on a listview.
Now I want to click on an item and see the info on the debug of android studio.
I know I'm supposed to create an OnItemClickListener but I'm not sure where I'm supposed to place it.
I tried placing it on the mainactivity, the app works, but the click function is never called, so there is something wrong.
I looked around Google for some help, but I couldn't wrap my mind around it.
It should be a straightforward action (I have a list, I click an item) but I am not able to make it work.
This what I tried so far:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.LinkedList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listViewDemo);
List list = new LinkedList();
list.add(new Contatto("Antonio","Rossi","1234567890"));
list.add(new Contatto("Pino","Bianchi","2345678901"));
list.add(new Contatto("Peppe","Verdi","3456789012"));
list.add(new Contatto("Leo","Rossi","4567890123"));
list.add(new Contatto("Mario","Blu","5678901234"));
list.add(new Contatto("Aldo","Da Vinci","6789012345"));
CustomAdapter adapter = new CustomAdapter(this, R.layout.rowcustom,list);
listView.setAdapter(adapter);
OnItemClickListener clickListener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view,
int position, long id) {
Contatto c = (Contatto)adapter.getItem(position);
Log.d(c.getNome(),c.getTelefono());
}
};
listView.setOnItemClickListener(clickListener);
}
}
OnItemClickListener is not recognized, and getItem neither.
The autocorrect of android Studio proposes me to change to "AdapterView.OnItemClickListener", getItem has nooptions to be recognized.
I tried to add " implements OnItemClickListener" on the class declaration, but it doesn't work either (gives error, name is in red, no solutions are provided by the android studio).
At one point I was able to remove all errors, but the code still didn't work and I don't remember what I did, I was just fiddling.
You can put the OnClickListener in onCreate()
If you post your code, that would certainly help
There are a couple of things that are questionable about the code you posted.
You create OnItemClickListener but you don't have an import for AdapterView.OnItemClickListener. So are you sure you're using the right class?
Inside your onItemClick, you reference adapter, which is the local parameter of type AdapterView<?>, but AdapterView has no such method getItem(int). It can't be a reference to your CustomAdapter because that's not declared final.
Your use of Log.d(c.getNome(),c.getTelefono()); is wrong. The various log methods take a "tag" as the first parameter. It could be that you're not seeing the log messages because that's just wrong.
So, really, this shouldn't even compile. Please review your code and post the most recent, most correct, most compilable version you have. Including the code for you custom adapter and layout wouldn't hurt either.
Have you tried using your debugger to step through this code and seeing if a breakpoint at the click point gets hit?

Can't be resolved as a type android Eclipse

It is saying that popupWindow.OnDismissListner Can't be resolved as a type and when I hover the mouse over it the only option I get is to make it an interface, which isn't what I want to do.
public class MainActivity extends Activity implements OnItemSelectedListener,
PopupWindow.OnDismissListener, View.OnClickListener {
I am unsure exactly what is wrong. I imported android.widget.PopupWindow.OnDismissListener; and it still is giving the error. I am trying to get a menu to pop up with a list of dice and when a dice is clicked it will return a matching int value based on how many sides the dice has. I have no other errors except the one mentioned above.
Try replacing
import android.widget.PopupWindow.OnDismissListener;
with
import android.widget.PopupWindow;
Or simply use OnDismissListener in your code (not PopupWindow.OnDismissListener).

getText() Not Responding

I'm trying to get the text from one XML to another but it just crashes when its supposed to happen so any help will be welcomed!
Here is the code by the way
package com.android.test1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class xmltwo extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.xmltwo);
TextView one = (TextView) findViewById(R.id.textView1);
EditText two = (EditText) findViewById(R.id.editText1);
one.setText(two.getText());
}
}
That's from the java file that opens the XML where the textView is.
My guess?
The logcat will be showing a NullPointerException for the line one.setText(two.getText()); because the layout file referenced by R.layout.xmltwo doesn't actually contain a TextView with an id of R.id.textView1 so the TextView called one is null.
Either that or that layout file doesn't contain an EditText with an id of R.id.editText1 which will make that null and cause an NPE when trying to call two.getText().
Just as an extra bit of information, if you want to get the text from an EditText you need to use getText().toString()
In this article Android: edit textview defined in xml one of the members has a similar issue. They theorize that possibly setContentView hasn't finished running yet and that may be the issue. However, you're saying that it is just completely crashing so I'm not sure if that could be the problem.
Could there be another part of your in your super class that could be hanging up? Could you use the getString() function instead perhaps?
I hope some of this might be helpful! I am interested in finding a solution so I intend to keep on reading to try and help!
try
one.setText(two.getText().toString().trim()+"");
hope this help..

Show different TextView's depending on the time of the day

I'm new to develop Android Apps and writing java. But, I only want to make a simple app that will give a different output on TextView depending on what time of the day it is.
F.ex. If the time is between 06:45 and 08:45 I want it to say "Good Morning", and so forth.
I have been playing with Eclipse and created a simple WebView-app but I can't seem to find any information to either choose which TextView I want shown on a specific time or show a different layout.
Do you have any tips?
This is my .java file in src for now.
package com.wao.texttime;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class TextTime extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv1 = (TextView) findViewById(R.id.TextView01);
tv1.setText("Good Morning");
}
}
There is no need to switch between different TextViews. If you just want to display different messages in your TextView I would simply call the setText function to update the TextView's text attribute.

Categories

Resources