android gridview doesn't respond click event after back from other Activity - android

1.I set GridView OnItemClickListener at MyFragment
noScrollgridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
adapter = new GridAdapter(getActivity());
adapter.update();
noScrollgridview.setAdapter(adapter);
noScrollgridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (arg2 == Bimp.SelectBitmap.size()) {
ll_popup.startAnimation(AnimationUtils.loadAnimation(
getActivity(), R.anim.activity_translate_in));
pop.showAtLocation(mBaseView, Gravity.BOTTOM, 0, 0);
} else {
Intent intent = new Intent(getActivity(),
GalleryActivity.class);
intent.putExtra("position", "1");
intent.putExtra("ID", arg2);
startActivity(intent);
}
}
});
2.I click the gridview item and into GalleryActivity. At GalleryActivity, I clcik the "Cancel" Button and go back to the MyFragment.
3.When I back MyFragment interface, I can't click the grid item again.

noScrollgridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
adapter = new GridAdapter(getActivity());
adapter.update();
noScrollgridview.setAdapter(adapter);
noScrollgridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (arg2 == Bimp.SelectBitmap.size()) {
ll_popup.startAnimation(AnimationUtils.loadAnimation(
getActivity(), R.anim.activity_translate_in));
pop.showAtLocation(mBaseView, Gravity.BOTTOM, 0, 0);
} else {
Intent intent = new Intent(getActivity(),
GalleryActivity.class);
intent.putExtra("position", "1");
intent.putExtra("ID", String.Valueof(arg2));
startActivity(intent);
}
}
});

Related

How to pass item Click Position in Listview

This is My Main Listview `
lvComments.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), Subcategories.class);
intent.putExtra("position", i);
startActivity(intent);
}
});
This is code i am using for item position`This is my 2nd Listview its open when i am clicking on main ListView But This opens in all position and i want to open this 2nd listview on 0th position of main(1ST) Listview
lvComments.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), Subcategories.class);
if(i == 0){
intent.putExtra("position", i);
startActivity(intent);
}else {// do whatever your stuff
}
}
});
In Second Activity
int position = getIntent().getIntExtra("position", 0); // 0 will be default value if there is no value in bundle
If you want to open second listview only when you click on 0th position then use following code.
lvComments.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), Subcategories.class);
if(i == 0){
intent.putExtra("position", i);
startActivity(intent);
} else {
//do your stuff for other position.
}
}
});

onlistitemclick in AppCompatActivity

I followed steps mentioned in this answer to set a toolbar for a listview [listview image][1].
Now the list items are not clickable. When using ListActivity list items were clickable, when clicking on any item it will open another activity with item title and its content.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Note note = posts.get(position);
Intent intent = new Intent(this, EditNoteActivity.class);
intent.putExtra("noteId", note.getId());
intent.putExtra("noteTitle", note.getTitle());
intent.putExtra("noteContent", note.getContent());
startActivity(intent);
}
Do this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Note note = posts.get(position);
Intent intent = new Intent(this, EditNoteActivity.class);
intent.putExtra("noteId", note.getId());
intent.putExtra("noteTitle", note.getTitle());
intent.putExtra("noteContent", note.getContent());
startActivity(intent);
}
});

Start Activities from onItemClick of ListView in fragments

