I'm trying to create a Spinner in Android Studio - java

I'm new to Android Studio, any help would be greatly appreciated.
This is not the mainActivity but another page called StudentReg1 and branch refers to the spinner id as well as as string array in strings.xml
public class StudentReg1 extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.student_reg1);}
Spinner mySpinner = (Spinner) findViewById(R.id.branch);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(StudentReg1.this,android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.branch));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(myAdapter);
}
setDropDownViewResource and setAdapter appear in red, I don't seem to understand the issue.

I think you need to use:
ArrayAdapter<String> myAdapter = ArrayAdapter.createFromResource(StudentReg1.this,R.array.branch,android.R.layout.simple_spinner_item);

hey buddy use this easy library to make spinner
implementation 'com.jaredrummler:material-spinner:1.3.1'
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="#+id/newAnnouncementSpinner1"
android:layout_marginHorizontal="25dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/select_type"/>
you can use the above code for your xml code and below for java code
private MaterialSpinner newAnnouncementSpinner1
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newAnnouncementSpinner1=view.findViewById(R.id.newAnnouncementSpinner1);
List<String> categories= new ArrayList<>();
categories.add("string one");
categories.add("string two");
categories.add("string three");
categories.add("string four");
categories.add("string five");
categories.add("string six");
newAnnouncementSpinner1.setItems(categories);
}

Related

I am trying to add a spinner in my android app

I'm trying to add a spinner to my android app but gives me this error(Compilation failed; see the compiler error output for details.) and this part (MainActivity.this,) has a red line under it when the cursor is under it says 'com.example.myapplication.MainActivity' is not an enclosing class. I'm doing this from another activity not from the main activity. No changes in the MainActivity.
AddEmployee class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_employee);
Spinner myspinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter myAdapter = ArrayAdapter.createFromResource(MainActivity.this, R.array.types, android.R.layout.simple_list_item_1);
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
myspinner.setAdapter(myAdapter);
}
try with like this
List<String > strings = new ArrayList<>();
ArrayAdapter<String> adapter = new ArrayAdapter<>(activity,android.R.layout.simple_list_item_1,strings);
spinner.setAdapter(adapter);
You need a valid context, so you should use your current activity
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_employee);
Spinner myspinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter myAdapter = ArrayAdapter.createFromResource(SecondActivity.this, R.array.types, android.R.layout.simple_list_item_1);
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
myspinner.setAdapter(myAdapter);
}
}
or you can use this or getApplicationContext() instead of MainAcitvity.this
but I prefer to use this
createFromResource's first parameter is context. You can get context in two way:
Application Context: getApplicationContext()
Activity's Context: ActivityName.this
Application context is general context of project, you can get it from all activity.
Activity contexs are specific to the activity. So you can access it from only it's own activity. You tried to access an activity context from another activity so it gave this error. You can try getApplicationContext() or AddEmployee.this
Here is a working example :
public class Main extends AppCompatActivity implements AdapterView.OnItemSelectedListener
{
String[] item_list= {"Select your option", "item1", "item2", "item3", "item4"};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_employee);
Spinner spin = (Spinner) findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);
ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, item_list);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}

using 2 Java classes to display information on 1 XML file

