When I first launch my app everything displays and works correctly but when I close and reopen it a spinner and button that sit next to each other at the top of my relative layout are only about half of their usual height and the spinner is unusable. If I click it nothing drops down (but the button works as usual). Other elements below the spinner and but display as usual.
I spent half the day googling and playing with the app code and layout XML and have made little to no progress.
Anyone got a clue as to what could be causing this? It only happens on subsequent runs of the app.
Here is the layout XML:
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Spinner
android:id="#+id/namespinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/button1" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/namespinner"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/namespinner"
android:onClick="onEditButtonPressed"
android:text="#string/dob_edit" />
<TextView
android:id="#+id/secondsDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="36dp"
android:text="#string/seconds"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="48sp" />
<TextView
android:id="#+id/minutesDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/secondsDisplay"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:text="#string/minutes"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="48sp" />
<TextView
android:id="#+id/hoursDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/minutesDisplay"
android:layout_marginBottom="27dp"
android:text="#string/hours"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="48sp" />
<TextView
android:id="#+id/hourstext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/hoursDisplay"
android:layout_alignBottom="#+id/hoursDisplay"
android:layout_alignLeft="#+id/daystext"
android:text="#string/hours_lable"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/dayDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/hoursDisplay"
android:layout_centerHorizontal="true"
android:layout_marginBottom="22dp"
android:text="#string/days"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="48sp" />
<TextView
android:id="#+id/daystext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/dayDisplay"
android:layout_toRightOf="#+id/namespinner"
android:text="#string/days_lable"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/minutesText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/minutesDisplay"
android:layout_alignBottom="#+id/minutesDisplay"
android:layout_alignLeft="#+id/hourstext"
android:text="#string/minutes_lable"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/secondstext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/secondsDisplay"
android:layout_alignBottom="#+id/secondsDisplay"
android:layout_alignLeft="#+id/minutesText"
android:text="#string/seconds_lable"
android:textAppearance="?android:attr/textAppearanceSmall" />
This should be the relevant parts of the main activity that call it:
public class MainActivity extends Activity {
static Spinner nameSpinner;
ArrayAdapter nameAdapter;
static ArrayList nameList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onResume() {
super.onResume();
// get spinner and init array if needed
if (nameList == null) {
nameSpinner = (Spinner) findViewById(R.id.namespinner);
nameList = new ArrayList();
nameList.add("( Add )");
}
// populate spinner
nameAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item, nameList);
nameSpinner.setAdapter(nameAdapter);
// listen for events/selections on the spinner
nameSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView parentView,
View selectedItemView, int position, long id) {
// read state
}
}
#Override
public void onNothingSelected(AdapterView parentView) {
// do I want to do anything here?
}
});
}
}
Edited to clean up the code display and add the class def. and class variables.
You have to insert the code wrote in the onresume method in oncreate method!
Setting the spinner in an if statement that didn't really have anything to do with setting the spinner was the cause of the problem. By changing this:
if (nameList == null) {
nameSpinner = (Spinner) findViewById(R.id.namespinner);
nameList = new ArrayList();
nameList.add("( Add )");
}
to
nameSpinner = (Spinner) findViewById(R.id.namespinner);
if (nameList == null) {
nameList = new ArrayList();
nameList.add("( Add )");
}
got it sorted.
A good nights sleep and fresh eyes where all that were needed. :P
Related
I have a little problem in my android app. I want to put a word in an AutoCompleteTextView and then jump to a specific Activity(depend on the word that the user gave).
The problem is that, when i give a specific word, the program does not correspond with the correct answer as expected but returns a wrong toast message. However with the log that i put i see the correct answer. I think that the solution is silly but i stuck and need to solve this.
the code:
Activity.java
public class paralActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paral);
final String [] temp = {"one","two","three"};
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
actv.clearListSelection();
final RelativeLayout myRelative = (RelativeLayout) findViewById(R.id.find);
myRelative.setVisibility(View.INVISIBLE);
ImageView myImage = (ImageView) findViewById(R.id.aktoploika);
myImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(paralActivity.this, topParalActivity.class);
startActivity(intent);
}
});
ImageView myOtherImage = (ImageView) findViewById(R.id.aeroporika);
myOtherImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myRelative.setVisibility(View.VISIBLE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(paralActivity.this, android.R.layout.select_dialog_item, temp);
//Getting the instance of AutoCompleteTextView
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
actv.setThreshold(1);//will start working from first character
actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
actv.setTextColor(Color.RED);
ImageView findBeach = (ImageView) findViewById(R.id.find_beach);
findBeach.setOnClickListener(new View.OnClickListener() {
#Override
** public void onClick(View view) {
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
String choice = actv.getText().toString();
Log.i("Answer",choice);
if (choice == "one"){
Intent firstIntent = new Intent(paralActivity.this, nauagioActivity.class);
startActivity(firstIntent);
}else if (choice == temp[1]){
Intent secondIntent = new Intent(paralActivity.this, gerakasActivity.class);
startActivity(secondIntent);
}else if (choice == temp[2]){
Intent thirdIntent = new Intent(paralActivity.this, limnionasActivity.class);
startActivity(thirdIntent);
}else{
Toast.makeText(paralActivity.this,"wrong",Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}
To help u understand, in this activity a have 2 imageViews. When the user presses the second, a relativeLayout appears (with an AutoCompleteTextView and a button inside). After the user writes the word when presses the button it must go to the specific activity. I declared a String Array (temp[3]) with three words inside and 3 activities for each of the words.
The problem starts in the last onclick method that i put ** . Every time i put a correct word from the Array i take the Toast message but in the log i see the correct.
here is Activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_paral"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/main"
android:scaleX="2"
tools:context="com.example.billy.zakynthosapp.paralActivity">
<TextView
android:id="#+id/categories"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#+id/welcome"
android:text="#string/categories"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:textSize="11sp"
android:textStyle="bold"
android:textColor="#color/welcome"
android:textAllCaps="true"/>
<ImageView
android:id="#+id/aktoploika"
android:layout_centerHorizontal="true"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_below="#id/categories"
android:layout_marginTop="35dp"
android:scaleType="centerCrop"
android:src="#drawable/par1" />
<TextView
android:id="#+id/aktoploika_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/aktoploika"
android:layout_alignTop="#+id/aktoploika"
android:layout_alignRight="#+id/aktoploika"
android:layout_alignBottom="#+id/aktoploika"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/paralies"
android:textSize="22sp"
android:textColor="#color/categories"
android:textStyle="bold"/>
<ImageView
android:id="#+id/aeroporika"
android:layout_centerHorizontal="true"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_below="#id/aktoploika"
android:layout_marginTop="35dp"
android:scaleType="centerCrop"
android:src="#drawable/par2" />
<TextView
android:id="#+id/aeroporika_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/aeroporika"
android:layout_alignTop="#+id/aeroporika"
android:layout_alignRight="#+id/aeroporika"
android:layout_alignBottom="#+id/aeroporika"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/search"
android:textSize="22sp"
android:textColor="#color/categories"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/aeroporika"
android:layout_marginTop="50dp"
android:layout_centerInParent="true"
android:background="#color/categories"
android:scaleX="0.5">
<TextView
android:id="#+id/textView_1"
android:layout_width="wrap_content"
android:gravity="center_vertical"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:text="#string/find_paral"
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/find_beach"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="200dp"
android:src="#drawable/find"
android:onClick="find"/>
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView_1"
android:layout_marginTop="10dp"
android:ems="10"
android:text="">
<requestFocus />
</AutoCompleteTextView>
</RelativeLayout>
Can anyone help me with this?
Thanks a lot!
Try choice.equals("one") instead of choice == "one"
I'm to new to android and I'm trying to develop an UI ,but it is giving me the following error,where I'm developing UI that contains text beside to that spinner...
java.lang.ClassCastException: android.widget.Spinner cannot be cast to android.widget.TextView
and my setting.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/minute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/mile"
android:layout_below="#+id/time"
android:text="#string/min" />
<TextView
android:id="#+id/distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/time"
android:layout_marginTop="34dp"
android:text="#string/Distance" />
<TextView
android:id="#+id/mile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/notification"
android:layout_below="#+id/distance"
android:text="#string/mile"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/minute"
android:layout_marginLeft="41dp"
android:layout_marginTop="47dp"
android:text="#string/Notifications"
android:textSize="20sp" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/notification"
android:layout_below="#+id/mile"
android:layout_marginTop="62dp"
android:text="#string/Time" />
<Spinner
android:id="#+id/distanceview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/time"
android:layout_alignTop="#+id/distance"
android:layout_marginLeft="22dp"
android:layout_toRightOf="#+id/mile" />
<Spinner
android:id="#+id/timeview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/notification"
android:layout_alignLeft="#+id/distanceview"
android:layout_alignTop="#+id/time" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/timeview"
android:layout_centerVertical="true"
android:layout_marginLeft="24dp" />
</RelativeLayout>
and my java code
public class SettingsActivity extends SherlockActivity implements AdapterView.OnItemSelectedListener {
private TextView distance, time;
private Spinner spin;
String spin_val;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
addDistanceRadioButtons();
addTimeRadioButtons();
}
public void addDistanceRadioButtons() {
spin = (Spinner) findViewById(R.id.distanceview);
final String[] item = { "0.1", "0.2", "0.4", "0.6", "0.8", "1.0", "1.2" }
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
// TODO Auto-generated method stub
spin_val = item[position];
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
ArrayAdapter<String> spin_adapter = new ArrayAdapter<String>(getBaseContext(), R.layout.settings, item);
spin.setAdapter(spin_adapter);
}
}
It may Cause Because of You Are Trying to get the Spinner As a TextView
Its seems that you have did code like:
Spinner mSpinner = (TextView) findViewById (R.id.spinner);
Instead, it should be casted properly as:
Spinner mSpinner = (Spinner) findViewById (R.id.spinner);
Or
You may use Spinner id instaed of TextView Id
-----------------------------------------------------------------------------------------------------------------------------------
Your Problem # this line: the id distance is TextView not Spinner
spin = (Spinner) findViewById(R.id.distance);
I Think You need to use id distanceview instead od distance
this line:
spin = (Spinner) findViewById(R.id.distance);
should be
spin = (Spinner) findViewById(R.id.distanceview);
I think you give wrong ID to spinner. Maybe you gave text id to spinner that's why you are getting this. If you paste your Java code here then we will also see that mistake.
Or just clean your project after run your project. Sometimes some views keep their resource id and mash up all ids, so just clean and run.
I have a Spinner in my class with certain items. I have to display an EditText whose visibility I have set to "Invisible". I need to show this EditText when "Others" item is selected in the spinner.
Here is my code:-
Activity class:-
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.maintenance);
ETServiceStation = (EditText)findViewById(R.id.ETServiceStation);
ETServiceType = (EditText)findViewById(R.id.ETServiceType);
ETServiceCost= (EditText)findViewById(R.id.ETServiceCost);
spinServiceType = (Spinner)findViewById(R.id.spinServiceType);
btnMaintenenceSave= (ImageButton)findViewById(R.id.btnMaintenenceSave);
btnMaintenanceClear = (ImageButton)findViewById(R.id.btnMaintenenceClear);
spinServiceType.setOnItemSelectedListener(this);
btnMaintenanceClear.setOnClickListener(this);
btnMaintenenceSave.setOnClickListener(this);
ArrayAdapter<String> serviceTypeAdapter = new ArrayAdapter<String>(getApplicationContext(), R.array.service_type_array, android.R.layout.simple_spinner_item);
serviceTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinServiceType.setAdapter(serviceTypeAdapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int servicePos, long id)
{
// TODO Auto-generated method stub
spinnerServiceType = spinServiceType.getItemAtPosition(servicePos).toString();
if(spinnerServiceType.matches("Others"))
{
ETServiceType = (EditText)findViewById(R.id.ETServiceType);
ETServiceType.setVisibility(View.VISIBLE);
spinnerServiceType = ETServiceType.getText().toString();
}
serviceType = spinnerServiceType;
}
}
Resource:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="service_type_array">
<item>Full Service</item>
<item value="serviceother">Other</item>
</string-array>
</resources>
maintenance.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"
android:padding="30dp">
<TextView
android:id="#+id/txtMTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Maintenance"
android:layout_gravity="center_horizontal"
android:textSize="25sp"
android:textColor="#color/SteelBlue"
android:typeface="sans"/>
<TextView
android:layout_marginTop="60dp"
android:id="#+id/txtServiceStation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Service station name:" />
<EditText
android:id="#+id/ETServiceStation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/txtServiceType"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Service type: "/>
<Spinner
android:id="#+id/spinServiceType"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/ETServiceType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:visibility="invisible"
android:ems="10" />
<TextView
android:id="#+id/txtServiceCost"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Servicing expence:" />
<EditText
android:id="#+id/ETServiceCost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="50dp"
android:orientation="horizontal">
<ImageButton
android:id="#+id/btnMaintenenceSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:src="#drawable/fuelbtnsaveclick" />
<ImageButton
android:id="#+id/btnMaintenenceClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:background="#color/white"
android:src="#drawable/fuelbtnclearclick" />
</LinearLayout>
</LinearLayout>
Database:
public long createMainEntry(MaintenanceExpense me)
{
ContentValues mcv = new ContentValues();
mcv.put(KEY_SERVICE_STATION,me.getServiceStation());
mcv.put(KEY_SERVICE_TYPE,me.getServiceType());
mcv.put(KEY_SERVICE_COST,me.getServiceCost());
return otherInfoDatabase.insert(MAINTENANCE_DATABASE_TABLE, null, mcv);
}
I have to display the ETServiceType when "Others" item is clicked.
Thanks for any help.
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if(spinServiceType.getItemAtPosition(position).toString().equalsIgnorecase("Others")
{
editextObject.setVisibility(View.VISIBLE);
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
// TODO Auto-generated method stub
spinnerServiceType = spinServiceType.getItemAtPosition(position).toString();
//if spinnerServiceType 's value is other
//then set visibility of textview visible put text watcher on it so u can get text when user finished ..if u dont want then u can use simple getText then change its visibility
//As u are setting your array value from resource you cant update it programmatically
//so first of all u have to take its array items in list . so u can update this list after user enter the value in edit text
}
I've created a popup window and put a spinner inside it. But I can't get values when an item is selected from it.
My code to create popup:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popwindow=new PopupWindow(inflater.inflate(R.layout.addpain, null,false),300,350,true);
popwindow.showAtLocation(this.findViewById(R.id.tabHost), Gravity.CENTER, 0, 0);
XML file of PopUp
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#A0BBBBBB">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#A0BBBBBB" >
<TextView
android:id="#+id/addpaintext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/addpain"
android:textColor="#000000"
android:textSize="18dp"
android:typeface="serif" />
<EditText
android:id="#+id/statustbox"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/addpaintext"
android:layout_marginTop="17dp"
android:hint="#string/addpain"
android:inputType="textMultiLine"
android:textColor="#000000"
android:textSize="20dp"
android:typeface="serif" />
<Button
android:id="#+id/addpainbutton"
android:layout_width="80dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/statustbox"
android:layout_marginLeft="18dp"
android:layout_marginTop="98dp"
android:onClick="statusupload"
android:text="#string/statusupload" />
<Button
android:id="#+id/cancelbutton"
android:layout_width="80dp"
android:layout_height="45dp"
android:layout_alignBaseline="#+id/addpainbutton"
android:layout_alignBottom="#+id/addpainbutton"
android:layout_alignParentRight="true"
android:layout_marginRight="18dp"
android:onClick="canceladdpain"
android:text="#string/cancel" />
<Spinner
android:id="#+id/spinner_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/addpainbutton"
android:layout_alignRight="#+id/cancelbutton"
android:layout_below="#+id/statustbox"
android:layout_marginTop="20dp"
android:prompt="#string/testtxt"
android:entries="#array/list"/>
</RelativeLayout>
</LinearLayout>
My StatusUpload Function:
public void statusupload(View view)
{
EditText status=(EditText) popwindow.getContentView().findViewById(R.id.statustbox);
Spinner spinner=(Spinner) popwindow.getContentView().findViewById(R.id.spinner_list);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id)
{
category=parent.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
category="Miscellaneous";
}
});
Toast.makeText(getBaseContext(),category, Toast.LENGTH_LONG).show();
}
What happens is the vraible category isnt getting any values !! IT's just blank.
you're not seeing the results you want because everything hinges on the button to turn on your spinner listener. thus you have have to click the button, make a spinner selection, and then click the button again. I'd seriously reconsider the design. For instance, are you aware that you don't need the listener on to get the item selected? you could just use:
public void statusupload(View view)
{
EditText status=(EditText) popwindow.getContentView().findViewById(R.id.statustbox);
Spinner spinner=(Spinner) popwindow.getContentView().findViewById(R.id.spinner_list);
category = spinner.getSelectedItem().toString();
Toast.makeText(getBaseContext(),category, Toast.LENGTH_LONG).show();
}
I am making a ListView with TextView and 1 delete button for each row.
To populate the list I am using my custom adaptor (extends base adapter) and sqlite db to map into list.
My requirement is onclick of delete button in a row that record should be deleted and list should refresh.
I am able to delete record from db but my list is not refreshing until I rotate the device or assign a new instance of my adapter from activity.
I have tried following answer
but didn't work in my case. the difference between this answer and my case is I am using baseAdapter and he is using cursorAdapter.
public class BookmarksPDFAdapter extends BaseAdapter {
public View getView(int position, View convertView, ViewGroup parent) {
openDatabase();
btnDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
deleteBookmark(getLocation(v));//getlocation(View) method returns which delete button clicked
notifyDataSetChanged();
}
});
}
closeDatabase();
return convertView;
}
my activity looks like
public class BookmarkActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bookmarks);
btnEdit = (Button) findViewById(R.id.edit_bookmarks);
btnAdd = (Button) findViewById(R.id.add_bookmarks);
list = (ListView) findViewById(android.R.id.list);
adapter = new BookmarksPDFAdapter(this);
list.setAdapter(adapter);
}
bookmark.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10.0"
android:paddingTop="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:weightSum="1.0"
android:layout_marginRight="5dip">
<ImageView
android:id="#+id/iconShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/delete_icon"
android:visibility="invisible"
android:layout_weight="1.0"/>
</LinearLayout>
<TextView
android:id="#+id/bookmark_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_weight="7.0"
android:gravity="center_horizontal|center_horizontal"
android:lines="1"
android:text="#+id/TextView01"
android:textSize="24dp" />
<Button
android:id="#+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="2.0"
android:text="#string/btn_txt_delete"
android:visibility="invisible" >
</Button>
listitem.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10.0"
android:paddingTop="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:weightSum="1.0"
android:layout_marginRight="5dip">
<ImageView
android:id="#+id/iconShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/delete_icon"
android:visibility="invisible"
android:layout_weight="1.0"/>
</LinearLayout>
<TextView
android:id="#+id/bookmark_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_weight="7.0"
android:gravity="center_horizontal|center_horizontal"
android:lines="1"
android:text="#+id/TextView01"
android:textSize="24dp" />
<Button
android:id="#+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="2.0"
android:text="#string/btn_txt_delete"
android:visibility="invisible" >
</Button>
deleteBookmark method
void deleteBookmark(int wantedChild) {
String bookmarkItem = getBookmarkItemText(wantedChild, true);
datasource.open();
int check = datasource.deleteBookmark(bookmarkItem);
if (check == 1) {
btnDelete = (Button) (viewList.get(wantedChild)
.findViewById(R.id.btnDelete));
btnDelete.setText(R.string.btn_txt_deleted);
btnDelete.setEnabled(false);
}
datasource.close();
}
Here I am deleting record from my database and changing text of delete button from delete to deleted
adapter.notifyDataSetChanged();
You can call the above method to refresh list view any time. In your case call it after you delete a record from database.
I update listview by calling:
listview.invalidateViews();
notifyDataSetChanged();
is the only solution for me also now my deleteBookmark method looks like
void deleteBookmark(int wantedChild, String bookmarkItem) {
openDatabase();
int check = datasource.deleteBookmark(bookmarkItem);
if(check==1)
Toast.makeText(context, R.string.msg_bookmark_delete, ReaderConstants.TOAST_SHOWTIME).show();
else
Toast.makeText(context, R.string.msg_bookmark_delete_failed, ReaderConstants.TOAST_SHOWTIME).show();
notifyDataSetChanged();
closeDatabase();
}
inside adapter set the custome method for update dataset and notify changes
public void updateData(ArrayList<DataSet> d) {
this.data = d;
notifyDataSetChanged();
}
Then call method inside Activity,
(for my case ,I am calling inside onResume Method)
#Override
public void onResume(){
super.onResume();
adapter.updateData(dataList);
}