Reputation: 8408
I'm trying to get my list view to navigate to different activities based on a specific item that has been tapped. I know that the code is what's need but I don't know what I need to change my to
package com.apptacularapps.exitsexpertlondonlite;
import android.app.ActionBar;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
public class WC_line extends ListActivity {
ArrayList<Item> items = new ArrayList<Item>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
items.add(new EntryItem("Bank", "Fare zone 1"));
items.add(new EntryItem("Waterloo", "Fare zone 1"));
EntryAdapter adapter = new EntryAdapter(this, items);
setListAdapter(adapter);
}
@Override
public void onListItemClick(AdapterView<?> adapterView, View view, int position, long id){
//When clicked, go to specific activity
if (position == 0) {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), activity_1.class);
startActivityForResult(myIntent, 0);
}
};
}
XML
@Override error
package com.apptacularapps.exitsexpertlondonlite;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
public class WC_line extends ListActivity {
ArrayList<Item> items = new ArrayList<Item>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView list = getListView();
setContentView(R.layout.activity_wc_line);
items.add(new EntryItem("Bank", "Fare zone 1"));
items.add(new EntryItem("Waterloo", "Fare zone 1"));
EntryAdapter adapter = new EntryAdapter(this, items);
setListAdapter(adapter);
}
@Override
public void onListItemClick(AdapterView<?> adapterView, View view, int position, long id){
//When clicked, go to specific activity
if (position == 0) {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), WC_Bank.class);
startActivityForResult(myIntent, 0);
}
};
}
Upvotes: 0
Views: 149
Reputation: 562
if you are extending ListActivity then there's a method which you must have to implement
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String list_item=l.getItemAtPosition(position).toString();
Intent i=new Intent(MainActivity.this,List1.class);
i.putExtra("list_item", list_item);
startActivity(i);
}
after getting string in list_item you can do whatever you want to do with it.
and if you are using extends activity.
ListView lv;
lv=(ListView)findviewbyid(R.id.listview);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String list_item=lv.getItemAtPosition(arg2).toString();
}
});
now its depend on you how you use that listview data.
Upvotes: 1
Reputation: 73721
as you can see you have an error on your @Override
which is probably saying there is no such method because your onListItemClick
is not correct
see the documentation
you need ListView,View,int,long
as your arguments
Upvotes: 0