OK, I realize this is prolly pretty basic, but im so new to this its unreal.
What I have is an array. What I want is for when a user clicks an item in the array, it opens a new activity that is specific to that item. Its a list of festivals, and when you click on one of the festivals, and when you click on it, it opens an activity that provides information about that festival.
I have no idea what I'm doing here. Im pretty sure I need to use an OnClickListener, but thats it.
Activity
package com.MADONK.LAFESTS;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
public class Home extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.main, Festivals));
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
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
static final String[] Festivals = new String[] {
"Lake Arthur Regatta", "Contraband Days", "Iowa Rabbit Festival",
};
}
Since you're extending ListActivity, you can override onListItemClick(). You could do something like this, which gets the appropriate Festival object, and passes a member of it into an Intent, the Intent is then used to start another Activity:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Festival item = (Festival) l.getItemAtPosition(position);
Intent i = new Intent(v.getContext(),YourFestivalDetailActivity.class);
i.putExtra("some_attribute", item.getSomeAttribute());
startActivity(i);
}
And then in the Activity you start that is meant to show the Festival detail, which in the above example is called YourFestivalDetailActivity, you should extract the Festival information from the Intent used to start it:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String someAttribute = getIntent().getStringExtra("some_attribute");
}
Note that in this example I only pass a single String via the Intent, but know that you can pass more than that via an Intent. See the docs.
Related
I'm trying to see text from GridView using a toast when I selected or clicked on GridView's data. I can see position, but don't know what to put to see a text.
Here is a code.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends Activity {
String [] data = {"a","b", "c","d","e","f", "g","h"};
GridView gdView;
ArrayAdapter<String> adapter;
String result;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, data);
gdView = (GridView) findViewById(R.id.gdView);
gdView.setAdapter(adapter);
gdView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getBaseContext(), position, Toast.LENGTH_SHORT).show();
}
});
}
}
Could someone suggest me, what I need to use or where to look for it?
Thank you
The parameter int position of the method onItemClick() on AdapterView.OnItemClickListener gives you:
The position of the view in the adapter.
See here: https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html#onItemClick(android.widget.AdapterView, android.view.View, int, long)
You can use this to retrieve the content of this view from your ArrayAdapter with:
adapter.getItem(position)
See here: https://developer.android.com/reference/android/widget/ArrayAdapter.html#getItem(int)
This will return the content of your ArrayAdapter at the given position, in your case as a String, which you can store in a variable if you like or pass directly into Toast.makeText().
Turn last string to
Toast.makeText(getBaseContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
I've looked up similar post regarding this question but they didn't solve my issue, so here it is.
I'm trying to set a setOnItemClickListener, for when I click on any item of my list I will open a dialog with the info (retrieve from the list) on the clicked list and more info. But i'm struggling to implement the setOnItemClickListener part.
I'm getting and error in line: myList.setOnItemClickListener(new OnItemClickListener()){
package com.example.proyectoprueba;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;;
public class PlantillaChina extends ActionBarActivity{
// DB Class to perform DB related operations
DBController controller = new DBController(this);
// Progress Dialog Object
ProgressDialog prgDialog;
HashMap<String, String> queryValues;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.lista);
Intent myIntent = getIntent();
String value = myIntent.getStringExtra("key");
TextView titulo=(TextView)findViewById(R.id.titulo);
// Get Platos records from SQLite DB
ArrayList<HashMap<String, String>> platoList = controller.getAllEntremeses();
if (value.equals("entremeses")){
platoList = controller.getAllEntremeses();
titulo.setText(value);
}else if(value.equals("arroces y pasta")){
platoList = controller.getAllArrocesyPasta();
titulo.setText(value);
}else if(value.equals("mar")){
platoList = controller.getAllMar();
titulo.setText(value);
}else if(value.equals("carnes")){
platoList = controller.getAllCarnes();
titulo.setText(value);
}
// If Platos exists in SQLite DB
if (platoList.size() != 0) {
// Set the Plato Array list in ListView
ListAdapter adapter = new SimpleAdapter(PlantillaChina.this, platoList, R.layout.itemlista, new String[] {
"platoId", "platoNombre", "platoDescripcion", "platoPrecio" }, new int[] {R.id.codigo, R.id.nombre, R.id.descripcion, R.id.precio });
ListView myList = (ListView) findViewById(R.id.listaplatos);
myList.setAdapter(adapter);
myList.setOnItemClickListener(new OnItemClickListener()){
}
}
Thank you for your time
It think you want something like this.
myList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id)
{
// You have the position available here as well as the clicked view.
}
});
You need to actual define the OntItemClickListener to do something.
First, the code you have doesn't make much sense. You want something like this:
myList.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
// Your code here
}
});
Basically you are creating your own OnItemClickListener class, and overriding the OnItemClick method. This callback will get called whenever an item in your list is clicked. You can do whatever processing you would like inside that function.
I have an activity where it contains an ActionBar (with four tabs) a fragment respectively assigned to each of these tabs. In these fragments I've assigned some ListAdapters filled with string values, clickable that furthermore I want to operate. When clicking on an item I want that app to send from that fragment to another. I know that I have to use FragmentManager() and FragmentTransaction() but since I'm new to Android dev I demand of any kind of help, help that is appreciated.
Here's the snippet code of one of the tabs(UserFragment.java):
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/** This is a listfragment class */
public class UserFragment extends ListFragment
{
/** An array of items to display in ArrayList */
String user_items[] = new String[]
{
"Account",
"Addresses",
"Payment Providers",
"Profile",
"Transactions",
"Wallet"
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
/** Creating array adapter to set data in listview */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_expandable_list_item_1, user_items);
/** Setting the array adapter to the listview */
setListAdapter(adapter);
getListView().setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id)
{
Intent myIntent = new Intent(getActivity().getBaseContext(), Profile.class);
startActivity(myIntent);
}
});
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onStart()
{
super.onStart();
/** Setting the multiselect choice mode for the listview */
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
The Profile.java activity code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Profile extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_layout);
//Button test = (Button) findViewById(R.id.btnTest);
}
}
You need to define OnItemClickListener for your ListFragment to handle item click events. For example:
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
// start your new activity here
}
});
I found what was wrong. After a long search I learnt that if the onCreateView() method is static than it's all good to set listener/s but in this case while we fill an array-adapter of string than it's a no-go since first of first it has to created its View therefore doesn't let the app to make any further listeners. In order to make that available, the onActivityCreated(Bundle) should be initiated/created between onCreateView() and onStart() methods and insert the rest of the code.
Here's the solution to link a ListFragment to another FragmentActivity class:
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
getListView().setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id)
{
Intent myIntent = new Intent(getActivity().getBaseContext(), Profile.class);
startActivity(myIntent);
}
});
}
i want to add two numbers using spinner view. here in my code two spinners .After i run the emulator it displays straight result only. it does not display spinner control and i'm not able to select the two numbers. Pls give one solution. Thanks in advance. Here code
package com.kk;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.R.id;
public class TrckActivity extends Activity {
/** Called when the activity is first created. */
String[] a={"-select-","1","2"};
String[] b={"-select-","2","4"};
int first,second,f,s,c;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> a1= new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,a);
final Spinner sp1=(Spinner)findViewById(R.id.spinner1);
sp1.setAdapter(a1);
sp1.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
first=sp1.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
ArrayAdapter<String> a2= new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,b);
final Spinner sp2=(Spinner)findViewById(R.id.spinner1);
sp2.setAdapter(a2);
sp2.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
second=sp2.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
if(first==1)
{
f=1;
}
else if(first==2)
{
f=2;
}
if(second==1)
{
s=2;
}
else if(second==2)
{
s=3;
}
c=f+s;
TextView tv=new TextView(this);
tv.setText(""+c);
setContentView(tv);
}
}
This might be because the spinner's "onItemSelected" method gets called initially as soon as your code enters the onCreate method. Maybe you have to maintain flag values to do this.
These links might help you get started with it,
Spinner onItemSelected called erroneously (without user action)
Spinner onItemSelected() executes when it is not suppose to
Try to exchange
android.R.layout.simple_dropdown_item_1line
to
android.R.layout.simple_spinner_item
hey all.. i have been looking at other questions to get help and answers without luck..
my problem is that i want to open different class from my listView. according to the specific name in the listView. . the names are in lv_arr[] ..
If anybody can give a detailed answer i will be very thankfull and happy..
Im new in android and not the best in java :-(
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class StatusActivity extends Activity implements OnItemClickListener {
public ListView lv1;
public String lv_arr[]= {"John", "Andrew","alex","alice","bob","bla bla"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabview);
lv1=(ListView)findViewById(R.id.ListView01);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
lv1.setOnItemClickListener(this);
}
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(getApplication())
.inflate(R.menu.menu, menu);
return(super.onPrepareOptionsMenu(menu));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.close:
super.finish();
break;
case R.id.icontext:
Intent i = new Intent(this, InfoActivity.class);
startActivity(i);
break;
}
return true;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
if(position == 0){
//Intent w = new Intent (this, Seekbar.class);
//startActivity(w);
Toast.makeText(this, "You pressed the first item in the list", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "You pressed all other items in the list", Toast.LENGTH_SHORT).show();
}
}
}
try this....if you have doubts add comment.
edited:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabview);
lv1=(ListView)findViewById(R.id.ListView01);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if(position==0){
Intent i = new Intent(this, InfoActivity.class);
startActivity(i);
} else if(position==1){
start another activity here...
}
}
});
}
hope it helps..
well i think you want to know basically the flow of how it is possible.
If all the classes which should be opened are more likely the same with just text and image changes and same layout then you dont need to create separate class file for each and every item clicked.
Just create one class file and one xml file and on item click pass on the bundle to the class file which will just changes the content on different item clicks and will workk as a charm as with this flow you will have only one class file and one xml file for all the list items you have
If you dont want to keep the same layout then you can obviously make the some views gone, visible or invisible at runtime
try something like this
public String lv_class_names[]= {Activity1.class.getName(), Activity2.class.getName(), Activity3.class.getName(), .....};
In the onItemClick method write
Intent i = new Intent(this, Class.forName(lv_class_name[position]));
startActivity(i);
for more details see this