I have radio button group and spinner in my android activity.First, I choose a radio button, then I choose spinner item. I want to see selected spinner item in textView. However, when I change spinner item, textView does not update. It updates only when I change radio button. How can I update textView dynamically? I am trying to product my radio button and spinner and display on textView.
public class GenelBagis extends AppCompatActivity {
Spinner spinnerGenelBagis;
ArrayAdapter<String> dataAdapterForGenelBagis;
String[] aylar;
TextView mText;
int amount=1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_genel_bagis);
aylar = getResources().getStringArray(R.array.month);
spinnerGenelBagis = (Spinner)findViewById(R.id.spinner2);
dataAdapterForGenelBagis = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,aylar);
spinnerGenelBagis.setAdapter(dataAdapterForGenelBagis);
mText = (TextView)findViewById(R.id.textView16);
spinnerGenelBagis.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
RadioButton meslekLisesiButton = (RadioButton) findViewById(R.id.meslekLisesi);
RadioButton yuksekOgrenimButton = (RadioButton) findViewById(R.id.yuksekOgrenim);
RadioButton yuksekLisansButton = (RadioButton) findViewById(R.id.meslekLisesi);
RadioButton doktoraButton = (RadioButton) findViewById(R.id.meslekLisesi);
String text = spinnerGenelBagis.getSelectedItem().toString();
int month = Integer.parseInt(text);
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (meslekLisesiButton.isChecked() == true) {
String amount = Integer.toString(month * 200);
mText.setText(amount);
} else if (yuksekOgrenimButton.isChecked() == true) {
//
} else if (yuksekLisansButton.isChecked() == true) {
//
} else if (doktoraButton.isChecked() == true) {
//
}
}
});
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}}
xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.merve.tev.GenelBagis"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Burs Vermek İstediğiniz Toplam Tutar"
tools:layout_constraintTop_creator="1"
android:layout_marginTop="22dp"
app:layout_constraintTop_toBottomOf="#+id/spinner2"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="279dp"
android:layout_height="130dp"
android:checkedButton="#+id/meslekLisesi"
tools:layout_constraintTop_creator="1"
tools:layout_constraintBottom_creator="1"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="#+id/spinner2"
android:layout_marginTop="92dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="92dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="16dp">
<RadioButton
android:id="#+id/meslekLisesi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Meslek Lisesi (Aylık) 200 TL" />
<RadioButton
android:id="#+id/yuksekOgrenim"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Yüksek Öğrenim (Aylık) 510 TL"
tools:layout_editor_absoluteX="42dp"
tools:layout_editor_absoluteY="169dp" />
<RadioButton
android:id="#+id/yuksekLisans"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Yüksek Lisans (Aylık) 850 TL" />
<RadioButton
android:id="#+id/doktora"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Doktora (Aylık) 1150 TL" />
</RadioGroup>
<Spinner
android:id="#+id/spinner2"
android:layout_width="310dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
tools:layout_constraintTop_creator="1"
android:layout_marginTop="26dp"
app:layout_constraintTop_toBottomOf="#+id/textView15"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp" />
<Button
android:id="#+id/devamEt2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:textSize="15sp"
android:text="Devam Et"
style="#style/Widget.AppCompat.Button.Colored"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="#+id/textView16"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="43dp"
app:layout_constraintLeft_toLeftOf="#+id/textView16" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Öğrenci Eğitim Düzeyine Göre Bağış"
tools:layout_constraintTop_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="#+id/radioGroup1"
android:layout_marginStart="16dp"
android:layout_marginTop="46dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="29dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="16dp" />
<TextView
android:id="#+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kaç Aylık Burs Vermek İstersiniz?"
tools:layout_constraintTop_creator="1"
tools:layout_constraintBottom_creator="1"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="16dp" />
<TextView
android:id="#+id/textView16"
android:layout_width="89dp"
android:layout_height="29dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintBottom_creator="1"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="#+id/devamEt2"
android:layout_marginTop="64dp"
app:layout_constraintTop_toBottomOf="#+id/spinner2"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="61dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp" />
</android.support.constraint.ConstraintLayout>
My activity looks like this:
I've totally changed the approach. Have a look.
public class Test extends AppCompatActivity {
Spinner spinnerGenelBagis;
RadioGroup radioGroup;
ArrayAdapter<String> dataAdapterForGenelBagis;
String[] aylar;
TextView mText;
private static int spinnerValue = 0;
private static int radioValue = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_genel_bagis);
aylar = getResources().getStringArray(R.array.month);
spinnerGenelBagis = (Spinner) findViewById(R.id.spinner2);
dataAdapterForGenelBagis = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, aylar);
spinnerGenelBagis.setAdapter(dataAdapterForGenelBagis);
mText = (TextView) findViewById(R.id.textView16);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.meslekLisesi) {
radioValue = 200;
updateTextView();
} else if (checkedId == R.id.yuksekOgrenim) {
radioValue = 300;
updateTextView();
} else if (checkedId == R.id.yuksekLisans) {
//
} else if (checkedId == R.id.doktora) {
//
}
}
});
spinnerGenelBagis.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String text = spinnerGenelBagis.getSelectedItem().toString();
spinnerValue = Integer.parseInt(text);
updateTextView();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
private void updateTextView() {
String totalAmount = Integer.toString(spinnerValue * radioValue);
mText.setText(totalAmount);
}
}
Hope it helps.
And, you don't need to write yuksekOgrenimButton.isChecked() = true if you used it. yuksekOgrenimButton.isChecked() returns boolean by itself.
Related
I'm new to Android studio and I'm working on a project which needs a spinner that has values populated from SQLite. I need to change an image(icon) according to the selection done by the user from the spinner.
I've tried making android:clickable="false" for the TextView on the custom layout. But it does not solve. I've also used spinner.setOnTouchListener to update the list whenever user touches the spinner.
categoryspinner_layout.xml (custom layout)
<TextView
android:id="#+id/category"
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textAppearance="#style/TextAppearance.AppCompat.Medium">
</TextView>
</LinearLayout>
Spinner Layout. (Has multiple spinners but without custom layout)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewTransfers">
<TextView
android:id="#+id/filterDateStart"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Date"
android:inputType="date"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout3" />
<Button
android:id="#+id/addFilterDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="ADD DATE"
app:layout_constraintStart_toEndOf="#+id/filterDateStart"
app:layout_constraintTop_toBottomOf="#+id/linearLayout3" />
<TextView
android:id="#+id/filterMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Recurring"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/mop_viewTransfer" />
<Button
android:id="#+id/viewTranfers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="56dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="View Transfer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.477"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/recurring_viewTransfer" />
<EditText
android:id="#+id/deleteCatID"
android:layout_width="124dp"
android:layout_height="66dp"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="70dp"
android:ems="10"
android:hint="Enter an ID"
android:inputType="number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/viewTranfers" />
<Button
android:id="#+id/delTransfer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="106dp"
android:layout_marginLeft="106dp"
android:layout_marginTop="70dp"
android:layout_marginEnd="73dp"
android:layout_marginRight="73dp"
android:text="DELETE"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/deleteCatID"
app:layout_constraintTop_toBottomOf="#+id/viewTranfers" />
<Spinner
android:id="#+id/mop_viewTransfer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<Spinner
android:id="#+id/recurring_viewTransfer"
android:layout_width="wrap_content"
android:layout_height="28dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/filterMode" />
<TextView
android:id="#+id/textView4"
android:layout_width="166dp"
android:layout_height="25dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Mode of payment"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/amount_filter" />
<EditText
android:id="#+id/amount_filter"
android:layout_width="142dp"
android:layout_height="42dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="52dp"
android:layout_marginRight="52dp"
android:ems="10"
android:hint="Amount"
android:inputType="numberDecimal"
app:layout_constraintEnd_toStartOf="#+id/currency_filter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/addFilterDate" />
<Spinner
android:id="#+id/currency_filter"
android:layout_width="96dp"
android:layout_height="41dp"
android:layout_marginStart="52dp"
android:layout_marginLeft="52dp"
app:layout_constraintStart_toEndOf="#+id/amount_filter"
app:layout_constraintTop_toBottomOf="#+id/addFilterDate" />
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="60dp"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Spinner
android:id="#+id/filterCategory"
android:layout_width="299dp"
android:layout_height="41dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName" />
<ImageView
android:id="#+id/imageView"
android:layout_width="46dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/imageview1" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
OnItemSelected
package com.example.watch;
import android.app.DatePickerDialog;
import android.database.Cursor;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
public class ViewTransfers extends AppCompatActivity implements DatePickerDialog.OnDateSetListener, AdapterView.OnItemSelectedListener {
EditText deleteIDs, amount;
TextView startDate;
Button view, delete , addDate;
Spinner mop_ViewTransfer,recurring_ViewTransfer,currency,category;
DatabaseHelper myDb;
DataHelper cDb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_transfers);
myDb = new DatabaseHelper(this);
deleteIDs=(EditText) findViewById(R.id.deleteCatID);
category =(Spinner) findViewById(R.id.filterCategory);
startDate =(TextView) findViewById(R.id.filterDateStart);
amount=(EditText) findViewById(R.id.amount_filter);
currency=(Spinner) findViewById(R.id.currency_filter);
recurring_ViewTransfer=(Spinner) findViewById(R.id.recurring_viewTransfer);
mop_ViewTransfer=(Spinner) findViewById(R.id.mop_viewTransfer);
view = (Button) findViewById(R.id.viewTranfers);
delete=(Button) findViewById(R.id.delTransfer);
addDate = (Button) findViewById(R.id.addFilterDate);
implementValue();
addDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DialogFragment datePicker=new DatePickerFrangment();
datePicker.show(getSupportFragmentManager(),"date picker");
}
});
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String categoryS , startDateS, recurrentS, modeS,amountS,currencyS;
categoryS = category.getSelectedItem().toString();
startDateS= startDate.getText().toString();
amountS=amount.getText().toString();
currencyS=currency.getSelectedItem().toString();
modeS= mop_ViewTransfer.getSelectedItem().toString();
recurrentS= recurring_ViewTransfer.getSelectedItem().toString();
if (amountS.length()!=0) amountS = amountS+" "+currencyS;
Cursor res = myDb.getFilteredData( categoryS , startDateS,amountS, modeS,recurrentS);
if (res.getCount() == 0) {
showMessage("Error", "No records found!");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("ID : " + res.getString(0) + "\n");
buffer.append("Category : " + res.getString(1) + "\n");
buffer.append("Date : " + res.getString(2) + "\n");
buffer.append("Amount : " + res.getString(3) + "\n");
buffer.append("Payment : " + res.getString(4) + "\n");
buffer.append("Recurring : " + res.getString(5) + "\n");
buffer.append("Comments : " + res.getString(6) + "\n \n");
}
showMessage("Money Tracker", buffer.toString());
}
});
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text =deleteIDs.getText().toString();
if (text.length() != 0) {
Integer IDs = Integer.parseInt(text) ;
boolean check = myDb.deleteTransfer(IDs);
if (check == true) {
Toast.makeText(ViewTransfers.this, "Data deleted!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ViewTransfers.this, "Something went wrong!", Toast.LENGTH_LONG).show();
}
}
else {
Toast.makeText(ViewTransfers.this,"Enter a valid ID!",Toast.LENGTH_LONG).show();
}
}
});
}
public void showMessage(String title, String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR,year);
c.set(Calendar.MONTH,month);
c.set(Calendar.DAY_OF_MONTH,dayOfMonth);
String currentDateString= DateFormat.getDateInstance().format(c.getTime());
startDate.setText(currentDateString); }
public void implementValue(){
Spinner recurring_ViewTransfer = findViewById(R.id.recurring_viewTransfer);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.transaction, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
recurring_ViewTransfer.setAdapter(adapter);
recurring_ViewTransfer.setOnItemSelectedListener(this);
Spinner mop_ViewTransfer = findViewById(R.id.mop_viewTransfer);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
R.array.mop, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mop_ViewTransfer.setAdapter(adapter2);
mop_ViewTransfer.setOnItemSelectedListener(this);
Spinner currency = findViewById(R.id.currency_filter);
ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this,
R.array.currency, android.R.layout.simple_spinner_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
currency.setAdapter(adapter3);
currency.setOnItemSelectedListener(this);
categoryspin();
/* category.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
categoryspin();
return false;
}
});
*/
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) {
case R.id.filterCategory : {
String category1 = category.getSelectedItem().toString();
Toast.makeText(ViewTransfers.this, category1, Toast.LENGTH_SHORT).show();
/*String category1 = category.getSelectedItem().toString();
String img = cDb.getIcon(category1);
imgIcon = (ImageView) findViewById(R.id.imageView);
int resID = getResources().getIdentifier(img, "drawable", getPackageName());
imgIcon.setImageResource(resID);*/
}
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
public void categoryspin(){
cDb = new DataHelper(this);
ArrayList<String> list = cDb.getAllCategories();
ArrayAdapter<String> adapter3 = new ArrayAdapter<>(this, R.layout.categoryspinner_layout, R.id.category, list);
category.setAdapter(adapter3);
}
}
There ain't any error message. When I open the page it gave me a toast message of the 1st entry in the spinner. After that nothing happens when I click on it.
I reviewed your code, You have to code like this according to your current code, You have implemented event on activity context. and You have not written code for the event listener.
public void categoryspin(){
cDb = new DataHelper(this);
ArrayList<String> list = cDb.getAllCategories();
ArrayAdapter<String> adapter3 = new ArrayAdapter<>(this, R.layout.categoryspinner_layout, R.id.category, list);
category.setAdapter(adapter3);
/*Add this line and it will start working with your existing code to handle spinner onItemSelected
event, Happy coding...*/
category.setOnItemSelectedListener(this);
}
Note: try to use the proper naming convention, It will help you to identify/review code easily and solve bugs.
Also, this issue is not for values you are getting from the SQLite database.
I have developed a screen where a no. of people from the database are displayed in a list view. I want to display the profile page of the selected person. So my question is how to bind each detail of the selected person like name, contact, etc. to the profile page which I have created? Will I have to call the getById API in the onItemClickListener?
Here's the edited code:-
public class Test extends AppCompatActivity {
List<Genie> genieList;
GenieAdapter genieAdapter;
TextView responseView;
ProgressBar progressBar;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
responseView = (TextView) findViewById(R.id.responseView);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
button = (Button) findViewById(R.id.test);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Test.this, "Blahblah", Toast.LENGTH_LONG).show();
new RetrieveFeedTask().execute();
}
});
}
class RetrieveFeedTask extends AsyncTask<Void, Void, List<Genie>> {
private Exception exception;
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
responseView.setText("");
}
protected List<Genie> doInBackground(Void... urls) {
GenieService genieService = new GenieService();
return genieService.getAll();
}
protected void onPostExecute(List<Genie> genies) {
if (genies == null) {
new ArrayList<Genie>(); // "THERE WAS AN ERROR"
} else {
progressBar.setVisibility(View.GONE);
Log.i("INFO", genies.get(0).name);
List<String> rows = genies.stream().map(genie -> getRow(genie)).collect(Collectors.toList());
genieAdapter=new GenieAdapter(getApplicationContext(),R.layout.genie_list, genies);
ListView list=(ListView)findViewById(R.id.listViewMain);
list.setAdapter(genieAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(Test.this, "" + position, Toast.LENGTH_SHORT).show();
// if (position == 1) {
// startActivity(new Intent(Test.this, viewGenie1.class));
// }
}
});
list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Test.this, viewGenie1.class);
intent.putExtra("name", "%s");
intent.putExtra("add", "%s");
intent.putExtra("phn", "%s");
intent.putExtra("sal", "%s");
intent.putExtra("lea", "%s");
startActivity(intent);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
private String getRow(Genie g) {
return String.format("%s, %s, %s, %s, %s", g.name, g.salary, g.contact, g.paid_leaves, g.address);
}
}
}
Here's the viewGenie1.class:-
public class viewGenie1 extends AppCompatActivity implements View.OnClickListener {
TextView name;
EditText address, contact, salary, leaves;
Button attendance;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_genie1);
name = (TextView) findViewById(R.id.txName);
address = (EditText) findViewById(R.id.txAddress);
contact = (EditText) findViewById(R.id.txContact);
salary = (EditText) findViewById(R.id.txSalary);
leaves = (EditText) findViewById(R.id.txLeaves);
Button update=(Button)findViewById(R.id.btUpdate);
update.setOnClickListener(this);
Button delete=(Button)findViewById(R.id.delete);
delete.setOnClickListener(this);
Button attendance = (Button) findViewById(R.id.attendance);
attendance.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showAtt();
}
});
String value = "";
if (getIntent().hasExtra("name")) {
String name = getIntent().getExtras().getString("name");
String add = getIntent().getExtras().getString("add");
String phn = getIntent().getExtras().getString("phn");
String sal = getIntent().getExtras().getString("sal");
String lea = getIntent().getExtras().getString("lea");
}
name.setText(value);
address.setText(value);
contact.setText(value);
salary.setText(value);
leaves.setText(value);
}
#Override
public void onClick(View view) {
final AlertDialog.Builder builder=new AlertDialog.Builder(viewGenie1.this);
builder.setMessage("Are you sure you want to delete records?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
new deleteTask().execute();
Toast.makeText(viewGenie1.this, "Genie deleted..!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(viewGenie1.this, navDrawer.class));
// GenieService genieService=new GenieService();
// genieService.delete(2);
// Log.d("Information", String.valueOf(genieService.delete(2)));
// Log.i("INFO", genies.get(0).name);
// startActivity(new Intent(viewGenie1.this,Test.class));
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
private class deleteTask extends AsyncTask {
#Override
protected Object doInBackground(Object[] objects) {
GenieService genieService = new GenieService();
return genieService.delete(6);
}
}
public void showAtt() {
Intent intent = new Intent(this, viewAbsentee.class);
startActivity(intent);
}
}
Here's the xml file of the profile page I have created with hard coded values but want to display the actual values from the local mysql database using an API call:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bcak"
tools:context="com.codionics.geniem.AddGenie"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="313dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#drawable/gradientbackground"
android:orientation="vertical">
<ImageView
android:layout_width="117dp"
android:layout_height="117dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:src="#drawable/genie" />
<TextView
android:id="#+id/txName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Abc"
android:textColor="#ffffff"
android:textSize="21sp"
android:textStyle="bold" />
</LinearLayout>
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="115dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="175dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact"
android:textColor="#f000"
android:textStyle="bold"
android:textSize="20sp" />
<EditText
android:id="#+id/txContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="123456789"
android:textColor="#3F51B5"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:textColor="#f000"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="#+id/txAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="Pune"
android:textColor="#3F51B5"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
<LinearLayout
android:layout_width="360dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="42dp"
android:paddingLeft="25dp">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:src="#drawable/ic_attach_money_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="20dp"
android:text="Paid leaves : "
android:textColor="#303F9F"
android:textSize="27dp"
android:textStyle="bold" />
<EditText
android:id="#+id/txLeaves"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:textSize="20dp"
android:textStyle="bold"
android:layout_weight="1"
android:text=" 5" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="42dp"
android:paddingLeft="25dp">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:src="#drawable/ic_money" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="20dp"
android:text="Salary : "
android:textColor="#303F9F"
android:textSize="27dp"
android:textStyle="bold" />
<EditText
android:id="#+id/txSalary"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:textSize="20dp"
android:textStyle="bold"
android:text=" 5000"
android:textColor="#123" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/btUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginTop="30dp"
android:background="#drawable/buttonstylegradient"
android:text="Update Genie"
android:textColor="#fff" />
<Button
android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="80dp"
android:layout_marginTop="-50dp"
android:background="#drawable/buttonstylegradient"
android:text="Delete Genie"
android:textColor="#fff" />
<Button
android:id="#+id/attendance"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/buttonstylegradient"
android:textColor="#fff"
android:text="Attendance" />
</LinearLayout>
I want to display the details in the a profile page like this:-
profile page
Please pass the value in Intent using putExtra()
Intent intent = new Intent(Test.this, viewGenie1.class);
intent.putExtra("key","Value"); //Key must be unique and value should be the value which you want to pass to viewGenie1 class.
startActivity(intent);
In viewGenie1 class you can get the value like this
String value="";
if(getIntent().hasExtra("key")) {
value = getIntent().getExtras().getString("key");
}
Please replace
String value = "";
if (getIntent().hasExtra("name")) {
String name = getIntent().getExtras().getString("name");
String add = getIntent().getExtras().getString("add");
String phn = getIntent().getExtras().getString("phn");
String sal = getIntent().getExtras().getString("sal");
String lea = getIntent().getExtras().getString("lea");
}
name.setText(value);
address.setText(value);
contact.setText(value);
salary.setText(value);
leaves.setText(value);
To
String mName = "",mAdd="",mPhn="",mSal="",mLea="";
if (getIntent().hasExtra("name")) {
mName = getIntent().getExtras().getString("name");
mAdd = getIntent().getExtras().getString("add");
mPhn = getIntent().getExtras().getString("phn");
mSal = getIntent().getExtras().getString("sal");
mLea = getIntent().getExtras().getString("lea");
}
name.setText(mName);
address.setText(mAdd);
contact.setText(mPhn);
salary.setText(mSal);
leaves.setText(mLea);
I have 2 two images (item1.png and item2.png) and I want to show each image in a ImageView when the items on the spinner are clicked. When the "item1" in the spinner is selected I want to show the "item1.png" image and the same for the "item2".
Im trying to do this with the code below but its no working. Do you know what is the issue?
On MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner2);
String string = String.valueOf(spinner.getSelectedItem());
final ImageView image = (ImageView)findViewById(R.id.image);
int image1 = R.drawable.item1;
int image2 = R.drawable.item2;
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//spinner.
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2) {
if (arg2 == 0) {
image.setImageResource(R.drawable.item1);
} else {
image.setImageResource(R.drawable.item2);
}
}
}
}
}
Activity xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ricardorei.tpc.MainActivity">
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="48dp"
android:text="Button"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="199dp"
android:layout_marginRight="58dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginEnd="58dp" />
<ImageView
android:id="#+id/image"
android:layout_width="0dp"
android:layout_height="220dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="#drawable/item1" />
<TextView
android:id="#+id/textView2"
android:layout_width="321dp"
android:layout_height="28dp"
android:layout_marginBottom="32dp"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:text="Are Awesome!"
app:layout_constraintBottom_toTopOf="#+id/imageView2"
app:layout_constraintHorizontal_bias="0.517"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="295dp"
android:layout_height="28dp"
android:text="TextView"
app:layout_constraintBaseline_toBaselineOf="#+id/button3"
android:layout_marginLeft="32dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginStart="32dp" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="134dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:entries="#array/spinner_values"
app:layout_constraintHorizontal_bias="0.522"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>
Try to change OnItemSelectedListener like below:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
image.setImageResource(R.drawable.item1);
break;
case 1:
image.setImageResource(R.drawable.item2);
break;
default:
//Default image
//image.setImageResource(R.drawable.item2);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
(First of all sorry my bad english)
I am doing an app about routes in my town, is like a project of my grade, and I have it done it all but I don't know what is the problem..
My problem is that the ListView that I made from a ViewHolder it doesn't show up when I start the app.
My code is this:
MainActivity:
public class RutasActivity extends AppCompatActivity {
private static final String LOGO = "";
ImageButton btnImgAtras;
private ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rutas);
lv = (ListView)findViewById(R.id.lvRutas);
RutasParser parser = new RutasParser(this);
if(parser.parse()) {
Ruta[] rutas = parser.getRutas();
RutaAdapter adapter = new RutaAdapter(this, rutas);
lv.setAdapter(adapter);
Toast.makeText(this, "Enseñando", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "No se pudieron obtener los datos de los países", Toast.LENGTH_SHORT).show();
}
btnImgAtras = (ImageButton)findViewById(R.id.imgBtnAtras);
btnImgAtras.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(RutasActivity.this, MainActivity.class);
startActivity(i);
}
});
}
}
Activity Layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RutasActivity"
>
<ImageView
android:layout_width="45dp"
android:layout_height="44dp"
app:srcCompat="#drawable/escudoazulsinfondo"
android:id="#+id/imageView5"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:contentDescription="" />
<TextView
android:text="#string/Rutas_Javea"
android:textSize="20sp"
android:textColor="#color/Blacky"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView4"
android:layout_marginTop="32dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp" />
<ImageButton
android:layout_width="32dp"
android:layout_height="32dp"
app:srcCompat="#drawable/atras"
android:id="#+id/imgBtnAtras"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:background="#android:color/transparent"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp" />
<ListView
android:layout_width="312dp"
android:layout_height="358dp"
android:id="#+id/lvRutas"
android:layout_marginStart="24dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="24dp"
android:layout_marginEnd="24dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="24dp"
app:layout_constraintHorizontal_bias="0.45"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="24dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent" >
</ListView>
<ImageView
android:layout_width="105dp"
android:layout_height="36dp"
app:srcCompat="#drawable/depturismenofondo"
android:id="#+id/imageView6"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
tools:ignore="ContentDescription" />
</android.support.constraint.ConstraintLayout>
Then, my AdapterView:
public class RutaAdapter extends ArrayAdapter<Ruta> {
Ruta[] rutas;
public RutaAdapter(Context context, Ruta[]rutas) {
super(context, R.layout.listitem_ruta, rutas);
this.rutas = rutas;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
ViewHolder viewHolder;
if(item == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
item = inflater.inflate(R.layout.listitem_ruta, null);
viewHolder = new ViewHolder();
viewHolder.vhKm = (TextView) item.findViewById(R.id.listKm);
viewHolder.vhNombreRuta = (TextView) item.findViewById(R.id.listNombre);
viewHolder.vhDesnivel = (TextView) item.findViewById(R.id.listDesnivel);
viewHolder.vhDificultad = (TextView) item.findViewById(R.id.listDificultad);
viewHolder.vhTiempoTotal = (TextView) item.findViewById(R.id.listTiempo);
item.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)item.getTag();
}
viewHolder.vhKm.setText(String.valueOf(rutas[position].getDistancia()));
viewHolder.vhNombreRuta.setText(rutas[position].getNombre());
viewHolder.vhDificultad.setText(rutas[position].getNivel_dificultad());
viewHolder.vhTiempoTotal.setText(rutas[position].getTiempo_efectivo());
viewHolder.vhDesnivel.setText(String.valueOf(rutas[position].getDesnivel_acumulado_ascenso()));
return item;
}
static class ViewHolder {
TextView vhNombreRuta;
TextView vhKm;
TextView vhDificultad;
TextView vhTiempoTotal;
TextView vhDesnivel;
}
}
My parser:
public class RutasParser {
private Ruta[] rutas;
private InputStream rutasFile;
public RutasParser(Context c) {
this.rutasFile = c.getResources().openRawResource(R.raw.rutas);
}
public boolean parse() {
boolean parsed = false;
rutas = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(rutasFile);
Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("country");
rutas = new Ruta[items.getLength()];
for (int i = 0; i < items.getLength(); i++) {
Node item = items.item(i);
int rutaId = Integer.parseInt(item.getAttributes().getNamedItem("code").getNodeValue());
String rutaNombre = item.getAttributes().getNamedItem("name").getNodeValue();
String rutaDescrip = item.getAttributes().getNamedItem("description").getNodeValue();
double rutaDistance = Double.valueOf(item.getAttributes().getNamedItem("distance").getNodeValue());
String rutaHora = item.getAttributes().getNamedItem("horas").getNodeValue();
String rutaDif = item.getAttributes().getNamedItem("dificultad").getNodeValue();
int rutaElev = Integer.valueOf(item.getAttributes().getNamedItem("elevacion").getNodeValue());
rutas[i] = new Ruta(rutaId, rutaNombre, rutaDescrip, rutaDistance, rutaDif, rutaHora, rutaElev);
}
parsed = true;
} catch (ParserConfigurationException pce) {
Log.e("CountryParser", "ParserConfigurationException: "+pce.getLocalizedMessage());
} catch (Exception e) {
Log.e("CountryParser", "Unknown Exception: "+e.getLocalizedMessage());
}
return parsed;
}
public Ruta[] getRutas() {
return this.rutas;
}
}
And the ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00.00km"
android:textSize="30sp"
android:id="#+id/listKm"
android:layout_gravity="center_horizontal|center_vertical"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:paddingLeft="3dp"
android:paddingStart="3dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="nombre de ruta"
android:id="#+id/listNombre"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listDificultad"
android:text="dificultad"
android:paddingRight="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listTiempo"
android:text="000000"
android:paddingRight="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listDesnivel"
android:text="000000"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
At first place I don´t see any error but it doesn´t show up in my app..
If you can help me, I would be grateful..
Thanks
Photo of what I tell you
I'm trying to create a custom dialog for the settings of my application, the problem is, that the custom buttons, EditText & the spinner don't respond to user activity. I hope someone can help.
Here is my Code:
public class SettingsDialog extends DialogFragment {
private EditText editText;
private Spinner ipSpinner;
private MainActivity mainActivity;
private Settings settings;
private int selectedIP;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_settings, null);
mainActivity = (MainActivity) getActivity();
settings = mainActivity.getSettings();
selectedIP = settings.getSelectedIP();
editText = (EditText) dialogView.findViewById(R.id.edit_text_ip);
ipSpinner = (Spinner) dialogView.findViewById(R.id.spinner_ip);
ImageButton imageButton = (ImageButton) dialogView.findViewById(R.id.image_button_save_ip);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String IP = editText.getText().toString();
settings.setIP(selectedIP, IP);
settings.settingsSave();
mainActivity.showToast("IP at " + Integer.toString(selectedIP) + " with value" + IP);
}
});
ipSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
selectedIP = position;
editText.setText(settings.getIP(selectedIP));
mainActivity.showToast(Integer.toString(position));
settings.settingsSave();
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
return;
}
});
builder.setView(inflater.inflate(R.layout.dialog_settings, null))
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
String IP = editText.getText().toString();
settings.setIP(selectedIP, IP);
settings.settingsSave();
mainActivity.showToast("IP at " + Integer.toString(selectedIP) + " with value" + IP);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SettingsDialog.this.getDialog().cancel();
}
});
return builder.create();
}
}
and my xml for the dialog:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IP-address"
android:textAllCaps="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Spinner
android:id="#+id/spinner_ip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="#array/spinner_ip_visual"
android:entryValues="#array/spinner_ip_values" />
<EditText
android:id="#+id/edit_text_ip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="8" />
<ImageButton
android:id="#+id/image_button_save_ip"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_done_black_24dp"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unit"
android:textAllCaps="true" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/button_unit_celsius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Celsius"
android:textAllCaps="true" />
<RadioButton
android:id="#+id/button_unit_fahrenheit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fahrenheit"
android:textAllCaps="true" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClickGenerateRandomData"
android:text="Generate random data"
android:textAllCaps="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClickDropDatabase"
android:text="delete weatherdata"
android:textAllCaps="true" />
Thanks in advice
You are inflating dialogView and then you set listeners for the views that are in dialogView, however when you set the view for dialog builder, you inflate the new view.
This is wrong.
builder.setView(inflater.inflate(R.layout.dialog_settings, null))
This is correct
builder.setView(dialogView)