Permanently adding a cell to a list - android

Here is my current code:
gameListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == plusPosition) //when plusSign is clicked
{
someGame = new Game(); //instance of game class
someGame.gameName = indicatedGameName;
arrayOfGames.add(0, someGame);
gameRowItem newGame = new gameRowItem(someGame.gameName);
GameRowItem.add(0, newGame);
adapter.notifyDataSetChanged();
plusPosition++;
}else{
Intent toGamePlay = new Intent(ListOfGames.this, GamePlay.class);
startActivity(toGamePlay);
}
}
});
Whenever I leave this activity and then go back to this activity, the cells that were added disappeared. How would I make the cells stay saved even if you leave the activity? Thanks in advance for any help given.

Related

How to read what is actually inside a ListView and not the position it is in on android app?

I have an ListView which I have populate with a dynamic ArrayList and I used an OnItemClickListener to make it does something when i click on them (Code bellow).
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,txtOnlyList);
returnSaveNotesList.setAdapter(arrayAdapter);
AdapterView.OnItemClickListener noteClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> txtOnlyList, View view, int position, long id) {
if(position == 0 ) {
Intent intent = new Intent(ReadFromFile1.this, SaveAndRstoreNote.class);
intent.putExtra("firstNote","1 .txt");
startActivity(intent);
}
}
};
returnSaveNotesList.setOnItemClickListener(noteClickListener);
Now comes the tricky part, I want, somehow to be able to read what is actually written lets say on the position [0], the actual String. Is there any way to achieve that?
public void onItemClick(AdapterView<?> txtOnlyList, View view, int position, long id) {
if (position == 0) {
String yourString = txtOnlyList.get(position);// use this
Intent intent = new Intent(ReadFromFile1.this, SaveAndRstoreNote.class);
intent.putExtra("firstNote", "1 .txt");
startActivity(intent);
}
}
Your ArrayAdapter<String> has a getItem() method that returns the String for a given position.
I have used the getItemAtPosition(position).
Here is the entire code:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,txtOnlyList);
returnSaveNotesList.setAdapter(arrayAdapter);
AdapterView.OnItemClickListener noteClickListener = new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> txtOnlyList, View view, int position, long id) {
if(position == 0 ){
Intent intent = new Intent(ReadFromFile1.this, SaveAndRstoreNote.class);
String stringFromArray=txtOnlyList.getItemAtPosition(position).toString(); // I added this
intent.putExtra("firstNote","1 .txt");
startActivity(intent);
}
}
};
returnSaveNotesList.setOnItemClickListener(noteClickListener);
The View type parameter being passed in your OnItemClick function is the list item's view at that position.
AdapterView.OnItemClickListener noteClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> txtOnlyList, View view, int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.tv);
String text = tv.getText().toString(); //The required text is available in this variable
}
}

Disable GridView items on click

I have a GridView that is generated dynamically from SQLite database entries. An activity is started when a user taps on one of the GridView items. During the time from the tap and the activity launch, there is a split second where the user can rapidly tap other Gridview items which initiates their onItemClickmethod. Is there a way so all the GridView items onItemClick methods are disabled after selecting one?
Here is the code:
gridView = (GridView) findViewById(R.id.gridView);
nautsList = db.getAllNauts();
nautsGridAdapter = new AwesomenautGridViewAdapter(this, R.layout.gridview_item, nautsList);
gridView.setAdapter(nautsGridAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
//do stuff
Intent intent = new Intent(AwesomenautSelectionActivity.this, AwesomenautSoundActivity.class);
startActivity(intent);
}
});
I would use this simple solution. It is successfully used in our apps.
Make a global boolean itemsLocked and set it to true as soon as you click. Set it back to false just before startActivity. Finally, in method onItemClick add
if (itemsLocked) return;
EDIT: method onItemClick
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if (itemsLocked) return;
itemsLocked = true;
//do stuff
Intent intent = new Intent(...);
itemsLocked = false;
startActivity(intent);
}

problems opening listView items with intents in android studio

