I have two string arrays, here s1[] contains a list of names and s2[] contains URL's associated with the respective name, now i need to populate ListView, with the names and on clicking any of the names, i want to start an intent for the browser to handle the URL.
How do i do that?
Use an array adapter to populate the listview from s1, and in the click handler for the listview find the URL for the given list position, and fire off an intent to the browser.
For an example of using an array adapter see the API demos and in particular list1.
For an example of setting an activity as an onItemClickListener see https://github.com/nikclayton/android-squeezer/blob/cache-server-data/src/com/danga/squeezer/AlbumsListActivity.java#L88 and https://github.com/nikclayton/android-squeezer/blob/cache-server-data/src/com/danga/squeezer/AlbumsListActivity.java#L288.
Create a class like:
class Link{
public String name;
public String link;
}
You can then create a custom listview which extends 'ArrayAdapter' and override 'getView' as well as 'onItemSelected'. In both of these method you will be able to get the item using the 'position' parameter.
public class MyActivity extends ListActivity{
private ArrayList<String> urls = new ArrayList<String>();
private ArrayList<String> names = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
urls.add("http://www.google.com");
names.add("google");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, names);
setListAdapter(adapter);
}
protected void onListItemClick (ListView l, View v, int position, long id){
String url = urls.get(position);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
}
Related
i have on my Activity1 a ListView with 10 items. Based on which item i click, i want to pass just a part of a StringArray to my next Activity. I want to bind passed StringArray over an ArrayAdapter to a GridView.
First Problem:
I don´t understand how can i pass something in the next Activity, DEPENDING on the clicked item in the ListView of my Activity1
Second Problem:
How can i get just parts of my StringArray. My String Array has 200 items. Now i want to pass (depending on itemclick in Activity1) just the items i really need.
Here is my code
public class MainActivity extends Activity {
// ListView items
String[] provinces = new String[]{
"Prozentrechnung, Terme und Brüche",
"Gleichungen",
"Ungleichungen und Beträge",
"Geraden, Parabeln und Kreise",
"Trigonometrie",
"Potenzen, Wurzeln und Polynome",
"Exponentialfunktionen und Logarithmen",
"Trigonometrische Funktionen",
"Differenzialrechnung",
"Integralrechnung"
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView provincelist = (ListView)findViewById(R.id.lvProvinceNames);
//add header to listview
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.listheader, provincelist, false);
provincelist.addHeaderView(header, null, false);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, provinces);
provincelist.setAdapter(adapter);
provincelist.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
//we use the items of the listview as title of the next activity
String province = provinces[position-1];
//we retrieve the description of the juices from an array defined in arrays.xml
String[] provincedescription = getResources().getStringArray(R.array.provincedescription);
//List<String> aufgabenListe = new ArrayList<String>(Arrays.asList(provincedescription));
//final String provincedesclabel = provincedescription[position-1];
Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
intent.putExtra("position",position);
intent.putExtra("province", province); //aktualisieren der Titel in der DetailActivity
intent.putExtra("provincedescription", provincedescription); //befüllen der GridView
startActivity(intent);
}
});
}
}
Here is the Activity2 where i have to bind my items to a GridView.
public class DetailActivity extends Activity {
String title;
String[] array;
int position;
//int image;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detailactivity);
TextView tvTitleLabel = (TextView)findViewById(R.id.tvTitleLabel);
GridView gridView = (GridView) findViewById(R.id.gridView);
ArrayAdapter<String> adapter;
Bundle extras = getIntent().getExtras();
position = extras.getInt("position");
if (extras != null) {
title = extras.getString("province");
tvTitleLabel.setText(title);
/////Fehlermeldung: array = null --> NullPointerException
array = extras.getStringArray("provincedescription");
gridView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
}
});
//adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);
//gridView.setAdapter(adapter);
}
}
}
UPDATE: Here is my string array
<string-array name="provincedescription">
<item>A1.1</item>
<item>A1.2</item>
<item>A1.3</item>
<item>A1.4</item>
<item>A1.5</item>
<item>A1.6</item>
<item>A1.7</item>
<item>A1.8</item>
<item>A1.9</item>
<item>A1.10</item>
<item>A1.11</item>
<item>A1.12</item>
<item>A1.13</item>
<item>A1.13</item>
<item>A1.14</item>
<item>A2.1</item>
<item>A2.2</item>
<item>A2.3</item>
<item>A2.4</item>
<item>A2.5</item>
<item>A2.6</item>
<item>A2.7</item>
<item>A2.8</item>
<item>A2.9</item>
<item>A2.10</item>
<item>A2.11</item>
<item>A2.12</item>
<item>A3.1</item>
<item>A3.2</item>
<item>A3.3</item>
<item>A3.4</item>
<item>A3.5</item>
<item>A3.6</item>
<item>A3.7</item>
<item>A3.8</item>
<item>A3.9</item>
<item>A3.10</item>
<item>A3.11</item>
<item>A3.12</item>
</string-array>
if I understand what you want maybe you should take a look at Singleton, I use and works great for now
https://gist.github.com/Akayh/5566992
Credits: https://stackoverflow.com/a/16518088/3714926
For resources like yours that are fixed its better to use string-array in your xml file. From Java I prefer to use static array in your case. Heres a sample:
public class Constants {
public static final String[] provinces = new String[] {
"Prozentrechnung, Terme und Brüche", "Gleichungen",
"Ungleichungen und Beträge", "Geraden, Parabeln und Kreise",
"Trigonometrie", "Potenzen, Wurzeln und Polynome",
"Exponentialfunktionen und Logarithmen",
"Trigonometrische Funktionen", "Differenzialrechnung",
"Integralrechnung" };
}
Then I can access the provinces from anywhere from my class like this:
String iWant = Constants.provinces[0];
Very Important Note
Static objects are dangerous in a number of scenarios and they are usually present in memory so use them sparingly.
As for the string array you cannot get a single element directly from the string-array defined in xml. For that you need to first get all the elements from the array:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.provincedescription);
This is my first time using eclipse for android, I want to know how can I get a value from listview and send it to another activity; something like session in c#
I want to make it so when I choose one of item in listview, and send it to another activity
listview1.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
/*some code to save data in MainActivity*/
Intent in = new Intent(MainActivity.this,Order.class);
startActivity(in);
}});
and show it in another listview in another activity;
array_list=new String[7];
/*array_list[0]= *something to get data from MainActivity* */
ListView lv = (ListView) findViewById(R.id.listViewMakanan);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array_list);
lv.setAdapter(adapter);
EDIT:
this is my array string in MainActivity:
private String array_list[];
array_list=new String[7];
array_list[0]="Nasi Gr Seafood";
array_list[1]="Nasi Gr Magelangan";
array_list[2]="Cap Cay Goreng";
array_list[3]="Cap Cay Kuah";
array_list[4]="Sapi Cabe Hijau";
array_list[5]="Iga Lada Hitam";
array_list[6]="Sapo Tahu Ayam";
and I use this to put my array in ListView:
ListView lv = (ListView) findViewById(R.id.listview1);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
lv.setAdapter(adapter);
Make it like this.
listview1.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Toast.makeText(getApplicationContext(), array_list[position], Toast.LENGTH_LONG).show();
/*some code to save data in MainActivity*/
Intent in = new Intent(MainActivity.this,Order.class);
in.putExtra("ListValue", array_list[position]);
startActivity(in);
}});
In your Order.class
//to get the value from the MainActivity.this.
String value= getIntent().getExtras().getString("ListValue");
Hope this will help you.
You can use Intent to pass the data between Activities.
Second Solution:
You can use public static Arraylist<String> MyArraylist in your MainActivity & you can access it in another Activity as MainActivity.MyArraylist.
I am developing android apps. In my apps having two activities, first activity is displaying list and in second activity having spinner and listview in the same activity and when user click on the item from spinner the listview will be displayed. when user navigate from first activity to second activity then spinner is properly populated with listview. but problem is that after listview displayed properly then spinner item was blank. I don't know where i am doing wrong. Please anybody have solution.
Here i am posting few code of Second Activity
public class ProjectDetailActivity extends SherlockListActivity {
private List<String> list = new ArrayList<String>();
private ArrayList<HashMap<String, String>> list2 = new ArrayList<HashMap<String,String>>();
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_project_detail);
//get spinner item from server when user comes from first activity.
new LoadPhaseData().execute();
//Listener for Phase spinner
projSpinnerPhase.setOnItemSelectedListener((OnItemSelectedListener) new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//get listview when user click item from spinner
new LoadPhaseData().execute();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
//this adapter for listview when click item from spinner
ListAdapter phaseAdapter = new SimpleAdapter(getApplicationContext(),
list2, R.layout.phase_avail_list_item,
new String[] {PHASE_NAME}, new int[]
{R.id.phaseName});
setListAdapter(phaseAdapter);
}
private class LoadPhaseData extends AsyncTask<String, Void, Void> {
#Override
protected Void doInBackground(String... params) {
//Here I am calling web service for spinner and listview
}
#Override
protected void onPostExecute(Void result) {
//following adapter for spinner item
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
getApplicationContext(),android.R.layout.simple_spinner_item,list);
dataAdapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
projSpinnerPhase.setAdapter(dataAdapter);
}
}
}
Thanks in advance
This is because you are assigning new Adapter to the Spinner with new values while you are adding new rows to Spinner. Spinner losses its previous row along with the previous data and get another Adapter with new data rows. You need to append these value to the existing data-holder (may be an array or ArrayList) and then call the adapter.notifyDataSetChanged();
You need to move ArrayList to the class-level, (i.e make it class field) then while you have downloaded data. just append new data to the list and call adapter.notifyDataSetChanged();
This is my ListActivity after clicking on the list it will start a new activity which shows Full detail of each attraction place. I have no idea how to implement Full detail page. Can you guys show me some code of full detail activity which retrieve data from the previous list?
public class AttractionsListActivity extends ListActivity {
private static MyDB mDbHelper;
String[] from = new String[] { Constants.TITLE_NAME , Constants.ADDRESS_NAME };
int[] to = new int[] {R.id.place_title, R.id.place_address};
private Cursor c;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new MyDB(this);
mDbHelper.open();
c = mDbHelper.getplaces();
setListAdapter(new SimpleCursorAdapter(this,
R.layout.list_place, c,
from, to));
final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View view,int position, long id) {
Intent newActivity = new Intent(view.getContext(), Place.class);
startActivity(newActivity);
}
});
}
}
I have no idea how to implement this activity to deal with the action from AttractionslistActivity.Class
public class Place extends Activity {
private static final String CLASSTAG = Place.class.getSimpleName();
private TextView title;
private TextView detail;
private TextView location;
private TextView phone;
private ImageView placeImage;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(Constants.TAG, " " + Place.CLASSTAG + " onCreate");
this.setContentView(R.layout.detail_place);
this.title = (TextView) findViewById(R.id.name_detail);
//title.setText(cursor.getString(Constants.TITLE_NAME));
this.detail = (TextView) findViewById(R.id.place_detail);
this.location = (TextView) findViewById(R.id.location_detail);
this.phone = (TextView) findViewById(R.id.phone_detail);
this.placeImage = (ImageView) findViewById(R.id.place_image);
}
}
Override onListItemClick(...) in your List Activity, using the Cursor get the id of the selected item
Start your Detail Activity passing the id through the intent extras
In your Detail Activity, recover the id and open a cursor to get your data back from the DB.
Fill your view with the data
The Android Notepad Tutorial includes an example of exactly this, if I understand the question correctly.
First of all sorry for the late answer,
in your adapter class you can use item click listener achieve this easily, please do the following steps for achieving this.
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(
AdapterView parent, View view,int position, long id) {
Intent newActivity = new Intent(
view.getContext(), Place.class);
startActivity(newActivity);
}
});
for passing list item details into next screen you can use intent extras.before calling start activity u need pass the extras like below.
newActivity.putExtra("name", c.place);
newActivity.putExtra("description", c.placeDscription);
newActivity.putExtra("distance", c.distance);
after u need to call ---> startActivity(newActivity);
if you want pass string, Integer, float, boolean etc...types of data you can like above code.
I'm using a ListActivity on wich I get the Access Points in the WIFI range, and list them in a cheking list.
I've been successfull doing this, but I would like to get the cheked items when I click in the footer button. How do I get them to a String array??
This is the code:
public class APselection extends ListActivity {
protected static final String TAG = "teste";
/** Called when the activity is first created. */
private TextView mScanList;
public List<ScanResult> listaAPs;
protected WifiManager wifiManager;
private IntentFilter mWifiStateFilter;
public String SCAN;
public String[] SCANed;
public String scanAP;
public ListView lv;
public ListView lv1;
public List<Long> list = new ArrayList();
public String[] checked;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
View footer = View.inflate(this, R.layout.footer, null);
wifiManager= (WifiManager)getSystemService(Context.WIFI_SERVICE);
int i;
//function to get the APs
SCANed=handleScanResultsAvailable().split("SPl");
lv=getListView();
lv.addFooterView(footer);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, SCANed));
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setTextFilterEnabled(false);
}
}
There are several ways to do this. The easiest is probably to implement the OnClickListener for the ListView. When the user clicks a list item, extract the String from the clicked item. Store the items in an ArrayList. When the user clicks, if the list contains the String, remove it, otherwise add it. Bam. List of all selected items.
I don't think there is a way to directly get the checked item to a String array from the ListView, you have to go through intermediate steps :
You can use ListView.getCheckedItemIds to get an array of the id of the checked items. These id are assigned by your list adapter. Since you're using an ArrayAdapter, position=id, so you can just use ArrayAdapter.getItem() to get the String associated with each checked item id.
This would look like :
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
long ids[] = lv.getCheckedItemIds();
String checkedItems[] = new String[ids.length];
for (int i=0; i<ids.length; i++)
checkedItems[i] = adapter.getItem(i);
//You got your array of checked strings
}
}
Note that this require access to the adapter, so you would have to assign your ArrayAdapter to a variable.