Can anyone tell me why my spinners are not showing up? I'm doing an exercise and I don't understand why my spinners show no items at all. any suggestions?
import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import java.util.*;
public class Length extends Activity{
private Spinner spi1,spi2,cmbOpciones;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.length);
//Spinner1
List<String> imperialunits = new ArrayList<String>();
imperialunits.add("inch");
imperialunits.add("foot");
imperialunits.add("yard");
imperialunits.add("mile");
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, imperialunits);
spi1=(Spinner)findViewById(R.id.spinner1);
adaptador.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spi1.setAdapter(adaptador);
//Spinner2
final String[] SIunits =
new String[]{"km","m","cm"};
ArrayAdapter<String> adaptador2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, SIunits);
spi2 = (Spinner)findViewById(R.id.spinner2);
adaptador2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spi2.setAdapter(adaptador2);
//Spinner 3
final String[] datos = new String[]{"Elem1","Elem2","Elem3","Elem4","Elem5"};
ArrayAdapter<String> adaptador3 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, datos);
cmbOpciones = (Spinner)findViewById(R.id.CmbOpciones);
adaptador.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cmbOpciones.setAdapter(adaptador3);
}
}
Your code to populate the Spinners looks fine (I think),maybe your problem is on your layout xml?, but why don't you try using an array of strings instead? like here, plus you get to use resource values, I personally think its better not creating stuff like that at runtime.
I tested your code and works perfectly. I agree, it must be something wrong in your xml file.
Related
I'm having a problem with my database when I try to add data from the simulator it puts it in the wrong column. I understand that it begins from 0 like (1 column = 0, 2 column = 1....)
I think I set it to 1 to put the data in the 2 columns but instead it puts it in the 3 columns but when set it to "0" it adds data the first column like it supposed to. Anybody has any ideas why it might be happening?
package com.example.laivumusis;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class ViewListData extends AppCompatActivity {
KlausimynoDatabaseHelper myDB;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editlistview_layout);
ListView listView = (ListView) findViewById(R.id.listView);
myDB = new KlausimynoDatabaseHelper(this);
ArrayList<String> theList = new ArrayList<>();
Cursor data = myDB.getListContents();
if (data.getCount() == 0) {
Toast.makeText(ViewListData.this,"Database tuščias!",Toast.LENGTH_LONG).show();
}else{
while (data.moveToNext()){
theList.add(data.getString(1));
ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,theList);
listView.setAdapter(listAdapter);
}
}
}
}
Don't use the index of the column like this:
theList.add(data.getString(1));
Use the method getColumnIndex() by passing the name of the column, because the order of the columns may not be what you think it is.
So change to this:
theList.add(data.getString(data.getColumnIndex("columnName")));
Replace columnName with the name of the column that you want.
my problem is, that I have a constraint set, which dont work. I have already tried a lot, but nothing worked... The button and the text are at the left side and the text is behind the button.
Here is a screenshot from the result at the moment
I want that the button is at the right side of the grey box and the text at the left top. Is this possible with only code?
package com.example.sbt_local.ticketui;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class MainActivity extends Activity {
private LinearLayout lLayout;
private ConstraintLayout cLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context context = this;
lLayout = new LinearLayout(context.getApplicationContext());
lLayout.setOrientation(LinearLayout.VERTICAL);
cLayout = new ConstraintLayout(context.getApplicationContext());
cLayout.setBackgroundColor(Color.parseColor("#FFEDEDED"));
cLayout.setPadding(4,4,4,4);
cLayout.setId(View.generateViewId());
ConstraintSet set = new ConstraintSet();
set.clone(cLayout);
Button cButton = new Button(context.getApplicationContext());
cButton.setText(">");
cButton.setLayoutParams(new RelativeLayout.LayoutParams(135, ViewGroup.LayoutParams.MATCH_PARENT));
cButton.setId(View.generateViewId());
set.connect(cButton.getId(),ConstraintSet.RIGHT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0);
set.constrainHeight(cButton.getId(), 200);
set.applyTo(cLayout);
TextView tv_startDate = new TextView(context.getApplicationContext());
DateFormat df = new SimpleDateFormat("HH:mm dd.MM.yyyy");
tv_startDate.setText("Test");
tv_startDate.setId(View.generateViewId());
// Constraint Set
System.out.println("Layout ID: " + cLayout.getId());
System.out.println("Button ID: " + cButton.getId());
// Add Elements to Layout
cLayout.addView(cButton);
cLayout.addView(tv_startDate);
// Add Ticket to Layout List
TextView spacer = new TextView(context.getApplicationContext());
spacer.setTextSize(4); // Spacer Size
lLayout.addView(cLayout);
lLayout.addView(spacer);
LinearLayout llLayout = (LinearLayout) findViewById(R.id.layout);
llLayout.addView(lLayout);
}
}
Put this line:
set.applyTo(cLayout);
after the cButton has been added into cLayout:
cLayout.addView(cButton);
cLayout.addView(tv_startDate);
set.applyTo(cLayout);
because before cButton has been added into cLayout, its ConstraintSet.PARENT_ID is undefined.
studio to create an android app that adds a string from an editText with an id of input and displays it into textView with an id of output. But
EditText input = EditText(findViewById(R.id.input));
and
TextView output = TextView(findViewById(R.id.output));
doesnt work as it says Method call expected. Any kind of help would be great, thankyou.
package com.example.toshb.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
private Model model;
public MainActivity() {model = new Model();}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void processInput(View view)
{
EditText input = EditText(findViewById(R.id.input));
TextView output = TextView(findViewById(R.id.output));
model.addString(editText.getText().toString());
output.setText(model.getList());
input.setText("");
}
}
You need to add proper declaration of EditText And TextView . Before write a code please have a look here:
https://developer.android.com/index.html
EditText input = (EditText)findViewById(R.id.input);
TextView output =(TextView)findViewById(R.id.output);
model.addString(input.getText().toString());
I am trying to create a list view in Java that allows each list item to open a web url address but I can not seem to get the code right. Please can some one show me where IO going wrong.
package com.sasquatchapps.hydraquip10.bestofmonsterquest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Season1Activity extends Activity{
private String episodes[] = {"America's Loch Ness Monster","Sasquatch Attack",
"Giant Squid Found","Birdzilla","Bigfoot","“Mutant K9","Lions in the Backyard","Gigantic Killer Fish","Swamp Beast","Russia's Killer Apemen","Unidentified Flying Creatures","The Real Hobbit",
"Giganto: The Real King Kong","American Werewolf"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter = new
ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, episodes);
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(this, "Item clicked;" + episodes[position], Toast.LENGTH_SHORT).show();
if (position == 0) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/watch?v=o7-RdxrCFAg"));
startActivity(intent);
}
}
}
You have some errors - Where is your listview? Modify your code
1) create listview -
ListView myList;
2) In your onCreate assign this list:
myList = (ListView) findViewById(R.id.my_list);
3) Set adapter to your list:
myList.setAdapter(adapter);
I am working on a project that needs to add buttons dynamically. But whenever I run my application the application force closes. I've learned that the problem is when I try to add buttons.
package com.Feras.TestProject;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class TestProject extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AddAll();
// Set Text for the button in the Old Testament
}
public void AddAll() {
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout1);
Button btn = new Button(this);
btn.setText("MyButton");
linearLayout.addView(btn);
}
}
try like this:
linearLayout.addView(
btn,
new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT)
);
there will only occure an error if linearLayout is null, ensure that layout1 is a valid Item of R.layout.main
Try the following in your custom activity class:
this.addContentView(call,
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));