Android ListView trouble with OnItemClick Listener - android

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);

Related

I would like the listview item to display the data of the relevant item in new activity

Currently, the code i have only displays the data (on a new activity when clicked) at index 0 of that list view - not the item i actually clicked. i would like to somehow change my code so it can display the data at index n (n being the item i clicked in list view):
ResultsActivity.java
final ArrayList<String> searchResults = getValuesFromJSON(jsonResult);
final ListView listView = (ListView) findViewById(R.id.listView);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, searchResults);
listView.setAdapter(arrayAdapter);
//list items become clickable and open the movie detail page which displays the movie data of the specific movie
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent newActivity = new Intent(ResultsActivity.this, MoviePage.class);
newActivity.putStringArrayListExtra("movie data", searchResults);
startActivity(newActivity);
}
});
}
MoviePage.java
ArrayList<String> searchResults = getIntent().getStringArrayListExtra("movie data");
TextView t = (TextView)findViewById(R.id.textViewfinal);
t.setText(searchResults.get(9));
Just set a onclick listener on your listview so you can listen for item click events.
Put this in your onCreate method in your activity:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
//you can move the textview to a class variable if you want
TextView t = (TextView)findViewById(R.id.textViewfinal);
t.setText(searchResults.get(position));
}
});
update: you'll want to pass the position to the next activity the same way your passing the searchResults to MoviePage so just do this:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent newActivity = new Intent(ResultsActivity.this, MoviePage.class);
newActivity.putStringArrayListExtra("movie data", searchResults);
newActivity.putExtra("position",position);//pass the position to the next activity
startActivity(newActivity);
}
});
now in MoviePage.java
ArrayList<String> searchResults = getIntent().getStringArrayListExtra("movie data");
int position = getIntent().getIntExtra("position",0);
TextView t = (TextView)findViewById(R.id.textViewfinal);
t.setText(searchResults.get(position));
Try this :
ResultsActivity.java
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent newActivity = new Intent(ResultsActivity.this, MoviePage.class);
newActivity.putExtra("movie data", searchResults.get(position));
startActivity(newActivity);
}
});
MoviePage.java
Bundle extras = getIntent().getExtras();
if (extras != null) {
TextView t = (TextView)findViewById(R.id.textViewfinal);
t.setText(extras.getString("movie data"));
}

On Item Click Listener for List View extending ArrayAdapter

What I have tried:
public class EntryAdapter extends ArrayAdapter<Item> {
private Context context;
private ArrayList<Item> items;
private LayoutInflater vi;
public EntryAdapter(Context context,ArrayList<Item> items) {
super(context,0, items);
this.context = context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, final View convertView, ViewGroup parent) {
// // // // NON-FUNCTIONING CODE BELOW
AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getContext(), NewsItemActivity.class);
convertView.getContext().startActivity(intent);
}
};
}
The AdapterView.onItemClickListener doesnt yield any errors but doesnt seem to function whatsoever.
What is the proper way of setting this onClick Listener?
Note: I have to set it in this adapter class, not the main class for my own reasons.
You shuldn't implement a listener inside the getView() unless you what set a listener on a particular view inside your row layout.
You should instead use setOnItemClickListener() method on your ListView:
ListView lv = (ListView) findViewById(R.id.your_listview);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(context, NewsItemActivity.class);
context.startActivity(intent);
}
});
EDIT:
If, for each action inside the onclick, you need information that resides in your Objects (Item) then you can get it in this way:
Item item = (Item)listview.getAdapter().getItem(position);
from inside of onItemClick() method
You can use:
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Make what you want
}
});
Or depending on your item view, you can make the OnCLickListener on your global layout of the item or a specific item's layout
Try by this code :-
Spinner cb_tv;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter;
list.add(phone1);
list.add(name1);
dataAdapter = new ArrayAdapter<String>(Display_Tank.this,android.R.layout.simple_spinner_item, list);
cb_tv.setAdapter(dataAdapter);
cb_tv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();
}
});
at the place of cb_tv Spinner You can use ListView
First of all, the best way to achieve this is to use your own ViewHolders in ListView.
This is an example of a custom ViewHolder
class MyViewHolder extends ViewHolder implements View.OnItemClickListener {
public MyViewHolder(View view) {
/* Implement your views here
* like this: mTextView = (TextView) view.findViewById(R.id.myId);
*/
view.setOnItemClickListener(this);
}
#Override
public void onClick(View view) {
Intent intent = new Intent(getContext(), NewsItemActivity.class);
convertView.getContext().startActivity(intent);
}
}
And on your getView method you create this ViewHolder and populate your Views with your data.
You can try this way... it is working in my case Change your context to
Activity activity = (Activity) context;
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(activity, NewsItemActivity.class);
activity.startActivity(intent);
}
});

