About Android button event - android

Hi
I am new in android application development.I am practicing the android development for a while.Here is what i am trying to do:
In the screen there is two spinner named "To" and "From" and two edit text button named "Amount" and "Result".i want to do the following:
when Clear button clicked it will reset the “To” and “From” to a default values and clear “Amount” and “Result”.
Can anybody have any idea?it will be very helpful if i get the code.
Thanks.

This is just the crude code, I haven't tested, I just wrote on top of my head, so there can be some errors and I just assumed some layout ids. You need to figure that out.
public class SpinnerExample extends Activity {
private String array_spinner[];
private Spinner toSpinner;
private Spinner fromSpinner;
private Button btnClear;
private EditText etAmount;
private EditText etResult;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
array_spinner=new String[3];
array_spinner[0]="1";
array_spinner[1]="2";
array_spinner[2]="3";
fromSpinner = (Spinner)findViewById(R.id.fSpinner);
toSpinner = (Spinner)findViewById(R.id.tSpinner);
ArrayAdapter fromadapter = new ArrayAdapter(this,
android.R.layout.fspinneritem, array_spinner);
ArrayAdapter toadapter = new ArrayAdapter(this,android.R.layout.tspinner_item,array_spinner);
btnClear = (Button)findViewById(R.id.clear_button);
btnClear.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
etAmount = (EditText)findViewById(R.id.amount_et);
etResult = (EditText)findViewById(R.id.result_et);
etAmount.setText("");
etResult.setText("");
fromSpinner.setAdapter(fromadapter);
toSpinner.setAdapter(toadapter);
}
});
}
}

Related

combining 2 activities together

ok so I'm making an app where I use an edit text to write anything and when I press a button it adds whatever I wrote in the edit to a listView, and it works, but I want the List to be in a different activity than the button and edit text, so I moved it without changing the code.can anyone figure this out,
BTW all the variables are public.
public class MainActivity extends AppCompatActivity {
public ArrayList<String> arrayList2;
public ArrayAdapter<String> adapter,adapter2;
public EditText editText,editText2;
public ArrayList<String> itemList,itemList2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] items = {};
itemList = new ArrayList<String>(Arrays.asList(items));
adapter = new ArrayAdapter<String>(this, R.layout.activity_list_layout,R.id.txtItem,itemList);
ListView listV = (ListView)findViewById(R.id.list_layout);
listV.setAdapter(adapter);
editText = (EditText)findViewById(R.id.thingadd);
Button btAdd = (Button)findViewById(R.id.add);
String[] age = {};
arrayList2 = new ArrayList<String>(Arrays.asList(age));
itemList2 = new ArrayList<String>(Arrays.asList(age));
adapter2 = new ArrayAdapter<String>(this, R.layout.activity_list__layout2,R.id.txtage,itemList2);
ListView listV2 = (ListView)findViewById(R.id.Age);
listV2.setAdapter(adapter2);
editText2 = (EditText)findViewById(R.id.agetxt);
btAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String newItem=editText.getText().toString();
String newItem2=editText2.getText().toString();
itemList.add(newItem);
itemList2.add(newItem2);
adapter.notifyDataSetChanged();
adapter2.notifyDataSetChanged();
}
});
}
}
You can use SQLite Database for that, where you would be saving data in database from one activity and reading it and displaying it in listview from another.
You have 2 options to do so.
You can use any database service. Store the values and display them in the next activity. But this needs Internet service to be enabled in the phone.
You can use intent. Convert those input to string type and pass them to next activity and display them using ids. This type do not require Internet service. To know more about this, Visit this link

Edit Text and put it to List View

