How to implement an ExpandableList in a ViewPager in Android?
My question is the exact opposite of the above question, I want to display a list view (preferably an expandable one) and when an item is clicked I want display a bunch of images (different ones for each item of course).
P.S: Please dont tell me to launch a different activity for each list item.
This is some possible guidance not solution. (I am not exactly clear on what the problem is)
I would probably just create one activity. Each time you get a click on the listview it creates a intent with some stored flags. Then you start the activity with that intent. Unpackage the intent to find out what you want you activity to do.
private OnItemClickListener itemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(this, myclass.class);
intent.putExtra(KEY, v.getId())
intent.putExtra(KEY2, position)
intent.putExtra(KEY3, id)
startActivity(intent);
}
};
And then get the intents flags in the new activity onCreate()
Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent= getIntent();
resId = intent.getIntExtra(KEY, 0);
...
super.setTitle(title);
Not sure if that what you were looking for hope it helps
Related
I've implemented a clickable List View using an ArrayAdaptor:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_tasks);
myTasksListView = (ListView) findViewById(R.id.mytasks_listView);
myTasksListView.setOnItemClickListener(this);
tasks = getTasks(); //of type Task[]
ArrayAdapter<Task> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, tasks);
myTasksListView.setAdapter(adapter);
}
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Intent intent = new Intent(this, TaskDetail.class);
intent.putExtra("position", position);
intent.putExtra("id", id);
startActivity(intent);
}
Now, in the new activity I'm starting on item click, I can get the position of the item that was clicked; however, I'm not sure what would be the best method of getting the actual array. Can I
Find the Activity that launched the new activity and then access the ArrayAdaptor from there?
make my class Task parcelable and then just call intent.putExtra() on my object itself?
or is this another approach that's considered "best practice"? I was trying to follow along this helpful comment but it looks like the array in question was hard coded?
The best approach would be to make the Task class implement the Parcelable interface. If you did so, you'd be able to use Intent.putExtra(String, Parcelable[]) and simply call
intent.putExtra("tasks", tasks);
I want to go from one fragment to another fragment via click on a widgets.
I used this code in the main activity and used onclick in wedget property:
public void goOther(View v) {
Intent go_To_FragmentTwo = new Intent(this, FragmentTwo.class);
startActivity(go_to_FragmentTwo);
}
please with sample code. and please introduce some articles.
You would need to do the following:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String ID = ((TextView) view.findViewById(R.id.pid)).getText().toString();
Intent intent = new Intent(view.getContext(), EventDetails.class);
intent.putExtra(pid, ID);
view.getContext().startActivity(intent);
}
The code below is actually also passing the id of the selected item from one fragment to another, to then display the details according to the value in the ID.
Hope this helps :)
I want to get all the items in a list view that way when each one is clicked they will open the second activity?
Here I can get a specific list view item and use an if statement to say if this is clicked open/launch the second activity but I want to be able to say if any list view item is clicked open/launch the second activity how do I do that I know it sounds simple??
public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
{
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
}
});
when I remove the if statement it keeps crashing logcat states:"RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list" am using :android:id="#android:id/list" my activity also extends ListActivity? I dont't get it
If I understand your question:
but I want to be able to say if any list view item is clicked open/launch the second activity how do I do that I know it sounds simple??
Just remove the if block like this:
public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
{
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
}
});
And your second activity will be opened on any item click.
Hope this helps...
How to display the details of any of the items clicked in a listview in another activity?
I referred to this. But unfortunately couldn't understand as to how to go about in displaying the details. Could anyone please help me on this?
you can use putExtra of intent and can pass any value or any serialized class object
yourActivity.java
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
Intent intent = new Intent(yourActivity.this,item.class);
intent.putExtra("item",parent.getSelectedItem().toString);
startActivity(intent)
}
item.java
Intent intent = getIntent();
String itemText = intent.getStringExtra("item");
textView.settext(itemText);
hope u got the point..:
This is in the initial Activity when my app starts up. The gridview contains six click able icons that i want to all go to different activities. I have aboslutely no problem starting up a new activity for another thing to do in my app. I created one activity just to work with initially, a 'contact us' form. I want only one of these icons to go to that activity, however i can't find a way to make it so the onItemClickListener callback registers individual clicks for each icon and launches the appropriate activity; currently, no matter which icon i click, they all go to the same activity. See below for my code:
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Intent myIntent = new Intent();
myIntent.setClassName("com.beneast.main", "com.beneast.main.ContactUs");
startActivity(myIntent);
};
});
I am a bit of a noob at doing this as you can plainly see, this app is getting developed as i learn more stuff. But thanks for any help.
You need to make use of the int position parameter in the onitemClick method. The position parameter is the position of the icon in the gridview which has been clicked. So i would use a switch-case statement for your need as follows:
gridview.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
switch(position)
{
case 0:
Intent myIntent = new Intent();
myIntent.setClassName("com.beneast.main", "com.beneast.main.ContactUs");
startActivity(myIntent);
break;
case 1:
Intent myIntent = new Intent();
myIntent.setClassName(X, Y);
startActivity(myIntent);
break;
}
};
});
You would continue this to cover all possible cases i.e. all icon positions in the gridview.