I have a listView with 2 items in the list, the first item in position 0 responds to clicks but the other does not! i have tried to copy the same onclick method and changed the if statement to the list item in position 1 but it is not working out for me so i am looking how to get this working if someone could help me? i have included the only code i think is needed to resolve this,
public class TopLevelActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top_level);
//Create an OnItemClickListener
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView,
View v,
int position,
long id) {
if (position == 0) {
Intent intent = new Intent(TopLevelActivity.this,
DrinkCategoryActivity.class);
startActivity(intent);
}
}
};
//Add the listener to the list view
ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnItemClickListener(itemClickListener);
//what ive tried to open the foodCategoryActivity list item
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView,
View v,
int position,
long id) {
if (position == 1) {
Intent intent = new Intent(TopLevelActivity.this,
FoodCategoryActivity.class);
startActivity(intent);
}
}
};
//Add the listener to the list view
ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnItemClickListener(itemClickListener);
}
}
thank you.
Currently using two different click listeners for same ListView to do different on different items click in ListView with is not valid way to do task according to click position in ListView.
Use single click listener and inside onItemClick method use switch-case or if-else ladder for doing task like:
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView,
View v,
int position,
long id) {
if (position == 0) {
}
else if (position == 1) {{
}
}
};
and also remove following line which you are using two times:
ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnItemClickListener(itemClickListener);
because no need to create listView and call setOnItemClickListener multiple times just do it once.
You can only set one OnItemClickListener
you need to differentiate in the implemented method via either the position or the View object itself:
AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView,
View v,
int position,
long id) {
if (position == 0) {
//code for drink category
}
else {
//code for food category
}
}
};
//Add the listener to the list view
ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnItemClickListener(itemClickListener);

How to perform intent function in selected row of Listview

I have created one listview of some names,what i need is when i will click selected row it will go to that page only,on click on different row it will move to the same class but different content.I think it will move by question id.could anybody help me how to pass the question id Or any other method to do this..
here is my code..
private OnItemClickListener mlist = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
}
};
You can try something like this -
private OnItemClickListener mlist = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
if(Some condition)
{
Intent i= new Intent(YourActivity.this,ActivityOne.class);
// To pass data
i.putExtra("SomeId", someValue);
startActivity(i);
}
else if(Some other condition)
{
Intent i= new Intent(YourActivity.this,SecondActivityTwo.class);
startActivity(i);
}
else
{
// Do something else--
}
}
};
And in the other activity -
String identifier = getIntent().getExtras().getString("SomeId");
Here I've given an example assuming you have user list and clicking on item you want to show user profile...
In List_Act activity...
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = mInflater.inflate(R.layout.rowitem,parent,false);
convertView.setTag(UserId);
}
private OnItemClickListener mlist = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent i=new Intent(List_Act.this, Profile_Act.class);
int UserId = ((View)v.getParent()).getTag();
i.putExtra("UserId", UserId); //Setting variable you want to pass to another activity
startActivity(i);
}
};
in Profile_Act activity in onCreate()
String UserId = getIntent().getExtras().getString("UserId"); //retrieving value in another activity
now you'll have UserId variable set and you can use it...

Odd Android Spinner behavior

This is the code to populate my spinner.
The code:
String[] rcs = new String[review_cycles.length];
for (int i = 0; i < review_cycles.length; i++)
rcs[i] = review_cycles[i].ReviewCycleName;
ArrayAdapter<String> rc_spinnerAdapter = new ArrayAdapter<String>(
ActivityManagementReview.this,
R.layout.simple_spinner_item,
rcs);
sp_review_cycle.setAdapter(rc_spinnerAdapter);
sp_review_cycle.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
DtoMrReviewCycleVersion selected_review_cycle_version = review_cycles[position];
if (selected_review_cycle_version != latest_review_cycle_version) {
latest_review_cycle_version = selected_review_cycle_version;
int mr_version_id = latest_review_cycle_version.MrdVersionId;
_URL_version = _url_base + "MrVersion/" + Integer.toString(mr_version_id);
new GetMrVersionInfoAsyncTask().execute();
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
return;
}
});
When I select a value from the spinner, the code is executed twice. Once with the selection I made and then right afterward, the first item in the list is executed again bringing me back to where I started.
How can I prevent this from happening?
Thanks.
When I select a value from the spinner, the code is executed twice.
As I reminded you in the comments, Spinners call onItemSelected() when they load the default value. This feature is useful when you know about it, but since it is not what the average developer expects it is problematic as well.
The Spinner will also call onItemSelected() when the user re-selects the current value... When I want to avoid both of these false alarms I use something like this:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
int previous = -1;
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(previous != position && previous < -1) {
Log.v("Example", "Selected: " + position);
// Do something
}
previous = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
});

Categories

Resources