I'm kinda new to Android Studio so I will be grateful if you can help me with my question. How can I enter some text and add it to a ListView with the use of a Bundle?
For example, say I enter a name in the EditText component in the MainActivty, and then when I press the OK button, it will be seen in to another Activity in a List View.
I've been using Bundles to transfer text to another Activity but I can't figure out how to transfer text to ListView.
If all you want to do is display strings in your list items, you can do it fairly simply, otherwise you will have to make a custom adapter. For the former, this is what you will want to do
Create an ArrayList<String> where you will store your values that are entered from the EditText.
Create an ArrayAdapter<String> that will serve to connect the ArrayList to the ListView.
Your final code will look something like this:
public class MainActivity extends Activity {
private ListView listView;
List<String> strings;
ArrayAdapter<String> arrayAdapter;
public void onCreate(Bundle saveInstanceState) {
setContentView(R.layout.yourLayout);
listView= (ListView) findViewById(R.id.yourListView);
strings = new ArrayList<String>();
strings.add("list item 1");
strings.add("list item 2");
arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
strings);
listView.setAdapter(arrayAdapter);
}
}
Then in the onClick of the "OK" button simply add the string from the EditText to the arrayList, and notify the listView that it should be updated be calling this:
arrayAdapter.notifyDataSetChanged();
I don't think you can use Bundles for what you want to achieve. Here's how I do it...
First you need to set up your list. I would use global variables:
ListView listView;
EditText editText;
ArrayList<String> strings;
ArrayAdapter<String> adapter;
Now, in onCreate() , you can do...
listView = (ListView) findViewById(R.id.your_listview);
editText = (EditText) findViewById(R.id.your_edittext );
strings = new ArrayList<>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);
listView.setAdapter(adapter);
You will need to set the OnClickListener for your Button. I would do this in onCreate() too:
Button button = (Button) findViewById(R.id.your_button);
button .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here we get the text from the EditText component
String text = editText.getText().toString();
// Now add it to the list
strings.add(text);
// And finally, update the list
adapter.notifyDataSetChanged();
}
});

Multiple ListViews, with same content, on different layouts

Multiple ListViews, with same content, on different layouts
So basically what I have is two ListViews that are getting their content from SQLite DB. I have created a BaseActivity below to extend my other activities to access the same data. The problem I ran into is that I cannot display the data because their are two different layout that contain these ListViews, one in a Dialog and the other in a TabWidget, that are both in separate activities.
So basically....
I need to know how to display two ListViews with the same data that are in different activities (one in dialogBox and the other in TabWidget)
The error I am currently getting is from the layout in the SimpleCursorAdapter is only for one of the ListViews and it wont add the other because it cannot find the View
I am not extending ListActivity at any point
Thank you very much in advance. I will be standing by to edit or clarify.
Part of my Base Activity
public class BaseActivity extends Activity
{
private SimpleCursorAdapter contactAdapter;
public static final String ROW_ID = "row_id";
private static ListView study_guide_list_view;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String[] from = new String[] { "name" };
int[] to = new int[] { R.id.study_guide_item_in_listview };
contactAdapter = new SimpleCursorAdapter(BaseActivity.this, R.layout.study_guide_item_in_listview, null, from, to);
}
}
This segment is where I add the ListView to the TabWidget and it is currently working
study_guide_list_view = (ListView) findViewById(R.id.list);
contactAdapter = getSimpleCursorAdapter();
study_guide_list_view.setAdapter( contactAdapter );
study_guide_list_view.setOnItemClickListener(listview_item_listener);
Where I am trying to add the ListView in the Custom Dialog Box (does not work: error is on study_guide_dialog_list_view.setAdapter( contactAdapter ); )
public OnClickListener save_slide_page_to_guide_btn_listener = new OnClickListener()
{
#Override
public void onClick(View v)
{
TabbedPagesActivity.getListViewAdapter();
dialog = new Dialog(PDFViewerActivity.this);
dialog.setContentView(R.layout.study_guide_custom_dialog_box);
dialog.setTitle("Select a Study Guide");
dialog.setCancelable(true);
study_guide_dialog_list_view = (ListView) findViewById(R.id.list);
contactAdapter = getSimpleCursorAdapter2();
study_guide_dialog_list_view.setAdapter( contactAdapter );
study_guide_dialog_list_view.setOnItemClickListener(listview_item_listener);
Button dialog_ok_btn = (Button) dialog.findViewById(R.id.dialog_ok_btn);//it says cancel though
dialog_ok_btn.setTextSize(20);
dialog_ok_btn.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/AGENCYR.TTF"));
dialog_ok_btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
dialog.dismiss();
}
});
dialog.show();
}
};
Wow so I finally figured it out. The problem lies within the custom Dialog. Instead of calling...
study_guide_dialog_list_view = (ListView) findViewById(R.id.list);
it needs to be....
study_guide_dialog_list_view = (ListView) dialog.findViewById(R.id.list);
If you do not do this the findViewById will return null, hence the NullPointerException

