I am trying to add the text written in a textfield everytime and add it into an ArrayList. For eg- if the text "abcdef" is written in the textfield, then it should get added into the list. Again if the text "ghijkl" is written in the textfield, than that should get added.
My code for adding the text into the listview:
final ArrayList<String> mylist = new ArrayList<String>(5);
String s3=text1.getText().toString();
if(s3!=null)
g++;
else g--;
if(g>0) {
for(int i=0;i<mylist.size();i++) {
mylist.add(s3);
Toast.makeText(getApplicationContext(), mylist.get(i), Toast.LENGTH_LONG).show();
}
}
Is this the right way to do this? Also I am trying to get the list item for testing purpose, but I don't get any toast.
I assume you like to add the Content of your EditText field into the list after pressing a button, so you could do it like this:
// Member to store your Texts in list
private ArrayList<String> _list = new ArrayList<>()
// Method to call to add an entry to your list
// Either by pressing a button, removing focus of your edit text or something else
private void addTextToList()
{
String text = text1.getText().toString();
if (text != null)
{
_list.add(text);
}
}
private void showToast()
{
for(String text: _list)
{
Toast.makeText(this, mylist.get(i), Toast.LENGTH_LONG).show();
}
}
The toast is not shown because you need to pass your current activity.
Assuming your Code is in your activity, I passed this in the example above.
you have to make a button to handle this action every click i make editTex ,textView and Button
this is the action of button it every click take the value of editText and stor it in ArrayList and increse String by this value
public void getText(View v){
if(ed.getText().toString().length()!=0){
s+="\n"+ed.getText().toString();
arr.add(ed.getText().toString());
}
tv.setText(s);
}
Related
I'm making a button that when i click on him the visibility of a checkbox withing a listView will change. however it appears that the code run as expected but the visibailty is not udpateing. is there a way to update the item's visabilty?
mButtonEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i = 0 ; i<calanders.size();i++){
View view = mListView.getAdapter().getView(i,null,mListView);
if(mButtonEdit.isSelected()){
print("button is selected");
CheckBox checkBox = view.findViewById(R.id.clockproperties_checkBox);
checkBox.setVisibility(View.GONE);
}else{
print("button is not selected");
CheckBox checkBox = view.findViewById(R.id.clockproperties_checkBox);
checkBox.setVisibility(View.VISIBLE);
}
}
mListView.getAdapter().
if(mButtonEdit.isSelected()){
mButtonEdit.setSelected(false);
}else{
mButtonEdit.setSelected(true);
}
}
});
You should never call the ListAdapter's getView() method. It is only supposed to be called by the system when scrolling though the ListView. Instead you need to update the list by calling mListView.getAdapter().notifyDataSetChanged().
Add a boolean field in the adapter and update its value when the button is clicked.
You can create a model/data class based on your data and keep a Boolean variable for checkbox visibility. So default make it false and on button click get the position of list-view item and update Boolean variable to true, and do adapter.notifyDataSetChanged().
You can also try with :
((YourAdapter) yourListView.getAdapter()).notifyDataSetChanged();
So I have a situation where I populate TextView from Intent extras and then allow users to edit it. When user taps on edit Button, EditText appears with text shown in TextView.
Then user can input new value.
And finally when the save Button is pressed, new value isn't saving, instead the old value is still shown.
Here's my code:
Bundle extras = getIntent().getExtras();
if (extras != null){
textViewNazivTodoaDetails.setText(extras.getString("naslov"));
textViewDatumTodoaDetails.setText(extras.getString("datum"));
final int id = extras.getInt("id");
String oldText = extras.getString("naslov");
editTextDetaljnoIspravljanje.setText(oldText);
}
final String newText = editTextDetaljnoIspravljanje.getText().toString();
buttonDetaljnoIspravljanjeDone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textViewNazivTodoaDetails.setText(newText);
}
});
}
What am I doing wrong?
You are clearly setting the text back to old text inside your onClick with the following line.
textViewNazivTodoaDetails.setText(newText);
You don't need this line inside onClick. Instead of this you should have the code to save the new value to where ever you want to save it.
The line of code where you initialized newText was called before you edited it and contains the same value as oldText. Hence you see the old text again.
Your text view already has the newly entered text.
I have an arraylist for my spinner, 1 add button to add element into the list and 1 delete button to delete the element inside the list. The elements that I added into the list will show in the spinner. Initially the arraylist is empty with nothing inside. When it is empty and I press the delete button, means that I am trying to delete elements in a arraylist with no element inside and this makes my app crashes.
So, I wanted to add a toast to replace the delete function when the list is empty. When the list is not empty, then the delete function will come back.
Any solution for this?
spinner = (Spinner) findViewById(R.id.spinner1);
adp = new ArrayAdapter<String>(CarSelection.this,android.R.layout.simple_spinner_item, list);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
spinner.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, final int position, long id) {
Button delete = (Button) findViewById(R.id.delete);
View.OnClickListener del = new View.OnClickListener() {
#Override
public void onClick(View view) {
list.remove(position);
}
Firstly,IMHO for better UX, you should not display the spinner if the list is empty.You can show toast message to the user saying that you cant perform this operation.
Anyways here is the code snippet you can use to do the check.You can put this check in whichever place you want
if(!list.isEmpty())
//list is empty
else
list is not empty
Lets say, your ArrayList is called mList, your delete function should look something like -
public void deleteElement(int pos) {
if(mList.isEmpty()) {
//Toast
Toast.makeText(yourContext, "Ooi, list is empty", Toast.LENGTH_SHORT).show();
} else {
mList.remove(pos);
}
}
UPDATE
#Override
public void onClick(View view) {
if(list.isEmpty()) {
//Your Toast
Toast.makeText(yourContext, "Ooi, list is empty", Toast.LENGTH_SHORT).show();
} else {
list.remove(position);
}
}
You should test for the "empty" case. Supposing an array named "elements":
if elements.isEmpty() {
deleteButton.disable();
}
The best is to disable the delete button when there is no elements in the array.
A want to develop a project in android using SOAP service protocol. So that i get XML and parse it into String array. I want to show one String (Like String[0]) in one TextView then when a user click the next button next String (Like String[1]) will show on the same page and the same TextView. How how could i do that?
Probably you do not know much of android.
in your main activity class where you want to show data create the filed String array as
public class NNN extends Activity {
//... some other fields
private String[] data;
private int current;
Private TextView yourTextView;
//in on create
data = null;
current = 0;
yourTextView = (TextView) findViewById(R.id.myTextView);
then in the method where you parsed the XML of string array do
data = parsedArray;
current =0;
then on button click where you want to set the text to text view
if(data ! = null) {
//means you have parsed XML to string array
if(current != data.length) {
//means there is some data that user did not viewd
youTextView.setText(data[current]);
current++;
}
}
You must see some tutorials to get familiar to the android. See Creating hello world app
I don't know the SOAP architecture. But I will assume you store the strings parsed as following array. And also we must keep track of the current displaying string:
static String[] myparsedarray;
static int curIndex;
add an OnClickListener to your 'next' button:
class MyActivity extends Activity {
//your string array
static String[] myparsedarray;
static int curIndex;
.
.
. //lots of other code
// inside onCreate()
public void onCreate(...) {
Button nextBut = (Button) findViewById(R.id.nextbutton);
nextBut.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView myloopingtext = (TextView) findViewById(R.id.mytextview);
curIndex = (curIndex + 1) % myparsedarray.length;
//the modulus to avoid arrayIndexOutOfBoundsException :)
myloopingtext.setText(myparsedarray[curIndex]);
}
});
.
.
. //rest of the code
}
}
P.S others: Correct me if I am wrong :)
edit: As indivisible said, look into android tutorials. I also suggest you search for similar questions. Cheers!
I have an EditText field that represents an ID number. That field can either be filled programmatically, using IDField.setText(String) based on the results of a card swipe, or it can be filled manually using the keyboard.
Once the text is filled both methods (auto login--based on swipe, or manual--based on button click) both run the same sign in script. However when I go to grab the contents of the EditText field, if I edited the text manually I get an empty string returned. If the text was set programmatically then it works perfectly.
This doesn't make any sense to me. Is there a reason that editText.getText().toString() would not return the content that is visibly shown in the textbox?
XML:
<Button
android:id="#+id/btn_swipeCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/signInID"
android:layout_toLeftOf="#+id/textView1"
android:onClick="SignInStudent"
android:text="Swipe ID" />
Button Initialization:
IDField = (EditText) layout.findViewById (R.id.signInID);
LoginButton = (Button) findViewById(R.id.button1);
LoginButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { SignInStudent(); } } );
Card Swipe:
// displays data from card swiping
private Runnable doUpdateTVS = new Runnable()
{
public void run()
{
try
{
//Grab ID Number
String[] splitMSG = strMsrData.split("=");
//txtIDNumber.setText(splitMSG[2]);
IDField.setText(splitMSG[2]);
StringBuffer hexString = new StringBuffer();
hexString.append("<");
String fix = null;
for (int i = 0; i < msrData.length; i++) {
fix = Integer.toHexString(0xFF & msrData[i]);
if(fix.length()==1)
fix = "0"+fix;
hexString.append(fix);
if((i+1)%4==0&&i!=(msrData.length-1))
hexString.append(' ');
}
hexString.append(">");
myUniMagReader.WriteLogIntoFile(hexString.toString());
SignInStudent();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
};
Sign In Logic:
public void SignInStudent()
{
String temp = "http://wwww.tempUrl.com/signIn?ID="+ IDField.getText().toString() + "&DeviceId="+KEY;
webView.loadUrl(temp);
}
The layout is only updated during the onCreate phase of the loop. This is fired when an onResume event is called as well which explains why the fields update after you lock and unlock the device. There are a few workarounds for this such as doing more background processing and then creating a new view with correct values, or using a surfaceView that allows drawing to occur while the program is in its normal execute cycle.
For my application I either do background processing and then move to a new view, or have a view that just keeps calling itself to get the onCreate events to fire again. The solution depends on the application, but that's why the problem occurs.