I have a Listview activity and a Mainactivity.
In my Listview activity i have a array that is
public static final String[] link = {
"http://3313.live.streamtheworld.com/WKRKFM_SC",
"http://3573.live.streamtheworld.com/KXXRFMAAC",
"http://2713.live.streamtheworld.com/KXXRFMAAC",
"http://r16---sn-npo7enel.gvt1.com/videoplayback" };
Im using putExtra and passing this array to my Mainactivity using this.
Intent intent =new Intent(MainActivity.this, Linkview.class);
intent.putExtra("link", link[position]);
startActivity(intent);
And i received my extra using this in Mainactivity.
final Bundle bundle = getIntent().getExtras();
String link = bundle.getString("link");
and i can use it very well but in my MainActivity
I have a next button
next.setOnClickListener(new View.OnClickListener() {
#SuppressWarnings("unused")
#Override
public void onClick(View v) {
//??????
}
});
I want that when i pressed the next button the next array comes from listview activity
and i can show this in my MainActivity.
How i can do that??
So from your question, I assume you want to show the next item in the array.
As long as you keep your array as a static final variable (i.e. a constant array), you can directly access it from your main class. You don't need an instance of the class to access a static variable. For instance, the following should work from your main Activity:
String item = ListviewActivity.link[position];
Thus, you can simply pass the position as an integer, look up the string with the above code, and add one to the position to get the next item.
Related
I want to retrieve a value from the spinner and pass it to the textView of another class and I am not figuring out the way how to do this any help would be appreciated? I am a new to Android :)
Simply this is how you get selected value from a spinner:
String text = mySpinner.getSelectedItem().toString();
There are many ways to pass it to another activity, here are some of them:
Pass it through an intent as extras
Save it as a Singleton
Save it in the shared preferences
Make it some kind of a static variable in a static class
Here is an example using the first way. Suppose you will click a button after selecting a value from a spinner and want to pass the value to the next activity
String text = mySpinner.getSelectedItem().toString();
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
intent.putExtra("mySpinnerValue", text);
startActivity(intent);
}
});
And inside the onCreate method of MainActivity2, do the following:
Bundle bundle = getIntent().getExtras();
String text = bundle.getString("mySpinnerValue");
myTextView.setText(text);
I am working on a project where I am showing multiple image in SwipeView with a button. So whenever the button is clicked a new activity will start according to the position of the image in the array. How to do this?
With each item in your list, you need a Class reference on which Activity you want to start, then assign the click listener to pass that Class to an Intent and then startActivity
Assuming you have some ArrayList<Class> list or an ArrayAdapter<SomeObject> adapter, you could do
public void onClick(View v) {
Class activityClass = list.get(clickPosition);
// Class activityClass = adapter.getItem(clickPosition).getActivityToStart();
Intent intent = new Intent(this, activityClass);
startActivity(intent);
}
You could also use a String tag on the View with the name of a class, but you'll need to catch the ClassNotFoundException.
public void onClick(View v) {
String className = (String) v.getTag();
Class activityClass = Class.forName(className);
Intent intent = new Intent(this, activityClass);
startActivity(intent);
}
Set a tag (relevant to the activity being launched) to each ImageView while inflating, using setTag method.
In the onClick handler retrive the tag using the getTag method. Use this information to launch the required activity, using a simple switch case .
The tag can be anything like activity name, array index etc.
More info: What is the main purpose of setTag() getTag() methods of View?
My first activity contains a listview with textviews in each cell and uses a custom adapter. So if you click on any of the items, it will open up a form activity containing textfields. The user can fill up the details and once they press the save form button the details appear on the listview. Now I am trying to add items to the list dynamically. I have created a button which when clicked adds a new instance item so that more users can register the same way. I have been able to implement these functions. However, my problem now is when i click on the newly added item and go to the form activity and click save, i am not able to see the newly added entry after i come back to the listview activity.All I see is the first entry alone. So i am guessing it gets destroyed as soon as i leave the activity. How to ensure all newly added items are not destroyed when i keep moving between these two activities.
Here is my code of the ListView Activity:
public class FormTableActivity extends Activity {
private PassengerListAdapter adapter;
Button add_passenger;
String mrzdata,ic_data,name_data;
SharedPreferences nPref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final_display_listview);
nPref = PreferenceManager.getDefaultSharedPreferences(this);
mrzdata = nPref.getString("MRZ", "");
name_data = nPref.getString("resultData", "");
ic_data = nPref.getString("icdata", "");
final ListView lv1 = (ListView) findViewById(R.id.custom_list);
adapter = new PassengerListAdapter(this);
adapter.add(new CustomerDetails(ic_data, name_data, mrzdata));
add_passenger = (Button) findViewById(R.id.add_user);
add_passenger.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// mrzdata = "";
// name_data = "";
// ic_data = "";
adapter.add(new CustomerDetails(ic_data, name_data, mrzdata));
}
});
lv1.setAdapter(adapter);
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent intent = new Intent(v.getContext(), FirstActivity.class);
startActivity(intent);
}
});
}
The easiest way to pass data between Intent is
Intent intent = new Intent(getBaseContext(), your_list_view_activity.class);
intent.putExtra("String_Key", data1);
//data1 could be an array of string where you have hold the values previously
startActivity(intent);
now on your list_view_activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
String [] value = extras.getString("String_Key");
}
This way you won't get any exception but you get to populate your listView if there is data.
Another way to get data is via SharedPreference but I won't recommend it as it increases the size of the app.
You have to save these newly added items somewhere , e.g. to a SQLite database and retrieve them on create to populate the listview
You can see here if You want, the code is commented ,
here I have a listview with custom adapter with two items
the feed's name and it's url
i add URL and name using a text input dialog (with two edit text), save to DB, and retrieve them on create to populate the listview
https://github.com/enricocid/iven-feed-reader/blob/master-as/project/app/src/main/java/com/iven/lfflfeedreader/mainact/ListActivity.java
Sir/Madam/Friends,
I am new to android program, I created one Linear layout in this activity I put 20 images, and when i click one image that particular image will open in another activity,in second activity i put only one Image view. So please how to do that?.
i did but my programe will showing only one same image only every time.
//this is the first activity.
public class abcd extends ActionBarActivity{
public final static String VIEW_ID = "name";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abcd);
}
public void click(View view)
{
String iname=getResources().getResourceEntryName(view.getId());
Intent intent=new Intent(this,abcd_disp.class);
intent.putExtra("VIEW_ID", x);
startActivity(intent);
}
}
//this is the second activity
public class abcd_disp extends ActionBarActivity {
ImageView imageView;
int[] im=new int[]{
R.mipmap.a,R.mipmap.b
};
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent=getIntent();
String vidname=(String)intent.getStringExtra(abcd.VIEW_ID);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_abcd_disp);
if(vidname=="aa"){
imageView=(ImageView)findViewById(R.id.img_abcd_disp);
imageView.setImageResource(im[0]);
}
else
{
imageView=(ImageView)findViewById(R.id.img_abcd_disp);
imageView.setImageResource(im[1]);
}
}
The easiest way to do this is:
Suppose you have a set of images in drawable folder with which you are populating ImageView's in your Activity A .
Let the images be #drawable/image1, #drawable/image2, #drawable/image3, #drawable/image4
So once you click on the particular ImageView, you can retrieve image resource name as
imageView.getTag();
But this returns an object. So type cast it to String. This is done to pass it along with intent, since object cannot be passed directly without serializing or parceble.
String imageResourceName = (String)imageView.getTag();
Once you have this, pass this along with the Intent you are going to startActivity() with.
intent.putExtra("resource_name", imageResourceName);
startActivity(intent);
So in Activity B, where there is a ImageView, you set the image src in ImageView using
imageView.setTag(getIntent().getStringExtra("resource_name"));
Further information of setting image using setTag()
Use Fragment instead of the Activity!
I have a ListView in activity1 where I have a few items which are the word from the database . The User Clicks on an item in the Listview and it will navigate to activity2 where it should show the details of the word which are stored in database as column word, definitions . But What appears in Screen2 depends on what item was clicked in Screen 1
For Ex - User Clicks A in Screen 1 - Words starting from A come up in Screen 2. Is there any way to pass the row id so that in next screen the word and definition from the database can be displayed.
Thank you in adv..
code for first activity:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
// TODO Auto-generated method stub
Cursor c = mDictCursor;
c.moveToPosition(position);
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
Bundle bundle=new Bundle();
//intent.putExtra("position",position);
bundle.putLong(DBAdapter.KEY_ROWID, id);
bundle.putString(DBAdapter.KEY_TITLE, c.getString(
c.getColumnIndexOrThrow(DBAdapter.KEY_TITLE)));
bundle.putString(DBAdapter.KEY_DEFINITION, c.getString(
c.getColumnIndexOrThrow(DBAdapter.KEY_DEFINITION)));
i.putExtras(bundle);
startActivity(i);
}
SecondActivity code:
Bundle extras = getIntent().getExtras();
mRowId = extras.getLong(DBAdapter.KEY_ROWID);
String title = extras.getString(DBAdapter.KEY_TITLE);
String body = extras.getString(DBAdapter.KEY_DEFINITION);
TextView word = (TextView) findViewById(R.id.word);
word.setText(title);
TextView definition = (TextView) findViewById(R.id.definition);
definition.setText(body);
}
Whenever I am clicking the listview item it is showing dialog to force close. Please help...
you can use Intent object putExtra(String name, int value) method in Screen1(see1) and pass the intent object to Screen2,in Screen2 use getIntExtra(String name, int defaultValue) method of Intent object(see 2).
1.use startActivity(Intent intent) method in Screen1
2.use getIntent() method in Screen2 to get Intent object which you passed in the Screen1
I guess you are looking something similar like this .
https://market.android.com/developer?pub=acharya
https://market.android.com/details?id=com.acharyaapp.malayalam.aksharam.full
(Those are my apps)
Why dont you try using a Singleton class to store information. One Intent can set it and the other intent can read it. I employed that logic in my apps.
EDIT
public class MySingleton {
private static final MySingleton INSTANCE = new MySingleton();
//TODO ... all your variable as public static variables. eg. KEY_TITLE
private MySingleton() { }
public static MySingleton getInstance()
return INSTANCE;
}
}
Inside activities, use them as normal instances. Just make sure to call the getInstance() instead of constructor to get the shared instance.
hope this helps.