So I have two fragments linking to layout files which display ListViews. The ListViews are defined in the xml and have entries from a string array. I want to click on items in the ListView and open new activities. There are 8 items in one ListView and 9 in the other. In the onItemClick code, how do I create intents to start activities based on the item clicked? I will create 1 class per item as its own activity. How can I start the activities in the classes via intents inside the onItemClick methods of this code?
class CommunityFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.community_fragment, container, false);
ListView lv = (ListView) view.findViewById(R.id.communityListView);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
// TODO Auto-generated method stub
}
});
return view;
}
}
class ResourcesFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.resources_fragment, container, false);
ListView lv = (ListView) view.findViewById(R.id.resourcesListView);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
});
return view;
}
}
Implement your OnItemClickListener() like below
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
Intent intent = new Intent(getActivity(), nextactivity.class);
startActivity(intent);
}
});
Make switch statement for each items click and open activities accordingly as below:
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
int itm=arg0.getItemAtPosition(arg2);
switch (itm) {
case 0:
Toast.makeText(m_context, "Position Zero", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), FirstActivity.class);
startActivity(intent);
break;
case 1:
Intent intent1 = new Intent(getActivity(), SecondActivity.class);
startActivity(intent1);
break;
case 2:
//..............................
}
});
on Item Click you will get Position based on position you can start fragment
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
switch(position) {
case CONST_FRAGMENT_1 :
//Start fragment 1
...
...
}
}
});
one Generic solution can be ..
Create an array of items that hold class name of activity you want to open ..
like ..
Class[] activityArray = new Class[numberOfItemsInListView];
activityArray[0] = Activity1.class;
//add all activities like that..............
now on ListView onItemCLick :
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long arg3) {
Intent intent = new Intent(CommunityFragment.this.getActivity(), activityArray[postion]);
CommunityFragment.this.getActivity.startActivity(intent);
}
});
Use this to start next intent in your onItemClickListener:
Intent intent = new Intent(getActivity(), nextactivity.class);
startActivity(intent);
I think following code help you.
public class PdfListViewFragment extends Fragment {
ListView listView;
Activity rootView;
Activity context;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
//returning our layout file
//change R.layout.yourlayoutfilename for each of your fragments
View rootView = inflater.inflate(R.layout.pdf_list_view, container, false);
context = getActivity();
// Get ListView object from xml
listView = (ListView) rootView.findViewById(R.id.list);
// Defined Array values to show in ListView
String[] values = new String[]{"Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
// 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>(context,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
Intent myIntent = new Intent(view.getContext(), ListItemActivity1.class);
startActivityForResult(myIntent, 0);
}
if (position == 1) {
Intent myIntent = new Intent(view.getContext(), ListItemActivity2.class);
startActivityForResult(myIntent, 0);
}
if (position == 2) {
Intent myIntent = new Intent(view.getContext(), ListItemActivity1.class);
startActivityForResult(myIntent, 0);
}
if (position == 3) {
Intent myIntent = new Intent(view.getContext(), ListItemActivity2.class);
startActivityForResult(myIntent, 0);
}
if (position == 4) {
Intent myIntent = new Intent(view.getContext(), ListItemActivity1.class);
startActivityForResult(myIntent, 0);
}
if (position == 5) {
Intent myIntent = new Intent(view.getContext(), ListItemActivity2.class);
startActivityForResult(myIntent, 0);
}
if (position == 6) {
Intent myIntent = new Intent(view.getContext(), ListItemActivity1.class);
startActivityForResult(myIntent, 0);
}
if (position == 7) {
Intent myIntent = new Intent(view.getContext(), ListItemActivity2.class);
startActivityForResult(myIntent, 0);
}
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(context.getApplicationContext(), "Position :" + itemPosition + " ListItem : " + itemValue, Toast.LENGTH_LONG).show();
}
});
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("XYZ");
}
}
If you use an adapter to display the items in a list, it is important to differentiate the following:
The ID of the list AdapterView#getId() is different than the ID of the items in the list ArrayAdapter<String>#getId(), because the view of the list contains the views of the elements.
Suppose an example where you are going to launch activities with user roles. You will have to do the transformation for your particular case.
public class SignInFragment extends Fragment implements AdapterView.OnItemClickListener {
//TODO: Declare constants (GUEST, HOST, EMPLOYEE...)
private ArrayAdapter<String> userRolesAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_sign_in, container, false);
// ListView Instance
ListView userRolesList = root.findViewById(R.id.user_roles_list);
String[] userRoles = {
GUEST,
HOST,
EMPLOYEE
};
// Initialize the adapter
userRolesAdapter = new ArrayAdapter<>(
getActivity(),
android.R.layout.simple_list_item_1,
userRoles
);
// Link to the list with the adapter. This reference starts the process of filling the list.
userRolesList.setAdapter(userRolesAdapter);
// Events
userRolesList.setOnItemClickListener(this);
return root;
}
/**
* #param adapterView: View using the adapter from the list
* #param view: View of the item that has been pressed
* #param i: Refers to the position of the item that the adapter handles
*/
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (adapterView.getId() == R.id.user_roles_list) {
// Obtain the item pressed on the adapter with the entry position
String currentUserRol = userRolesAdapter.getItem(i);
assert currentUserRol != null;
switch (currentUserRol) {
case GUEST:
startActivity(Host.class);
break;
case HOST:
startActivity(Host.class);
break;
case EMPLOYEE:
startActivity(Employee.class);
break;
default:
Log.d("Error", "The activity passed as an argument to startActivity() does not exist");
break;
}
}
}
/**
* PRECONDITION: The class given as an argument exists.
*/
public void startActivity(Class<?> cls) {
Intent intent = new Intent(getActivity(), cls);
startActivity(intent);
}
}
Best :)

How to get the selected item from this ListView?