how to move from listview item to other layout in android

how to move from listview item to other layout in android .......
am trying to move from listview to other layout by click on it help me plz
public class ListViewActivity extends Activity {
private ListView lv1;
private String lv_arr[] = {"Android", "iPhone", "BlackBerry", "Nokia"};
#Override
public void onCreate(Bundle x)
{
super.onCreate(x);
setContentView(R.layout.activity_main);
lv1=(ListView)findViewById(R.id.ListView01);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
//lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new OnItemClickListener() {
}
}
}
Use onItemClickListener for this.
It's simple. if you search around you will get this.
List.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getApplicationContext(),SecondClass.class);
intent.putExtra("passsomething","value_of_variable");
startActivity(intent);
}
});
Try the below
lv1.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
Intent i = new Intent(ActivityName.this,SecondActivity.class);
// if you just want to navigate to second activity remove the below lines
//String value = (String) arg0.getItemAtPosition(position);
//i.putExtra("key",value);
startActivity(i);
});
In SecondActivity
Have a textview if you want to display item clicked in listview in second activity else ignore the below
Bundle extras = getIntent().getExtras();
if(extras!=null)
{
String value = extras.getString("key");
// textview.setText(value);
}

How to handle the click event in Listview in android?

The below is my testing code to create the list view, the list view display successfully, however, there is error in click event. I would like to create an intent to send a hardcode message to an new activity. However, it show error for the line
Intent intent = new Intent(context, SendMessage.class);
So , the problem is , what should I provide for this class?
Also , instead of hard code the output message, how to capture the data in list view row and pass to the new activity? e.g. BBB,AAA,R.drawable.tab1_hdpi for the first row.
Thanks.
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.ListViewTest.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<ListEntry> members = new ArrayList<ListEntry>();
members.add(new ListEntry("BBB","AAA",R.drawable.tab1_hdpi));
members.add(new ListEntry("ccc","ddd",R.drawable.tab2_hdpi));
members.add(new ListEntry("assa","cxv",R.drawable.tab3_hdpi));
members.add(new ListEntry("BcxsadvBB","AcxdxvAA"));
members.add(new ListEntry("BcxvadsBB","AcxzvAA"));
members.add(new ListEntry("BcxvBB","AcxvAA"));
members.add(new ListEntry("BvBB","AcxsvAA"));
members.add(new ListEntry("BcxvBB","AcxsvzAA"));
members.add(new ListEntry("Bcxadv","AcsxvAA"));
members.add(new ListEntry("BcxcxB","AcxsvAA"));
ListView lv = (ListView)findViewById(R.id.listView1);
Log.i("testTag","before start adapter");
StringArrayAdapter ad = new StringArrayAdapter (members,this);
Log.i("testTag","after start adapter");
Log.i("testTag","set adapter");
lv.setAdapter(ad);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(context, SendMessage.class);
String message = "abc";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
}
I can not see where do you declare context. For the purpose of the intent creation you can use MainActivity.this
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(MainActivity.this, SendMessage.class);
String message = "abc";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
To retrieve the object upon you have clicked you can use the AdapterView:
ListEntry entry = (ListEntry) parent.getItemAtPosition(position);
ListView has the Item click listener callback. You should set the onItemClickListener in the ListView. Callback contains AdapterView and position as parameter. Which can give you the ListEntry.
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
ListEntry entry= (ListEntry) parent.getAdapter().getItem(position);
Intent intent = new Intent(MainActivity.this, SendMessage.class);
String message = entry.getMessage();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
Error is coming in your code from this statement as you said
Intent intent = new Intent(context, SendMessage.class);
This is due to you are providing context of OnItemClickListener anonymous class into the Intent constructor but according to constructor of Intent
android.content.Intent.Intent(Context packageContext, Class<?> cls)
You have to provide context of you activity in which you are using intent that is the MainActivity class context. so your statement which is giving error will be converted to
Intent intent = new Intent(MainActivity.this, SendMessage.class);
Also for sending your message from this MainActivity to SendMessage class please see below code
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
ListEntry entry= (ListEntry) parent.getAdapter().getItem(position);
Intent intent = new Intent(MainActivity.this, SendMessage.class);
intent.putExtra(EXTRA_MESSAGE, entry.getMessage());
startActivity(intent);
}
});
Please let me know if this helps you
EDIT:-
If you are finding some issue to get the value of list do one thing declear your array list
ArrayList<ListEntry> members = new ArrayList<ListEntry>();
globally i.e. before oncreate and change your listener as below
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(MainActivity.this, SendMessage.class);
intent.putExtra(EXTRA_MESSAGE, members.get(position));
startActivity(intent);
}
});
So your whole code will look as
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.ListViewTest.MESSAGE";
ArrayList<ListEntry> members = new ArrayList<ListEntry>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
members.add(new ListEntry("BBB","AAA",R.drawable.tab1_hdpi));
members.add(new ListEntry("ccc","ddd",R.drawable.tab2_hdpi));
members.add(new ListEntry("assa","cxv",R.drawable.tab3_hdpi));
members.add(new ListEntry("BcxsadvBB","AcxdxvAA"));
members.add(new ListEntry("BcxvadsBB","AcxzvAA"));
members.add(new ListEntry("BcxvBB","AcxvAA"));
members.add(new ListEntry("BvBB","AcxsvAA"));
members.add(new ListEntry("BcxvBB","AcxsvzAA"));
members.add(new ListEntry("Bcxadv","AcsxvAA"));
members.add(new ListEntry("BcxcxB","AcxsvAA"));
ListView lv = (ListView)findViewById(R.id.listView1);
Log.i("testTag","before start adapter");
StringArrayAdapter ad = new StringArrayAdapter (members,this);
Log.i("testTag","after start adapter");
Log.i("testTag","set adapter");
lv.setAdapter(ad);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(MainActivity.this, SendMessage.class);
intent.putExtra(EXTRA_MESSAGE, members.get(position).getMessage());
startActivity(intent);
}
});
}
Where getMessage() will be a getter method specified in your ListEntry class which you are using to get message which was previously set.
First, the class must implements the click listenener :
implements OnItemClickListener
Then set a listener to the ListView
yourList.setOnItemclickListener(this);
And finally, create the clic method:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(MainActivity.this, "You Clicked at ",
Toast.LENGTH_SHORT).show();
}
//get main activity
final Activity main_activity=getActivity();
//list view click listener
final ListView listView = (ListView) inflatedView.findViewById(R.id.listView_id);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String stringText;
//in normal case
stringText= ((TextView)view).getText().toString();
//in case if listview has separate item layout
TextView textview=(TextView)view.findViewById(R.id.textview_id_of_listview_Item);
stringText=textview.getText().toString();
//show selected
Toast.makeText(main_activity, stringText, Toast.LENGTH_LONG).show();
}
});
//populate listview
According to my test,
implements OnItemClickListener -> works.
setOnItemClickListener -> works.
ListView is clickable by default (API 19)
The important thing is, "click" only works to TextView (if you choose simple_list_item_1.xml as item). That means if you provide text data for the ListView, "click" works when you click on text area. Click on empty area does not trigger "click event".

How to perform intent function in selected row of Listview

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...

Categories

Resources