So, this might be a simple question, or I may be doing things totally off here, but here's what I have:
public class SetPrefsActivity extends ListActivity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.radiolist);
ArrayList<Hall> listItems = new ArrayList<Hall>();
ArrayAdapter<Hall> ar = new ArrayAdapter<Hall>(this, android.R.layout.simple_list_item_single_choice, listItems);
setListAdapter(ar);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
boolean somethingChecked = false;
int lastChecked;
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
if(somethingChecked){
ListView lv = (ListView) arg0;
TextView tv = (TextView) lv.getChildAt(lastChecked);
CheckedTextView cv = (CheckedTextView) tv;
cv.setChecked(false);
}
ListView lv = (ListView) arg0;
TextView tv = (TextView) lv.getChildAt(arg2);
CheckedTextView cv = (CheckedTextView) tv;
if(!cv.isChecked())
cv.setChecked(true);
lastChecked = arg2;
somethingChecked=true;
}
});
new LoadListTask().execute(); //This loads Items into the List
Button b = (Button) findViewById(R.id.savePrefBtn);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//IF SOMETHING IS SELECTED,
//THEN UPDATE SHARED PREFERENCES
Intent i = new Intent(SetPrefsActivity.this, SomeOtherActivity.class);
startActivity(i);
}
}
});
}
//other stuff to fill the arrayAdapter
}
What I want to do, is:
When someone clicks the button, it gets information from the listview and updates a shared preference according to the radio option that's selected.
What I'm having trouble with is getting the index of the currently selected item. What is the best way to retrieve that information? Am I implementing the single choice list completely wrong?
Thank You!
arg2 is the position in your list.
It looks like you are doing some extra (unnecessary) processing. arg1 is your row view, and since the android.R.layout.simple_list_item_single_choice layout contains only a CheckedTextView, you can use that directly without having to look for it.
CheckedTextView cv = (CheckedTextView) arg1;
Related
What is the syntax to get a specific togglebutton from a listview containing the said togglebuttons within a listview_row layout?
I would like to initiate the state of each togglebutton (based on some values originating from a Database), within the onCreate method. I have the following code within a loop, but I am not sure how to change it to reference a specific togglebutton from the listview.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb = new DBHelper(this);
ArrayList array_list = mydb.getAllAlarms();
for(int i = 0; i < array_list.size(); i++) {
arrayListItem = array_list.get(i).toString();
activationInt = Integer.parseInt(arrayListItem);
LayoutInflater vi = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.listview_row, null);
alarm_activated = (ToggleButton) view.findViewById(R.id.alarm_activated);
if (activationInt == 1) {
alarm_activated.setChecked(true);
alarm_activated.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
} else {
alarm_activated.setChecked(false);
}
}
ArrayAdapter arrayAdapter =
new ArrayAdapter(this, listview_row,R.id.alarm_name,array_list);
obj = (ListView)findViewById(R.id.listViewAlarms);
obj.setAdapter(arrayAdapter);
obj.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String itemVal =(String) arg0.getItemAtPosition(arg2);
Bundle dataBundle = new Bundle();
dataBundle.putString("name", itemVal);
Intent intent = new
Intent(getApplicationContext(),DisplayAlarm.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
Updating views of a ListView/RecyclerView should not really be done in this way.
First because child views referenced in a ListView do not represent the totality of your rows, but only the visible rows at one moment.
ListView binds their item views on DataSet values, so you'd better use data objects that contains your "checked" boolean status, then when you need to sync, update your DataSet and notifyDataSetChange your adapter.
So you'll have to create your custom adapter by following this kind of example : Custom Adapter for List View
I have a ListView which is populated in my MainActivity, but when i want to find the selected item, all of the items appear to have the same position.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_teams);
ListView mainListView;
// Find the ListView in the UI.
mainListView = (ListView) findViewById( R.id.listView );
String values = "Team A vs Team B=Team C vs Team D";
//Matches are send in one long string, and are separated by the = sign.
//This splits the string up and puts it into an array.
String[] array = values.split("=", -1);
ArrayList<String> arraylist = new ArrayList<String>();
arraylist.addAll( Arrays.asList(array) );
ArrayAdapter listAdapter;
// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(this, R.layout.list_view_style, arraylist);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter(listAdapter);
}
This makes a list like this:
Team A vs Team B [checkbox]
Team C vs Team D [checkbox]
I have a button and when it is clicked it runs this method:
public void matchStart(View view){
String selectedMatch = String.valueOf(((ListView) findViewById( R.id.listView )).getSelectedItemPosition());
Toast.makeText(SelectTeams.this, selectedMatch, Toast.LENGTH_SHORT).show();
}
However whenever i click the button, the toast displays the same value no matter which item in the listview is selected. Why is this?
You should use the adapters getView method, something like this:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.some_layout, parent,
false);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//here get what you need by the position
}
});
}
}
I am trying to make app with one text view, one button and one spinner. I make button work showing random values from string array, but i have a lot different string arrays(list1, list2 etc). Now my question is when change spinner position, how to get button get another string array(from spinner) and show it to textview.
How to pass value from spinner when something is selected in spinner to button.
Any help is appreciated.
Here is my code:
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
Button btn;
public String[] myString,myString1;
public static final Random rgenerator = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//textview
final TextView tv = (TextView) findViewById(R.id.textureView);
final Resources res = getResources();
//string
myString = res.getStringArray(R.array.World_War_I);
myString1 = res.getStringArray(R.array.World_War_II);
//button
btn = (Button) findViewById(R.id.buttonxx);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
//i am missing code here, spinner position, and pass spinner position to if statement.
if (==0){
myString = res.getStringArray(R.array.list1);
String q = myString[rgenerator.nextInt(myString.length)];
tv.setText(q);
}
if (==1){
myString1 = res.getStringArray(R.array.list2);
String q1 = myString1[rgenerator.nextInt(myString.length)];
tv.setText(q1);
}
}
});
//drop list
Spinner spinner = (Spinner) findViewById(R.id.spinnerrrr);
spinner.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.kategorije, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
// when some cat selected
public void onItemSelected(AdapterView<?> parent, View view,
final int pos, long id) {
// An item was selected. You can retrieve the selected item using
parent.getItemAtPosition(pos);
parent.setSelection(0);
parent.getSelectedItemPosition();
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
Use this
// when some cat selected
public void onItemSelected(AdapterView<?> parent, View view,
final int pos, long id) {
// An item was selected. You can retrieve the selected item using
btn.setTag(pos+""); // Passing as string
parent.getItemAtPosition(pos);
parent.setSelection(0);
parent.getSelectedItemPosition();
}
and then
btn.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
//i am missing code here, spinner position, and pass spinner position to if statement.
int index = Integer.parseInt(btn.getTag().toString());
if (index==0){
myString = res.getStringArray(R.array.list1);
String q = myString[rgenerator.nextInt(myString.length)];
tv.setText(q);
}
if (index==1){
myString1 = res.getStringArray(R.array.list2);
String q1 = myString1[rgenerator.nextInt(myString.length)];
tv.setText(q1);
}
}
});
I have this code i am used to pass an array list to another page and show it as a listview. When the list shows up, i want to be able to check an item and remove it at "button click" which will modify the array.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oppout);
final ListView lv2 = (ListView) findViewById (R.id.custom_list_view);
lv2.setClickable(true);
lv2.setAdapter(new ArrayAdapter<String>(Oppout.this,
android.R.layout.simple_list_item_checked,
Entername.playerList));
lv2.setOnItemClickListener (
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView adapterView,
View view,int arg2, long arg3) {
int selectedPosition = adapterView.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "mu"+ selectedPosition,
Toast.LENGTH_SHORT).show();
}
});
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// on click call the adapterview and delete string at selected position.
// This is my problem, am not getting how to call the adapter and deleted
// the selected item/position
int selectedPosition = adapterView.getSelectedItemPosition();
adapterView.remove(player.SelectedPosition);
Intent myIntent = new Intent (view.getContext(), Callacab.class);
startActivityForResult(myIntent, 0);
}
});
}}
contacts.remove(index); //the arraylist you gave it to your adapter
arrayadapter.remove(index); // this is your adapter that you give it to the listview
arrayadapter.notifyDataSetChanged();
//you can delete from your arraylist or your adapter and then notifyDataSetChanged(); to make the effect happen in your listview....it better to delete from arraylist and its indexs will be the arg2 in your listview item listener
I have created a list. And I need to get the text on the list item, when it is clicked. Then that text need to be set in a TextView. Following is my code and i get a force stop when I run it. Please give some ideas.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtTask = (EditText)findViewById(R.id.txtTask);
btnAdd = (Button)findViewById(R.id.btnAddTask);
selectedTask = (TextView)findViewById(R.id.textViewTask);
list = getListView();
list.setTextFilterEnabled(true);
btnAdd.setOnClickListener(this);
list.setOnKeyListener(this);
toDoItems = new ArrayList<String>();
oo = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, toDoItems);
list.setAdapter(oo);
list.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id3) {
int tmp = list.getSelectedItemPosition();
String v= toDoItems.get(tmp).toString();
selectedTask.setTag(v);
flippy.showNext();
}
});
}
Replace below 3 lines of your code in onItemClick method with my suggested code.
int tmp = list.getSelectedItemPosition();
String v= toDoItems.get(tmp).toString();
selectedTask.setTag(v);
Suggested Code
String v= toDoItems[position]; // or
String v = list.getItemAtPosition(position).toString();
selectedTask.setText(v);
After you have got the string v, you need to put the following line :
selectedTask.setText(v);
Also there is no need to put list.setOnKeyListener(this); since you need to listen for the item being clicked.