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".
Related
I want to get the item on list view to edit Text in another activity.
When clicked on list view item, I want to transfer the item in another activity in edit Text.
You have to make onItemClickListner of listview like that.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
}
});
Then in the new Activity, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
And finally set Value to editText like this
editText.setText(value);
Hope this will help you.
lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getActivity(), NewActivity.class);
intent.putExtra("text", text want to transfer);
startActivity(intent);
}
});
You can make use of SharedPreferences. And when you pass the content of the ListView to the next activity, you can use editText.setText("Your Text").
You can also pass your data through intents from which you are calling your new activity.
create onClick method like this.
ListView list = (ListView) findViewById(R.id.newsList);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long offset) {
NewsItem item = (NewsItem) adapter.getItem(position);
Intent intent = new Intent(getApplicationContext(), NewsDetailsActivity.class);
intent.putExtra(KEY, item.getHeadline());
startActivity(intent);
}
});
In next activity
Intent intent = getIntent();
headline = intent.getStringExtra(KEY);
have a look here
I've an Activity containing a basic Listview. The functionality works as follows:
click a listview item -> get the listview item name -> pass the name with intent and start a new activity.
Everything is working except for the start of a new Activity. Its just not responsive and I cannot see why. I would really appreciate an extra pair of eyes to look over this.
listView = getListView();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, foodNames);
listView.setAdapter(adapter);
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parentView, View childView, int position, long id) {
TextView tv = (TextView) listView.getSelectedItem();
String value = tv.getText().toString();
Log.v("DEBUG","Name of item clicked" + value);
Intent intent = new Intent(childView.getContext(), FoodItemActivity.class);
intent.putExtra("FoodName", value );
startActivity(intent);
}
public void onNothingSelected(AdapterView parentView) {
}
});
I think you are missing line
startActivity(intent);
:-)
try it this way:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(view.getContext(), FoodItemActivity.class);
intent.putExtra("FoodName", value );
startActivity(intent);
}
});
this will solve your question, and if still problem arises, then do confirm that all the child controls are focusable=false and clickable=false.
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 been struggling using some tutorial for passing a listview option to a new activity and make it the title (I will do other stuff with it later). I have set up a OnClickListener by what is best to put inside it
ListView listView1 = (ListView) findViewById(R.id.sportslist);
String[] items = { "Archery", "Badminton", "Cricket", "Dodgeball", "Equestrian", "Football", "Golf", "Handball", "Ice Hockey", "Ju Jitsu", "Karate", "Lacrosse", "Mountain Biking", "Netball" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
?????
}
});
Thanks
EDIT:Extra code
final TextView changetitle = (TextView) findViewById(R.id.detailedsocietyname);
changetitle.setText(name);
For of all you will need to get the item that was selected:
final String selected = items[position];
Or as doctoror drive has suggested
final String selected = (String) parent.getSelectedItem();
Then you will need to pass that string as an extra to your new activity
Intent i = new Intent(getApplicationContext(), MyClass.class);
i.putExtra("name", selected);
startActivity (i);
And then finally in your next activity
Intent in = getIntent();
String name = in.getStringExtra(("name"));//gets name from intent
public void onItemClick(AdapterView parent, View view,int position, long id)
String str = items[position];
Intent in = new Intent(getApplicationContext(), NextClass.class);
in.putExtra("itemkey", str);
startActivity (in);
}
in public void onItemClick(AdapterView<?> parent, View view,int position, long id) add this code.
Intent i = new Intent(getApplicationContext(), NextClass.class);
i.putExtra("selectedItem", items[position]);
startActivity (i);
for getting value in NextClass Activitiy :
String SelectedItem = getIntent().getStringExtra("selectedItem");
Add the following to you Activity where you have listview
following is the variable that will contain the value that you want to pass to other activity
Declare it before Oncreate statement
// Activity_1
public final static String send_to_other_activity="ListViewSelected_ID";
Add the following code to the listView1.setOnItemClickListener
Intent i= new Intent(Recipe_List.this,Recipe_View.class);
i.putExtra(send_to_other_activity, string.valueof(position));
// itz (key-value) pair on the left key thru which u will access it on other place. on the right value that you want to pass
// Iam passing posiion to other activity here
startActivity(i);
Now on other activity extract this value from the key by adding following statement to the oonCreate of other activity
//Activity_2
getdata_from_list =getIntent().getStringExtra(Activity_1.send_to_other_activity);
now you have teh desired value in getdata_from_list
I have created a ListView where I have to add different items. Now, when I click on a particular item it displays another window. On that window, I want to display the name of that item which I click on the ListView.
My code:
private ListView contactList;
private String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople"};
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.contact_activity);
contactList=(ListView)findViewById(R.id.ListView01);
contactList.setAdapter(new ArrayAdapter<String (this,android.R.layout.simple_list_item_1 , lv_arr));
contactList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/*Intent myIntent = new Intent(view.getContext(), CallActivity.class);
startActivity(myIntent);*/
}
});
}
you have to get the name of the item on itemclick event. pass it to the activity which will be called. in in the calling activity get the name of item and display
contactList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String selecteditem = lv_arr[position];
Intent myIntent = new Intent(view.getContext(), CallActivity.class);
intent.putExtra("item", selecteditem);
startActivity(myIntent);
}
});
In CallActivity.java
write the following to get the selected item name
String selectedItem=getIntent().getStringExtra("item");
Instead of starting a new activity, use AlertDialog. You already have the position of the list item clicked. So displaying it on the dialog shouldn't be a problem if you follow the article linked.
EDIT :
As per your requirement, you have to launch a new activity to display a string
In the sending list activity
intent.putExtra(String key, String value)
In receiving activity,
String value = getIntent().getStringExtra(key);