I have a custom List view and I been trying to get to next screen if user clicks an item. It doesn't seem to work. I tried if else, switch. It works if I don't use either but that way, every item will go to the same screen. Any help would be appericiated.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.options_main);
OptionMainHeader option_list[] = new OptionMainHeader[]
{
new OptionMainHeader(R.drawable.clock_icon_48, "Helo"),
new OptionMainHeader(R.drawable.weather_cloudy, "Apple"),
new OptionMainHeader(R.drawable.weather_cloudy, "Mango"),
new OptionMainHeader(R.drawable.weather_cloudy, "Banana"),
};
OptionMainHeader1 adapter = new OptionMainHeader1(this, R.layout.option_main_header1, option_list);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.option_main_header, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
int position = listView1.getSelectedItemPosition();
String selectedFromList =(String) (listView1.getItemAtPosition(position));
if (selectedFromList == "Helo"){
Intent CurrentWaitTime = new Intent(getApplicationContext(), go.class);
startActivity(CurrentWaitTime);
} else if (selectedFromList == "Apple"){
Intent Appointments = new Intent(getApplicationContext(), no.class);
startActivity(Appointments);
} else if (selectedFromList == "Mango"){
Intent Logout = new Intent(getApplicationContext(), go.class);
startActivity(Logout);
} else if (selectedFromList == "Banana"){
Intent Exit = new Intent(getApplicationContext(), low.class);
startActivity(Exit);
}
}
});
}
Try this
ListView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
TextView textView = (TextView) arg1;
String strText = textView.getText().toString();
if ("Helo".equals(strText)){
Intent CurrentWaitTime = new Intent(getApplicationContext(), go.class);
startActivity(CurrentWaitTime);
} else if ("Apple".equals(strText){
Intent Appointments = new Intent(getApplicationContext(), no.class);
startActivity(Appointments);
} etc, etc
You can't compare Strings with "==". What you need to do is use the method String.equals(String). Something like:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedFromList = option_list[position].getTHATSTRING();
if (selectedFromList.equals("Helo")){
Intent CurrentWaitTime = new Intent(getApplicationContext(), go.class);
startActivity(CurrentWaitTime);
} else if (selectedFromLis.equals("Apple")){
Intent Appointments = new Intent(getApplicationContext(), no.class);
startActivity(Appointments);
} else if (selectedFromList.equals("Mango")){
Intent Logout = new Intent(getApplicationContext(), go.class);
startActivity(Logout);
} else if (selectedFromList.equals("Banana")){
Intent Exit = new Intent(getApplicationContext(), low.class);
startActivity(Exit);
}
}
Try this, it will help you.
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
//global variable
idItem = (int) id;
switch (idItem) {
case 0:
// This is your first Item
// Add your Intent and startActivity
break;
case 1:
// Add your Intent and startActivity
break;
case 2:
// Add your Intent and startActivity
break;
case 3:
// Add your Intent and startActivity
break;
}
}
});
You get over id the item you clicked. Then you can go to the right activity you want for this item.
Hope it helps.

Listview , open new activity onClick

Hey everyone I've looked for hours trying to find a solution to this, my goal is to have a Listview when it opens well open another activity. Well actually I got it to be able to open another activity when its click but how do I get it so that each list item will open its own activity? I am terribly sorry if this question is already answered but the links I found doesn't really describe what the code is doing [Yes i am a newbie :)]
this is the code im using
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] countries = getResources().getStringArray(R.array.countries_array);
setListAdapter(new ArrayAdapter<String>(this, R.layout.newfile, countries));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Intent myIntent = new Intent(view.getContext(), Html_file.class);
startActivityForResult(myIntent, 0);
}
});
}
}
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
if(position == 1) {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), Html_file1.class);
startActivityForResult(myIntent, 0);
}
if(position == 2) {
//code specific to 2nd list item
Intent myIntent = new Intent(view.getContext(), Html_file2.class);
startActivityForResult(myIntent, 0);
}
}
});
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position ) {
case 0: Intent newActivity = new Intent(this, i1.class);
startActivity(newActivity);
break;
case 1: Intent newActivity = new Intent(this, i2.class);
startActivity(newActivity);
break;
case 2: Intent newActivity = new Intent(this, i3.class);
startActivity(newActivity);
break;
case 3: Intent newActivity = new Intent(this, i4.class);
startActivity(newActivity);
break;
case 4: Intent newActivity = new Intent(this, i5.class);
startActivity(newActivity);
break;
}
}
If you have some limited number of list you can use switch case here on position
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Intent myIntent = new Intent(view.getContext(), Html_file.class);
startActivityForResult(myIntent, 0);
}
});
If you know which activity is to be opened when different list items are clicked, then just assign id or tag to the list items.
In the callback of onItemClick, you have a parameter View,
use it to get id or tag to differentiate them and call respective Activity.
// Add ArrayList and ArrayAdapter:
final ArrayList<String> listItems = new ArrayList<String>();
listItems.add("image_one");
listItems.add("image_two");
listItems.add("image_three");
ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, listItems);
myListView.setAdapter(myArrayAdapter);
// Add ArrayList of Classes:
final ArrayList<Class> intents = new ArrayList<Class>();
intents.add(image_one.class);
intents.add(image_two.class);
intents.add(image_three.class);
// Click on list item to open Class from ArrayList of Classes:
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
Intent listIntent = new Intent(getApplicationContext(),
intents.get(position));
startActivity(listIntent);
}
});
SEE IMAGE OF CLASS NAMES HERE

Categories

Resources