Android Spinner add items

I'm working on an App and need some help with the spinner widget.
The array has no entries by its creation and will be filled in another function. So I have to use adapter.add(item); and spinner.setAdapter(adapter); to update the spinner in this function, I guess.
// global variables
String[] test = new String[100];
int x = 0;
Spinner s;
ArrayAdapter adapter;
Button btn1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(this);
s = (Spinner) findViewById(R.id.spinner1);
adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, test);
}
public void onClick(View arg0) {
test[x] = String.valueOf(x);;
x++;
adapter.add(test[x]);
s.setAdapter(adapter);
}
just for education purpose I tried this, but there is still a bug in the onClick() function.
Something has to be done with the ArrayAdapter, I guess. But I don't familiar with this and didn't found an example for adding itmes by pressing a button.
Maybe there is just a simple failure somewhere else, but currently I just don't see it.
here the complete code:
ArrayList<Integer> test;
int x = 0;
Spinner s;
ArrayAdapter adapter;
Button btn1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(this);
s = (Spinner) findViewById(R.id.spinner1);
test = new ArrayList<Integer>();
adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, test);
}
public void onClick(View arg0) {
test.add(x);
x++;
s.setAdapter(adapter);
}
You are adding x to an array element then passing this element to the adapter.add method. This is effectively the same as using: adapter.add(x);.
Why not use an ArrayList instead of a array. ArrayLists are dynamic, and do not need to be given an initial size.
A very quick check has shown me that there is a constructor for an Array Adapter that is compatible with ArrayLists and I expect any descendant of the List class. See the code example below.
ArrayList<Integer> test = new ArrayList<Integer>();
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, test);
I know this is not a complete solution to your problem, but just a pointer to a possibly better way of doing it.
Do a web search with the terms ‘android updating arrayAdapter at runtime’ and I am sure you will stumble upon a more concrete example.
The problem is you are setting the ArrayAdapter's data to be a 100 null strings, and then adding to the end of that when you press the button. Try changing your test variable to be new String[0].
Then in your onClick method, just call adapter.add(String). It will add it to the end of your current data, so you don't need to give it a big enough array initially.

Connecting Android application with a webservice [duplicate]

Hi
I am new in android application development.I am practicing the android development for a while.Here is what i am trying to do:
In the screen there is two spinner named "To" and "From" and two edit text button named "Amount" and "Result".i want to do the following:
when Clear button clicked it will reset the “To” and “From” to a default values and clear “Amount” and “Result”.
Can anybody have any idea?it will be very helpful if i get the code.
Thanks.
This is just the crude code, I haven't tested, I just wrote on top of my head, so there can be some errors and I just assumed some layout ids. You need to figure that out.
public class SpinnerExample extends Activity {
private String array_spinner[];
private Spinner toSpinner;
private Spinner fromSpinner;
private Button btnClear;
private EditText etAmount;
private EditText etResult;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
array_spinner=new String[3];
array_spinner[0]="1";
array_spinner[1]="2";
array_spinner[2]="3";
fromSpinner = (Spinner)findViewById(R.id.fSpinner);
toSpinner = (Spinner)findViewById(R.id.tSpinner);
ArrayAdapter fromadapter = new ArrayAdapter(this,
android.R.layout.fspinneritem, array_spinner);
ArrayAdapter toadapter = new ArrayAdapter(this,android.R.layout.tspinner_item,array_spinner);
btnClear = (Button)findViewById(R.id.clear_button);
btnClear.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
etAmount = (EditText)findViewById(R.id.amount_et);
etResult = (EditText)findViewById(R.id.result_et);
etAmount.setText("");
etResult.setText("");
fromSpinner.setAdapter(fromadapter);
toSpinner.setAdapter(toadapter);
}
});
}
}

Categories

Resources