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);
}
Related
How Can I Load the Questions for different subject in listview item's click. For e.g; if Android is clicked it should load Android MCQ in another Activity
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
T item = parent.getAdapter().getItem(position);
switch (item) { //or if, else
// logic here
}
}
});
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);
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.
Hi i have a problem i would like on my android app that uses a list once an item is clicked on the list the id is captured and a query run the data received is then placed in an intent and a new activity is started and the data placed in a another list any help how this can be achieved with an example if possible
yourlist.OnItemClickListener(listenerOflistView);
private OnItemClickListener listenerOflistView = new OnItemClickListener() {
private String getSelectedItemOfList;
public void onItemClick(AdapterView<?> view, View view1, int pos, long arg3) {
Intent in = new Intent(Context, nextActivity.class);
String getSelectedItemOfList = mylist.get(pos).toString();
in.putExtras("AnythingName", getSelectedItemOfList);
startActivity(in);
}
}
In another activity:
Intent in = getIntent();
in.getStringextras("AnythingName");
then, you get the value of another activity.
I think you can achieve by using OnItemClickListener() for ListView. So that when you click list item you will get the content of Listview.
yourlist.OnItemClickListener(listenerOflistView);
private OnItemClickListener listenerOflistView = new OnItemClickListener() {
private String getSelectedItemOfList;
public void onItemClick(AdapterView<?> view, View view1, int pos, long arg3) {
getSelectedItemOfList = mylist.get(pos).toString();
// here you will get selected item. And you can perform rest of your task here.
}
}
I have a list view with 15 items. When I click on any item I want to change the screen(Intent). how can I change the activity on item selected in android?
any tutorial or source code?
You can use ListView's setOnItemClickListener, and start an new Activity in your implementation of this method. Following is sample code:
myListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id){
// Start your Activity according to the item just clicked.
}
});
final ListView list = (ListView) findViewById(R.id.SCHEDULE);
protected void onCreate(Bundle savedInstanceState) {
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
Toast.makeText(getApplicationContext(),"hiihih",Toast.LENGTH_SHORT).show();
}
});
}
Check the selected answer in ListView OnItemClickListener Not Responding?
If you also need code examples to change activity, head to https://developer.android.com/guide/index.html and start reading.
// Prepare intent
Intent newActivity = new Intent(this, NewActivity.class);
// start activity
startActivity(newActivity);