I am new to programing and I am trying to clean up my main class.
There is just too much going on there.
I was wondering if it is possible to set up a way that 2 classes were to control 1 xml file.
For now I started doing this, but it didnt work:
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....SOME CODE....
getParsha();
}
}
public void getParsha() {
new Parsha();
}
In new Parsha I had this code:
public class Parsha extends AppCompatActivity {
Parsha() {
setContentView(R.layout.activity_main);
....SOME CODE.....
}
}
I also tried doing this in onCreate() and it didnt work.
I am not sure why, is this even allowed to do in Android.
Please Note: I didnt get any error, it simply just didnt process the code, no Log or anything.
Thank you.
When you call the Activiy shoud use Intent
Like this
intent = new Intent(MainActivity.this, Parsha.class);
startActivity(intent);
finish();//if use need to stop use first activity.
And then, In your second activity namely Parsha, why you are set content view in inside constructor
You should give this in onCreate block only
setContentView(R.layout.activity_main);
Your two activity should be in following structure
public class MainActivity extends AppCompatActivity{
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
....SOME CODE....
getParsha();
}
public void getParsha() {
intent = new Intent(MainActivity.this, Parsha.class);
startActivity(intent);
}
}
Second Activity
public class Parsha extends AppCompatActivity{
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....SOME CODE....
}
}
Rather than using xml file in both java files why dont you just make the common variables static and keep the code in Parsha.java, use the class wherever inside onCreate(). The following example might give you a clear idea.
MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView list;
ArrayAdapter<String> adap;
static ArrayList<String> arr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
arr = new ArrayList<String>();
new Parsha();
arr.add("Hello");
arr.add("There");
adap = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr);
list.setAdapter(adap);
}
}
Parsha.java
public class Parsha extends AppCompatActivity{
Parsha(){
arr.add("This");
arr.add("Works");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

Java does not recognize ListView variable

I trying to create a ListView in AndroidStudio with value "nome". I created a list and a adapter for this, but my application not recognize variable "services".
This is my activity code
public class MainActivity extends AppCompatActivity {
private String service;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_item1);
ArrayAdapter<Services> servicesAdapter = new ArrayAdapter<Services>(this, android.R.layout.simple_list_item_1, services); <-- in this line
listView.setAdapter(servicesAdapter);
}
private List<Services> gerarServices(){
List<Services> services = new ArrayList<Services>();
services.add(criarServices("Home"));
services.add(criarServices("Delivery"));
return services;
}
private Services criarServices(String nome){
Services service = new Services(nome);
return service;
}
}
Any suggestion? Thank's in advance!
The services variable is not in the scope of your onCreate method. As mentioned by #Code-Apprentice, you might want to find out more about scope of local variables.
So, what you might want to do is to invoke the gerarServices() method in the onCreate method and assign it to a local variable which will then be passed to the ArrayAdapter. Like so:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_item1);
List<Services> services = gerarServices();
ArrayAdapter<Services> servicesAdapter = new ArrayAdapter<Services>(this, android.R.layout.simple_list_item_1, services);
listView.setAdapter(servicesAdapter);
}
or
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_item1);
ArrayAdapter<Services> servicesAdapter = new ArrayAdapter<Services>(this, android.R.layout.simple_list_item_1, gerarServices());
listView.setAdapter(servicesAdapter);
}
Hope it helps!
In your code, I dont see any mehod for declaring and initializing services.
But gerarServices() return a list of Services. So I guess you have to do above action(s) before using any variables or instances of class, etc.
Like so,
ListView listView = (ListView) findViewById(R.id.list_item1);
// Add this line of code
List<Services> services = gerarServices();
ArrayAdapter<Services> servicesAdapter = new ArrayAdapter<Services>(this, android.R.layout.simple_list_item_1, services);
Hope this help.

How to show json data in Listview with Retrofit2

I'm a newbie about Android programming :(
I have no idea to set Listview for show data
How to Show data in Listview.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "fong";
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(UdacityService.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
UdacityService service = retrofit.create(UdacityService.class);
Call<UdacityCatalog> requestCatalog = service.listCatalog();
requestCatalog.enqueue(new Callback<UdacityCatalog>() {
#Override
public void onResponse(Call<UdacityCatalog> call, Response<UdacityCatalog> response) {
if (!response.isSuccessful()) {
Log.i(TAG,"Errorr: " +response.code());
}else{
UdacityCatalog catalog =response.body();
for (Course c : catalog.courses){
Log.i(TAG,c.title);
Log.i(TAG,"--------------");
}
}
}
#Override
public void onFailure(Call<UdacityCatalog> call, Throwable t) {
Log.e(TAG,"Errorr: " + t.getMessage());
}
});
}
}
I want to show data c.title in ListView
Log.i(TAG,c.title) show
I/fong: Richard Kalehoff
I/fong: Firebase Analytics: iOS
I/fong: Firebase Analytics: Android
Thanks for yor help :)
It`s easy, First you have to add a ListView component in your Layout:
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
And then, find it in your JAVA code and put it in a instance variable:
ListView listview;
//in your onCreate() after setContentView():
listview = (ListView) findViewById(R.id.list);
You have to create a list with all your titles:
List<String> courses = new ArrayList<>();
for (Course c : catalog.courses){
courses.add(c.title);
}
Now, you have to create a SimpleAdapter and pass your data to it:
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.r.layout.simple_list_item_1, courses);
And finally, add your adapter in your ListView:
listview.setAdapter(adapter);
Take a list of Course
List<Course> courseList = catalog.courses;
then make a custom list Adapter and from this list get the title and show it in textView of the list
textView.setText(courseList.get(position).title);

where isimple_list_item_1 is declare in adapter parameters android java

In android application I am using adapter for populating ListView control like below:
public class ChildActivity extends ActionBarActivity implements GetChildList {
private ListView lv_child;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
List<String> array = new ArrayList<String>(); // for e.g array ["num1", "num2"]
lv_child = (ListView)findViewById(R.id.lv_child);
ArrayAdapter<String> adapterChild = new ArrayAdapter<String>(ChildActivity.this, android.R.layout.simple_list_item_1,array);
lv_child.setAdapter(adapterChild);
}
}
but i don't understand where its is simple_list_item_1 declare at this line ArrayAdapter<String> adapterChild = new ArrayAdapter<String>(ChildActivity.this, android.R.layout.simple_list_item_1,array);
Kindly suggest me, waiting for reply.

Categories