I wanted to devise some intents between two activities. I passed the intent without declaring anything . I wanted to define Adapter to inflate listview in the intent but I came across the issue when the intent is opened . It is caused by listview. How can I solve it out?
Here are the code below.
public class AttactivePlacesActivity extends AppCompatActivity {
ArrayList<City> c = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
MainActivity m = new MainActivity();
c = m.mArraylist;
AAdapter r = new AAdapter(this,c,R.color.mainBackground);
ListView listView = (ListView) findViewById(R.id.aa);
listView.setAdapter(r);
}
}
R.layout.a
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/aa"
tools:context="com.example.android.tourguide.AttactivePlacesActivity">
</ListView>
gridview Intent
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
switch (position){
case 0:
Toast.makeText(getApplicationContext(),"A",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,AttactivePlacesActivity.class);
startActivity(intent);
break;
}
}
});
You create a reference of MainActivity to get the mArrayList, don't you? Firstly, pls don't create any reference of an activity in another one. Secondly, if you want to get the mArraylist in the second activity, just put it in intent like this:
intent.putSerializable("My array",mArrayList);
and receive it by using:
c = getIntent.getSerializable("My array");
P/S: Some advices for you, please name the variable by the meaningful name, try to learn the OOP java carrefully before starting with Android. And the last one, searching before asking
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);
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
I am working on android applications. In my project I need to create Listview. My layout i.e info.xml contains topbar with one image view, footer with other imageview. Also in the center of the layout I kept an imageview and on that I have created the Listview. Also I have created row.xml for the textview to display listitems. Now when I click on each listitem a new intent should be called...i.e when I click on 1st listitem page1 should open. Similarly if I click on 2nd listitem page2 should open and so on. So how could I do that. I am struggling for this since 3 days but didnt find any correct solution. Please hgelp me regarding this.
My Code:
public class Information extends Activity
{
private String[] Countries;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
Countries = getResources().getStringArray(R.array.countries);
ListView list = (ListView)findViewById(R.id.listnew);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.row, Countries);
list.setAdapter(adapter);
registerForContextMenu(list); }
}
All you have to add is
list.setOnItemClickListener(this);
Then you let your class implement the OnItemClickListener interface and create this method:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position){
case 0:
Intent firstIntent = new Intent(this, MyClass.class);
startActivity(firstIntentIntent);
break;
case 1:
Intent secondIntent = new Intent(this, MySecondClass.class);
startActivity(secondIntentIntent);
break;
[... etc ...]
}
}
In this case, if the first item is clicked, it will launch the MyClass Activity, if the second item is clicked, the MySecondClass Activity will be launched, etc.
Yes, it is a bit tedious but it is the best way.
i have made menu list containing play, settings, exit. but clicking the button doesn take me to the desired activity the listner is not working..can any one help me solve the problem.. will b thankfull..
yes its a list view control..
there are two errors in my code one is that
## The type new AdapterView.OnItemClickListener(){} must implement the inherited abstract method AdapterView.OnItemClickListener.onItemClick(AdapterView, View, int, long)## in LINE1
And the other one is ## View cannot be resolved to a type## in LINE 2
"Actually i want to shift from one screen to another when i click on the item in main menu screen"
Here is the code of main menu
public class MenuActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
ListView menuList = (ListView) findViewById(R.id.list);
String[] items = {
getResources().getString(R.string.pla),
getResources().getString(R.string.sco),
getResources().getString(R.string.set),
getResources().getString(R.string.hel),
getResources().getString(R.string.qui)
};
ArrayAdapter < String > adapt = new ArrayAdapter < String > (this, R.layout.menu_items, items);
menuList.setAdapter(adapt);
menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() { //LINE 1 error
public void onItemClick(AdapterView <? > parent, View itemClicked, //LINE 2 error
int position, long id) {
TextView textView = (TextView) itemClicked;
String strText = textView.getText().toString();
if (strText.equalsIgnoreCase(getResources().getString(
R.string.pla))) {
// Game
startActivity(new Intent(MenuActivity.this,
GameActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(
R.string.hel))) {
// Help
startActivity(new Intent(MenuActivity.this,
HelpActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(
R.string.set))) {
//Settings
startActivity(new Intent(MenuActivity.this,
SettingsActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(
R.string.sco))) {
// Scores
startActivity(new Intent(MenuActivity.this,
ScoresActivity.class));
}
}
});
}
}
ListView lv = ...;
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0,View arg1, int arg2, long arg3)
{
//Do Your Stuff
}
});
or you can make your custom adapter and write the button's click event
use intent
Intent play= new Intent(getApplicationContext(),Play.class);
startActivity(play);
there's lots of tutorial about menu
This task can be accomplished using one of the android's main building block named as Intents and One of the methods public void startActivity (Intent intent) which belongs to your Activity class.
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
Refer the official docs -- http://developer.android.com/reference/android/content/Intent.html
public void startActivity (Intent intent) -- Used to launch a new activity.
So suppose you have two Activity class and on a button click's OnClickListener() you wanna move from one Activity to another then --
PresentActivity -- This is your current activity from which you want to go the second activity.
NextActivity -- This is your next Activity on which you want to move.
So the Intent would be like this
Intent(PresentActivity.this, NextActivity.class)
Finally this will be the complete code
public class PresentActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent activityChangeIntent = new Intent(PresentActivity.this, NextActivity.class);
// currentContext.startActivity(activityChangeIntent);
PresentActivity.this.startActivity(activityChangeIntent);
}
});
}
}
This exmple is related to button click you can use the code anywhere which is written inside button click's OnClickListener() at any place where you want to switch between your activities like in your setOnItemClickListener.
I am creating an android application in which there are two activites. One activity contains a button. On the button click, i want to switch to other activity which displays a list view containing few options.
Two switch between the screens or activities , i am using the following code
Intent intent = new Intent(Activity1.this,Activity2.class);
startActivity(intent);
Since my Activity2 class extends the 'ListActivity', this code doesn't seem to work. On my button click, i want to display a list view containing some data.
Any help would be appreciated
#Siddharth
i seem to be doing almost the same thing
Here is my actual code
From Activity 1
public void onClick(View v) {
Intent intent = new Intent(View_Data.this,CategoryList.class);
startActivity(intent);
}
In Activity 2
public class CategoryList extends ListActivity {
public TextView selection;
public String[] categories;
ArrayList<String> type_of_category;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category_list);
getCategories();
}
public void getCategories() {
DBHelper dbhelper = new DBHelper(this);
type_of_category = new ArrayList<String>();
type_of_category = dbhelper.getTypesOfQuotes();
String[] items = new String[100];
for(int i=0;i<type_of_type_of_category.size();i++)
{
items[i] = type_of_type_of_category.get(i);
}
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items));
}
}
Here is my XML File
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
In My 2nd Activity, i get the error in this line
setContentView(R.layout.category_list);
Depending on whether your data in the second activity is from a database or static data, this code should work for you. I am assuming from your post that you dont need to send data from the 1st activity to the 2nd activity. This is actual code from my application which is database driven. If you are not using a database, parts of this code can be changed to use that instead of a database. It should get you started:
From the 1st Activity:
public void onClickListContacts(View target)
{
Intent listContacts = new Intent(this, com.dzinesunlimited.quotetogo.ContactsList.class);
startActivity(listContacts);
}
The 2nd activity:
public class ContactsList extends ListActivity {
DBAdapter dbContactList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts_list);
dbContactList = new DBAdapter(this);
// Calls the method to display the ListView of all Contacts
fillData();
}
private void fillData() {
// Pull the data from the database
String[] fields = new String[] { dbContactList.TABLE_CON_NAME };
int[] views = new int[] { android.R.id.text1 };
Cursor c = dbContactList.getAllContacts();
startManagingCursor(c);
// Set the ListView
ListAdapter prjName = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, c, fields, views);
setListAdapter(prjName);
dbContactList.close();
}
For a static list, you can also refer to this tutorial: http://www.vogella.de/articles/Android/article.html#lists
Hope this helps.
PS: It would be great help if you could post code of the 2nd activity which has problems.
Add this to your XML and see if that helps:
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</ListView>