When the fragment is opened i.e the onCreateView method is called the spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener()) is called even though no item has been selected yet.
Why is this and how would it be possible to change the code to make it so that the method is only called when an item is selected?
here is the code:
package com.petroc.nationaldiploma;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.actionbarsherlock.app.SherlockFragment;
public class AddGradesFragment extends SherlockFragment {
private GradesDataSource datasource;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Spinner spinner;
View view = inflater.inflate(R.layout.fragment_add_grades, container,
false);
spinner = (Spinner) view.findViewById(R.id.spinner);
List<String> list = new ArrayList<String>();
list.add("Pass");
list.add("Merit");
list.add("Distinction");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_spinner_item, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner = (Spinner) view.findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
// EditText editText = (EditText)
// view.findViewById(R.id.editModuleText1);
return view;
}
}
Any help is appreciated thanks
when you set data into the spinner using list object and adapter it was select the default value as 0 on setting time so that's why you getting selected
Related
I have a Fragment activity that I am using to make a tab bar. Inside this activity I am planning to put a table or ListViews with some random values.
Here's my code:
import com.example.mvaguimaraes.beetrackprov.R;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
public class Orders extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.orders, container, false);
ListView listView1 = (ListView) findViewById(R.id.listView1);
Order[] items = {
new Order(1, "Milk", 21.50),
new Order(2, "Butter", 15.99),
new Order(3, "Yogurt", 14.90),
new Order(4, "Toothpaste", 7.99),
new Order(5, "Ice Cream", 10.00),
};
ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(this,
android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView) view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
}
The problem is that I cannot extend my Orders class to Fragment and Activity so I'm getting this errors in my code:
Cannot resolve method findViewByID(int)
ListView listView1 = (ListView) findViewById(R.id.listView1);
Cannot resolve constructor
ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(this,
android.R.layout.simple_list_item_1, items);
Cannot resolve method getBaseContext()
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
Probably because they inherit something from Activity. There's any way to make them work inside the Fragment? Sorry for my noob-ness I am new to Android Development.
To open another screen after clicking on one of the items of the listview I did this right after the onItemClick public void method:
String item = ((TextView) view).getText().toString();
Intent i = new Intent(getApplicationContext(), OrderDetails.class);
i.putExtra("new_variable_name",item);
Intent browserIntent =
new Intent(Orders.this, OrderDetails.class);
startActivity(browserIntent);
startActivity(i);
I tried to replace both "getApplicationContext()" and "Orders.this" with "getActivity()" but it didn't work.
Fragments do not have a findViewById() method like Activities. Instead you call findViewById() to lookup views on the View returned from inflating the layout in onCreateView(). Also, where you need a Context you typically use the Activity the fragment is attached to by calling getActivity().
public class Orders extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.orders, container, false);
ListView listView1 = (ListView) v.findViewById(R.id.listView1);
Order[] items = {
new Order(1, "Milk", 21.50),
new Order(2, "Butter", 15.99),
new Order(3, "Yogurt", 14.90),
new Order(4, "Toothpaste", 7.99),
new Order(5, "Ice Cream", 10.00),
};
ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(getActivity(),
android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView) view).getText().toString();
Toast.makeText(getActivity(), item, Toast.LENGTH_LONG).show();
}
});
return v;
}
}
New android programmer is here.
First : I dont know many things about list view and as I found out , its complicated to work with it.
So , I want to put my database data (Contains Id , Name) to a listview and get the Id of the item is clicked.
I have searched many but i just found this :
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class test extends Activity {
String[] wordlist = new String[] { "a", "b", "c" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wordlist[2] = "abds";
ListView list = new ListView(this);
list.setAdapter((ListAdapter) new MyAdapter(test.this, wordlist));
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Object entry= parent.getItemAtPosition(position);
Toast.makeText(test.this, entry.toString(), Toast.LENGTH_SHORT).show();
}
});
setContentView(list);
}
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, String[] strings) {
super(context, -1, -1, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout listLayout = new LinearLayout(test.this);
listLayout.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT,
AbsListView.LayoutParams.WRAP_CONTENT));
listLayout.setId(5000);
TextView listText = new TextView(test.this);
listText.setId(5001);
listLayout.addView(listText);
listText.setText(super.getItem(position));
return listLayout;
}
}
}
Just i can show strings , not Id.
Use SimpleCursorAdapter to populate ListView with data from database, for example - https://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/ (first link from google).
How to get the item id in an onItemClick handler
Read more about ListView and Adapters, your code is awful )
The following code gives me this error: The constructor ArrayAdapter(TrainingDetails, int, String[]) is undefined. How to rectify it? Please help. Here I am trying to retrieve the value of the selected spinner and store it in another string.
package com.example.fragments;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import com.example.R;
public class TrainingDetails extends BaseFragment
{
View view;
private EditText nameEdt;
private Spinner trainingTypes;
private Spinner trainerProfile;
String data1[] = {"1", "2", "3"};
String data2[] = {"a", "b", "c"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view=inflater.inflate(R.layout.training_details, container,false);
nameEdt = (EditText) view.findViewById(R.id.name_edt);
trainingTypes = (Spinner) view.findViewById(R.id.trainingtypes_spn);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data1);
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
trainingTypes.setAdapter(adapter_state);
trainingTypes.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
int index = arg0.getSelectedItemPosition();
String selectedItem = data1[index];
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
trainerProfile = (Spinner) view.findViewById(R.id.trainerprofile_spn);
return view;
}
}
change
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data1);
to
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, data1);
in Fragment you'll get Context by using getActivity()
For ArrayAdapter you need to pass Context
change this
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data1);
into
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, data1);
The first parameter of ArrayAdapter is a Context object not an instance of a Fragment. Change
new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data1);
with
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, data1);
I learned that in order to modify row in a listView I need to gain access eg via adapter.getItem(position) but I have no idea how to work this around. Should I post any code please let me know.
Here is my EditListItemDialog file:
package com.example.classorganizer;
import java.util.ArrayList;
import java.util.List;
import com.example.classorganizer.Monday.DiaryAdapter;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
class EditListItemDialog extends Dialog implements View.OnClickListener {
private View editText;
private DiaryAdapter adapter;
// public EditListItemDialog(Context context, List<String> fragment_monday) { //first constructor
// super(context);
// this.fragment_monday = fragment_monday;
// }
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_text_dialog);//here is your xml with EditText and 'Ok' and 'Cancel' buttons
View btnOk = findViewById(R.id.button_ok);
editText = findViewById(R.id.edit_text);
btnOk.setOnClickListener(this);
}
private List<String> fragment_monday;
public EditListItemDialog(Context context, DiaryAdapter adapter) {
super(context);
this.fragment_monday = new ArrayList<String>();
this.adapter = adapter;
}
#Override
public void onClick(View v) {
fragment_monday.add(((TextView) v).getText().toString());//here is your updated(or not updated) text
adapter.notifyDataSetChanged();
dismiss();
}
}
You will have to implement an OnItemClickListener which will be able to tell you what item has been clicked. For example:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Position is the number of the item clicked
//You can use your adapter to modify the item
adapter.getItem(position); //Will return the clicked item
}
See the official Android documentation for more information.
package com.example.classorganizer;
import java.util.ArrayList;
import java.util.List;
import com.example.classorganizer.Monday.DiaryAdapter;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class EditListItemDialog extends Activity {
ListView listView ;
private View editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_text_dialog);
View btnOk = findViewById(R.id.button_ok);
editText = findViewById(R.id.edit_text);
btnOk.setOnClickListener(this);
listView = (ListView) findViewById(R.id.list)
String[] values = new String[] { "Automotive", "Banking", "Consumer Electronics",
"Education", "HealthCare and Life Sciences","Industrial Automation","Printing and
Imaging","Manufacturing","Media and
Entertainment","Networking","Retail","Telecom"
};
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
}
}
Use This code for simple Listview..
I am making an application in which I have to change the position of the selected item in list view to first position and set other items one position less than current How can i do this.
Can any body suggest me some tutorial or any suggestion.
Use Adapter, and arraylist on item click remove the item from that position. out it at the 0th position and use notifyDataSetChanged to reaarange the listview
Example
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Main_Screen extends ListActivity implements OnItemClickListener{
ArrayAdapter arrayAdapter = null;
/** Called when the activity is first created. */
Context context = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
penList.add("MONT Blanc");
penList.add("Gucci");
System.out.println("...1...");
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, penList);
arrayAdapter.add("last by adapter");
setListAdapter(arrayAdapter);
penList.add("Last By list");
arrayAdapter.add("last by adapter2");
getListView().setTextFilterEnabled(true);
ListView lv = getListView();
this.registerForContextMenu(lv);
lv.setOnItemClickListener(this);
}
static ArrayList<String> penList = new ArrayList<String>();
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
String str = penList.get(arg2);
penList.remove(arg2);
penList.add(0, str);
arrayAdapter.notifyDataSetChanged();
}
}