My list includes eight items positioned beneath a 'header' row, all contained within a tab on my application. I cannot figure out how to change to an activity (or possibly a different tab) based upon the item clicked on in the list.
I'm currently extending the Activity class, not sure if this is an issue. I've attempted to use the onListItemClick whilst extending the ListActivity class; this however caused the application to crash.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class Tab2 extends Activity {
private ListView listView1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
Cinema cinema_data[] = new Cinema[]{
new Cinema(R.drawable.blue, "Blue Cinema"),
new Cinema(R.drawable.green, "Green Cinema"),
new Cinema(R.drawable.purple, "Purple Cinema"),
new Cinema(R.drawable.red, "Red Cinema"),
new Cinema(R.drawable.yellow, "Gold Cinema"),
new Cinema(R.drawable.blue, "Cyan Cinema"),
new Cinema(R.drawable.green, "Lime Cinema"),
new Cinema(R.drawable.purple, "Magenta Cinema")
};
CinemaAdapter adapter = new CinemaAdapter(this,
R.layout.listview_item_row, cinema_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
}
any help will be greatly appreciated!
edit:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
try {
Intent i = new Intent("android.lab.two.Tab1");
startActivity(i);
} catch(Exception e){
e.printStackTrace();
}
}
if the OnItemClickListener doesn't work, you can try setting it in your adapter :
in your adapter, in the getView method, you can add an OnClickListener to your row view like this :
public View getView(int arg0, View arg1, ViewGroup arg2) {
//Create the view for your row
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, 64);
TextView rowView = new TextView(getApplicationContext());
rowView.setLayoutParams(lp);
rowView.setText("your value");
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
//Starting a new intent ( here a Dial Activity )
Intent newIntent = new Intent(Intent.ACTION_DIAL);
newIntent.setData(Uri.parse("tel:"+value));
startActivity(newIntent);
} catch (ActivityNotFoundException e) {
Log.e("your application", "Dial failed", e);
}
}
});
}
You just set the click listener on your listview. You get the position and perform you logic from there. The code below should help you get started. I don't know what error you were getting, but there is not much that can go wrong.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(view.getContext(),
NextClass.class);
startActivity(myIntent);
}
});
Use the setCurrentTab method in TabHost to change tab based on the list item index.
Fire intent to this activity itself keeping it in the single top launch modes & then using the bundled data change the tab via tab host.
I hope this helps..
You cannot start an activity from a TabHost. You are in a inner class. You must use NestedClasses.
Instead of starting an Intent in your onClickListener, try to Log something, and you will see that will work.
Log.d("LOG","Message");
if you set onListItemClick() method inside your tab(activity) then you have to use
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this,tab1.class);
startActivity(i);
}
if you set onListItemClick() method is define in other class then you have to use
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Intent i = new Intent(v.getContext(),tab1.class);
startActivity(i);
}
if tab1(activity) is a main activity and you have to call tab2(activity) inside the tab1(activity) then you have to use this way...
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
System.exit(0);
}
Related
please I'm trying to add understand how to add onItemClickListener to the followng code such that when "Smartphone Plans" is clicked, its activity starts and so on. I've seen other questions on StackOverflow relating to this question but do not understand how to go about them.
I've already added an onItemClickListener but do not understand how to set it to specific list items.
here is the code
package devchuks.com.rechargeit;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.app.ListActivity;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class EtisalatData extends AppCompatActivity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_etisalat_data);
listView = (ListView) findViewById(R.id.list);
String[] values = new String[] {
"Smartphone Plans",
"Internet Bundles",
"Weekend Plans",
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
}
You can define your listView.setOnItemClickListener like this to go to different activity for clicking different element.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String item = listView.getItemAtPosition(position);
Toast.makeText(this,"You selected : " + item,Toast.LENGTH_SHORT).show();
if(position==0) {
// Do your code for clicking "Smartphone Plans" example
// startActivity(new Intent(getApplicationContext(),SmartphonePlans.class));
}
else if(position==1) {
// Do your code for clicking "Internet Bundles". example
// startActivity(new Intent(getApplicationContext(),InternetBundles.class));
}
else if(position==2) {
// Do your code for clicking "Weekend Plans". example
//startActivity(new Intent(getApplicationContext(),WeekendPlans.class));*/
}
});
onItemClick will be called whenever any of the list items are clicked. The position will be the position of the view in the Adapter.
Please refer -
How to handle the click event in Listview in android?
Thanks
Sriram
try this:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String text = values[position];
if(text.equals("Smartphone Plans")){ //your specific list item text
Intent i = new Intent(MainActivity.this, AnotherActivity.class);
i.putExtra("TEXT", text);
startActivity(i);
}
}
}
If this helps and is your concern
`
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String data = values[position];
switch(data){
case "Smartphone Plans":
// do somwthing
break;
// similarly for other two values.
}
}
});`
Below method give you a position of a clicked row. Take advantage of that and value from your array.
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Move your values array to member array or make it final to use
// in Anonymous class
final String selectedValue = values[position];
Intent intent = null
// Now add case and start activity
if("Smartphone Plans".equals(selectedValue)) {
intent = new Intent(EtisalatData.this, SmartPhonePlan.class);
}
else if("other Plans".equals(selectedValue)){
// other action
}
//... more cases and at the end start your activity if its not null
startActivity(intent);
}
I have a FragmentActivity that controls a ListFragment; that ListFragment contains a generic ListView, Adapter, and draws an ArrayList from a Singleton that I have created.
When in the onCreateView method of my ListFragment I put the following code:
public View onCreateView(LayoutInflater viewInflation, ViewGroup container,
Bundle SavedInstantState) {
cycleviewfragment = viewInflation.inflate(
R.layout.cycleviewfragment_page, container, false);
context = getActivity().getApplicationContext();
Singleton mySingleton = Singleton.getInstance();
basicList = (ListView) cycleviewfragment.findViewById(android.R.id.list);
adapter = new ArrayAdapter<listControlObject>(getActivity()
.getApplicationContext(), android.R.layout.simple_list_item_1,
mySingleton.getA1());
this.setListAdapter(adapter);
addButton = (Button) cycleviewfragment.findViewById(R.id.addbutton);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getActivity(),
listaddactivity.class);
getActivity().startActivity(myIntent);
}
});
basicList.setOnItemClickListener(new ListView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Log.d("basicListtester", "Testing onClickItem call");
Intent myIntent = new Intent(getActivity(),
listdetailactivity.class);
myIntent.putExtra("selectedObjectIndex",arg2);
getActivity().startActivity(myIntent);
}
});
adapter.notifyDataSetChanged();
return cycleviewfragment;
}
Any ideas as to why when I add items to my list they do not react and the OnItemClick is not called?
Thanks guys.
[Update]
I tried implementing it with basicList.setAdapter(adapter); which still did not work.
also tried having my ListFragment implement OnItemClickListener and added the method to the class; which did not work either.
Since you use ListFragment you shouldn't set onItemClickListener to your list. There is already a method in ListFragment that you should override.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//Do your thingy.
}
Use
basicList.setListAdapter(adapter);
instead of
this.setListAdapter(adapter);
Use same instance of ListView for setting Adapter and setOnItemClickListener.
you should override onListItemClick, since your class extends ListFragment. From the doc:
This method will be called when an item in the list is selected.
Subclasses should override.
Here the documentation
My app is a list of firms, each firm is a row in my listview that shows: picture, name and phone (by adapter). Everything is working! But when I click in a item of my listview, it should start another activity that shows page with firm details. I'm having trouble with onitemclick listener (it doesn't work):
empresa = firm (in portuguese) lv = my listview
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Intent i = new Intent (getApplicationContext(), detalheEmpresas.class );
Empresas empresa = (Empresas) parent.getItemAtPosition(position);
i.putExtra("Nome", empresa.getTitle().toString());
startActivity(i); }});
my detalheEmpresas activity
public class detalheEmpresas extends Activity {
#Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.detalhes_empresa);
Intent i = getIntent();
String nome = i.getStringExtra("Nome");
TextView NOME = (TextView) findViewById(R.id.Nome);
NOME.setText(nome);
}}
I had a very similar problem. Solved by setting on click listener to view itself in the list adapter.
public class EmpresasListAdapter extends ArrayAdapter<Empresas> {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//recycling view and setting view fields.
/* Set listener to open firm detail activity on click */
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code to open firm detail activity
Intent i = new Intent (getApplicationContext(), detalheEmpresas.class );
Empresas empresa = (Empresas) parent.getItemAtPosition(position);
i.putExtra("Nome", empresa.getTitle().toString());
startActivity(i);
}
});
return view;
}
}
could very well be related to the getApplicationContext...
try to use activity context:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Intent i = new Intent (mSavedContext.getActivity(), detalheEmpresas.class ); <-------------
Empresas empresa = (Empresas) parent.getItemAtPosition(position);
i.putExtra("Nome", empresa.getTitle().toString());
startActivity(i);
}});
---------- new edit -----------------
ok how about you just add a click handler to the parent activity and you should be good to go:
public class ParentActivity extends Activity implements OnItemClickListener {
your activity code...
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Intent i = new Intent (getActivity(), detalheEmpresas.class );
Empresas empresa = (Empresas) parent.getItemAtPosition(position);
i.putExtra("Nome", empresa.getTitle().toString());
startActivity(i);
}
in your activity code replace
lv.setOnItemClickListener(new OnItemClickListener() {
with
lv.setOnItemClickListener(this);
I have created one listview of some names,what i need is when i will click selected row it will go to that page only,on click on different row it will move to the same class but different content.I think it will move by question id.could anybody help me how to pass the question id Or any other method to do this..
here is my code..
private OnItemClickListener mlist = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
}
};
You can try something like this -
private OnItemClickListener mlist = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
if(Some condition)
{
Intent i= new Intent(YourActivity.this,ActivityOne.class);
// To pass data
i.putExtra("SomeId", someValue);
startActivity(i);
}
else if(Some other condition)
{
Intent i= new Intent(YourActivity.this,SecondActivityTwo.class);
startActivity(i);
}
else
{
// Do something else--
}
}
};
And in the other activity -
String identifier = getIntent().getExtras().getString("SomeId");
Here I've given an example assuming you have user list and clicking on item you want to show user profile...
In List_Act activity...
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = mInflater.inflate(R.layout.rowitem,parent,false);
convertView.setTag(UserId);
}
private OnItemClickListener mlist = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent i=new Intent(List_Act.this, Profile_Act.class);
int UserId = ((View)v.getParent()).getTag();
i.putExtra("UserId", UserId); //Setting variable you want to pass to another activity
startActivity(i);
}
};
in Profile_Act activity in onCreate()
String UserId = getIntent().getExtras().getString("UserId"); //retrieving value in another activity
now you'll have UserId variable set and you can use it...
I currently have a ListActivity and I am looking to start a new activity based on the selection in the list. Based upon Intents and Extras, how can I make this possible? Here is my code so far:
package com.fragile.honbook;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class GuideSelection extends ListActivity{
private TextView selection;
private static final String[] heroes={ "Agility", "Intelligence",
"Strength"};
public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.heroselect);
setListAdapter(new ArrayAdapter<String>(this,
R.layout.row, R.id.label,
heroes));
selection=(TextView)findViewById(R.id.select);
}
public void onListItemClick(ListView parent, View v,
int position, long id){
}
}
Like this
If you want to call an activity called NewActivity after clicking the list item with sending data as extra you can do this
public void onListItemClick(ListView parent, View v,
int position, long id){
Intent intent = new Intent(GuidSelection.this, NewActivity.Class());
intent.putExtra("DATA",heroes[position]);
GuideSelection.startActivity(intent);
}
public void onListItemClick(ListView parent, View v, position, long id){
Intent intent = new Intent(GuideSelection.this, NewActivity.class);
intent.putExtra("hero", heroes[position]);
startActivity(intent);
}
Update (with different activities per list item):
final Map<String, Class> activities = new HashMap<String, Class>();
{
activities.put("agility", AgilityActivity.class);
activities.put("intelligence", IntelligenceActivity.class);
// add more here
}
public void onListItemClick(ListView parent, View v, position, long id){
Intent intent = new Intent(GuideSelection.this, activities.get(heroes[position]));
intent.putExtra("hero", heroes[position]);
startActivity(intent);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if(position==1){
//First item clicked.
Intent intent = new Intent(this,NewActivity.class);
startActivity(intent));
}
// handle else ifs
}
This is just an idea. You may wish to improvise this to too much of if elses.