I searched for an answer but couldn't find it.
The problem:
Depending on the selected spinner-item the application should show a different image.
At this moment I can't get it to work.
The Url works like this: "my.site.com/images/" imc_met ".png"
were imc_met is the filename.
I can't get it to work.
Btw the app isn't finished yet
package example.myapplication;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
public class itemsActivity extends Activity {
private Spinner spinner1, spinner2;
private Button btnSubmit;
private Bitmap image;
private ImageView imageView;
private String imc_met, imc;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.items);
addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
// add items into spinner dynamically
public void addItemsOnSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
// get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(spinner1.getSelectedItem()!=null){
imc_met = spinner1.getSelectedItem().toString();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
imageView = (ImageView)findViewById(R.id.ImageView01);
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
URL url = null;
try {
url = new URL("my.site.com"); //here should the right link appear.
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
if (url != null) {
image = BitmapFactory.decodeStream(url.openStream());
}
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(image);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
EDIT
Please post the whole code needed to be used to make the correct URL.
You call spinner1.setOnItemSelectedListener() twice! The first time (which should get the the item's value) is overwritten by the second time (which is CustomOnItemSelectedListener). Thus, the first listener won't be executed.
Remove the spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
(When you use setOnXxxListener(), only the last call will be used).
YOu should take one string object and on click of spinner item, assign it the value of it and then just use that in URL.
The problem here seems to be that your list values returned contains blank spaces in them which are not permissible in URLs.. Try something without spaces...
EDIT:- ok.. you havent set and OnItemSelectedListener on your spinner2 and have set 2 different ones on spinner1
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
And
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
You have set data on spinner2 and added listener on spiner1. spiner1 has no data. Add listener on spiner2 and run it again
Related
I want to add items to appear as ListView, and I also want when I long click on a specific item, it should get deleted.
For the adding part, it is working well, but for the deleting part, it doesn't work out.
I think I should add an ArrayList, but I don't know how I can link it with the adapter, so the items that I enter should be put in ArrayList..
package com.example.asus.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> arrayList = new ArrayList<String>();
final ListView myList = (ListView)findViewById(R.id.listToDo);
final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
myList.setAdapter(listAdapter);
Button addButton = (Button) findViewById(R.id.button);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText myItem = (EditText)findViewById(R.id.editText);
String value = myItem.getText().toString();
listAdapter.add(value);
myItem.getText().clear();
}
});
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),((TextView)view).getText().toString(),Toast.LENGTH_LONG).show();
}
});
myList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//Adapter adapter = new Adapter(this, listAdapter);
listAdapter.remove(String.valueOf(position));
listAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Item deleted", Toast.LENGTH_LONG).show();
return false;
}
});
}
}
It was working well in the adding part without any error.
Update:
I added
listAdapter.remove(String.valueOf(position));
instead of
listAdapter.remove(position);
There is not any error, but when I long click the item, a message appears "Item deleted" as in the toast, but the item still exists on the listview, how can I fix that?
Because listAdapter is an instance of ArrayAdapter. It do not have any APIs which allow you delete an item based on given position. To delete an item you should you remove(T) API.
By the way when init the listAdapter you should pass arrayList into constructor.
final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
To delete a selected item
arrayList.remove(position);
listAdapter.notifyDataSetChanged();
or
listAdapter.remove(arrayList.get(position));
listAdapter.notifyDataSetChanged();
My goal is to make items from spinner list to change background color of main_activity.
But I can't get working setOnItemSelectedListener for some reason.
Spinner works fine, toast also, but setOnItemSelectedListener is getting error: "cant resolve the symbol..."
Couldn't find any help online that was applicable to my case.
Please see code below:
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Spinner;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Button;
import android.widget.ToggleButton;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import java.util.*;
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
private TextView tekst1;
private int ile = 1;
private Button klawisz1;
private ToggleButton onOff;
private Spinner spinner1;
public void setActivityBackgroundColor(int color) {
View view = this.getWindow().getDecorView();
view.setBackgroundColor(color);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tekst1 = (TextView) findViewById(R.id.tekst);
klawisz1 = (Button) findViewById(R.id.button);
onOff = (ToggleButton) findViewById(R.id.toggleButton);
spinner1 = (Spinner) findViewById(R.id.spinner);
spinner1.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);
spinner1.setOnItemSelectedListener(this);
List<String> colors = new ArrayList<String>();
colors.add("White");
colors.add("Yellow");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, colors);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
onOff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ((++ile%2)==0) {
spinner1.setVisibility(View.VISIBLE);
}
else{
spinner1.setVisibility(View.GONE);
}
}
});
}
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// On selecting a spinner item
//setActivityBackgroundColor(Color.YELLOW);
String item = parent.getItemAtPosition(position).toString();
if(selectedItem.equals("White"))
{
setActivityBackgroundColor(Color.WHITE);
}
else if(selectedItem.equals("Yellow"))
{
setActivityBackgroundColor(Color.YELLOW);
}
// Showing selected spinner item
//Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
public void buttonOnClick(View view) {
if ((++ile%2)==0){
tekst1.setText(new Date().toString());
klawisz1.setBackgroundColor(Color.RED);
}
else
{
tekst1.setText("Next click - TIME");
klawisz1.setBackgroundColor(Color.GREEN);
}
}
You have an extra } before the setOnItemSelectedListener() call and the code is outside of the onCreate() method. Code like spinner1.setOnItemSelectedListener(...) needs to be inside a method.
Try below code for your Spinner:
mTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Do your stuff at item selection time.
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// Intentionally kept blank
}
});
It's becoz you have set setOnItemSelectedListener two times for spinner 1
\\ spinner1.setOnItemSelectedListener((AdapterView.OnItemSelectedListener)
this); \\not required
spinner1.setOnItemSelectedListener(this);
you should set only 1 setOnItemSelectedListener for spinner1
This code worked fine for me.Try it...
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
List<String> colors;
private TextView tekst1;
private int ile = 1;
private Button klawisz1;
private ToggleButton onOff;
private Spinner spinner1;
public void setActivityBackgroundColor(int color) {
View view = this.getWindow().getDecorView();
view.setBackgroundColor(color);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tekst1 = (TextView) findViewById(R.id.tekst);
klawisz1 = (Button) findViewById(R.id.button);
onOff = (ToggleButton) findViewById(R.id.toggleButton);
spinner1 = (Spinner) findViewById(R.id.spinner);
klawisz1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonOnClick(view);
}
});
spinner1.setOnItemSelectedListener(this);
colors = new ArrayList<String>();
colors.add("White");
colors.add("Yellow");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, colors);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
onOff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ((++ile % 2) == 0) {
spinner1.setVisibility(View.VISIBLE);
} else {
spinner1.setVisibility(View.GONE);
}
}
});
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// On selecting a spinner item
//setActivityBackgroundColor(Color.YELLOW);
String item = colors.get(position);
if (item.equals("White")) {
setActivityBackgroundColor(Color.WHITE);
} else if (item.equals("Yellow")) {
setActivityBackgroundColor(Color.YELLOW);
}
// Showing selected spinner item
//Toast.makeText(parent.getContext(), "Selected: " + item,
//Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public void buttonOnClick(View view) {
if ((++ile % 2) == 0) {
tekst1.setText(new Date().toString());
klawisz1.setBackgroundColor(Color.RED);
} else {
tekst1.setText("Next click - TIME");
klawisz1.setBackgroundColor(Color.GREEN);
}
}
}
//MainActivity Details
package com.yay_or_nay.www.yayornayv1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.Categories_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String sSelected=parent.getItemAtPosition(position).toString();
Toast.makeText(this,sSelected, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
XML file that i'm using
<?xml version="1.0" encoding="utf-8"?>
<string-array name="Categories_array">
<item>Technology</item>
<item>Science</item>
<item>Fashion</item>
<item>Photography</item>
<item>Cars</item>
<item>Lifestyle</item>
<item>Food</item>
</string-array>
for example, what do I add to my main activity when I click "Technology" spinner array button? so it can create a new activity action? I'm trying to make it so, if a user clicks an option from the drop-down list from the spinner, it'll create a new instance.
`
package project.com.spinnerclick;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity
private Spinner spinner;
List<String> categoriesArray;
Context mContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeUI();
}
private void initializeUI() {
//initialize context;
mContext = this;
//initialize list to pass it spinner adapter
categoriesArray = new ArrayList<>();
// assign values to categories list
categoriesArray = Arrays.asList(getResources().getStringArray(R.array.categories_array));
// initialize spinner component
spinner = (Spinner) findViewById(R.id.spinner);
//set spinner adapter
spinner.setAdapter(new ArrayAdapter<>(mContext,
android.R.layout.simple_spinner_dropdown_item, categoriesArray));
//setting in item select Listener to spinner items
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
// you can open any activity by getting position of current
// selected item and open the related activity to this position
switch (position) {
case 1:
startActivity(new Intent(mContext, SecondActivity.class));
break;
case 2:
startActivity(new Intent(mContext, Main2Activity.class));
break;
}
//second solution by getting the clicked item value
// and open activity according this value
String selectItem = spinner.getItemAtPosition(position).toString();
Toast.makeText(mContext, selectItem, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
and here is spinner values in XML
<resources>
<string name="app_name">SpinnerClick</string>
<string-array name="categories_array">
<item>select Item</item>
<item>Technology</item>
<item>Science</item>
<item>Fashion</item>
<item>Photography</item>
<item>Cars</item>
<item>Lifestyle</item>
<item>Food</item>
</string-array>
</resources>
Use spinner.getSelectedItem.toString() instead of parent.getItemAtPosition….
I have an application that extends ActionBarActivity, but I'm using ListView and I need to implement OnItemClickListener and I'm not sure how to do this without extending ListActivity. Also I want to ask you: Moreover I have a button and I need to listen if the button is clicked or if an item in the list is clicked. I'm not pretty sure how to do this.
So I would really appreciate if you help me :)
Here is my code:
package com.src.vicnote;
import java.io.File;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
Button newButton;
String path = Environment.getExternalStorageDirectory().toString()+"/VICNote";
String lastOfPath;
File f = new File(path);
File files[] = f.listFiles();
String[] theNamesOfFiles = new String[files.length + 1];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
theNamesOfFiles[0] = "Create new note";
for(int i = 1; i < theNamesOfFiles.length; i++) {
lastOfPath = files[i-1].toString().split("/")[files[i-1].toString().split("/").length-1];
theNamesOfFiles[i] = lastOfPath.replace(".txt","");
Log.d("Files", "String: " + theNamesOfFiles[i]);
}
ListView lv = (ListView) findViewById(R.id.notesList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
theNamesOfFiles);
lv.setAdapter(adapter);
newButton = (Button) findViewById(R.id.buttonNew);
newButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent newNote = new Intent(MainActivity.this, NewNoteActivity.class);
startActivity(newNote);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
You can implement the OnItemClickListener interface and override onItemClick
lv.setAdapter(adapter);
lv.setOnItemClickListener( new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(MainActivity.this.this, "List View row Clicked at"+position,Toast.LENGTH_SHORT).show();
}
});
If the button belongs to activity_main.xml then what you have
newButton.setOnClickListener(new View.OnClickListener()
is right
You can set item click listener for ListView using this code.
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
// it is used to get the clicked string
String theNamesOfFile = adapter.getItem(position);
}
});
I guess it can help you.
I want the option selected in the spinner menu to be displayed in a textview whose id is 'spinnertxt'.
I'm able to open the spinner menu and select the option, but after that nothing happens.
Here is the code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class Addproject extends Activity implements OnItemSelectedListener {
protected int mPos;
protected String mSelection;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addproject);
Spinner spinner = (Spinner) findViewById(R.id.difficultyspinner); ArrayAdapter<CharSequence>
adapter = ArrayAdapter.createFromResource(this,R.array.difficultyarray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_addproject, menu);
return true;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
Addproject.this.mPos = pos;
Addproject.this.mSelection = parent.getItemAtPosition(pos).toString();
TextView spinnerresult = (TextView)findViewById(R.id.spinnertxt);
spinnerresult.setText(Addproject.this.mSelection);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
//NOTHING TO LOOK HERE, MOVE ALONG
}
}
Can someone tell me why? I'm new to this.
Instantiate your array and use
Addproject.this.mSelection = array[pos];
instead of
Addproject.this.mSelection = parent.getItemAtPosition(pos).toString();
Solved!
A listener was missing.
I just added
spinner.setOnItemSelectedListener(this);
to onCreate() after setAdapater()
The listener is needed so that the application listens for taps on particular items.