I'm using an SQLite database to populate a listview. I've made it so that the user can not pass the data to the listview until a TextView has been populated.
Now I'm trying to get the TextView, which is defined in XML, to be populated by having the user click a button that should then display text on the TextView. However, I do not know how I can pass it to the TextView when it's not made programmatically.
So how can I pass the data (packageName) to TextView?
If relevant, my onItemClick looks like this:
myGrid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Putting package name into packageName
final String packageName = mApps.get(position).activityInfo.packageName;
ResolveInfo info = mApps.get(position);
info.activityInfo.loadLabel(getPackageManager());
Toast.makeText(getApplicationContext(), "Clicked " + packageName,
Toast.LENGTH_SHORT).show();
}
});
You would do need to declare your text view then put this inside the OnItemClick method.
myTextView.setText(packageName);
Related
I'm trying to get the value of an item that is inside a Spinner, and then I want to send it to a database.
However, the code:
String variable = spinner_name.getSelectedItem().toString() does not work at all
So it seems the best way is through of the method:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_LONG).show();
}
But how do you get the value of this variable called text, which is inside the above method, to insert into another method?
Create a CustomAdapter for your spinner, pass a callback as parameter when creating the customAdapter. Once you click on a item on spinner, because of callback, you'll have the item on your frag/activity.
From there you can call any method you want.
I want to toast Text of selected list item in ListView. I'm using Custom Adapter. I tried this code.
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,final int pos,
final long id) {
String text=String.valueOf(lv.getItemAtPosition(pos));
Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
}
But it's Toasting Object name like com.demo.CustomList#413ffb10. Can anyone tell me how to Toast respective item?
Note: The above code works for Simple Adapter,but not for Custom Adapter
Assuming that the list view has objects of the type CustomList as per your code it is displaying the default toString() method of your CutsomList class. You should instead access the variables that you would like to display and toast it.
For example:
public class CustomList {
String title;
public String getTitle(){
return name;
}
}
And in the onClick you can do the following:
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,final int pos,
final long id) {
CustomList customList= (CustomList )lv.getItemAtPosition(pos);
Toast.makeText(getActivity(), customList.getTitle(), Toast.LENGTH_SHORT).show();
}
This is exactly what you are looking for. Hope that helps.
I don't know the structure of your custom list view. But if you want to toast TextView value from your custom list view then do something like this :
String text= (TextView)view.findViewById(R.id.YOUR_LIST_ITEM_TEXT_VIEW).getText();
Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
Try this
String text=parent.getItemAtPosition(pos).toString();
Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
The getItemAtPosition() method returns an object belonging to the data structure the adapter works with, not an instance of the graphic widget
I have a ListView which is implemented using a customs adapter. For making the adapter i am using a holder class. The class has various TextViews and ImageViews as well as an Int variable id to store the id being fetched from the database. Now when i click the particular list i want to get the id so that using it I can further display the information on a new activity. The id is not meant to be displayed in the ListView. How can i get the id from onItemClickListener()
You can set a tag to a View (.setTag()) and then retrieve it, it happens on getView() method inside your custom Adapter
Here is a sample code:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String transactionId = ((TextView) view
.findViewById(R.id.tvTID)).getText().toString();
handler.getTransactionDetails(callback, transactionId);
}
});
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
YourObject selected = adapter.getItem(position);
Then you can send the selected list item (Object) to your new activity
Intent mIntent = new Intent(ThisActivity.this, NewActivity.class);
mIntent.putExtra("list_selected", selected);
startActivity(mIntent);
as well as an Int variable id to store the id being fetched from the
database.
You didn't provide more infomation about that id (i don't know exactly what it presents) but to make it simple you can set this id for each widget in ListAdapter via setTag() method and then simply retrieve it from your onItemClick() method.
I try to get the value of a selected Item within a custom adapter on a listview. I try this with following code:
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
View curr = parent.getChildAt((int) id);
TextView c = (TextView)curr.findViewById(R.id.tvPopUpItem);
String playerChanged = c.getText().toString();
Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();
}
At the beginning, if I click, the values are good, but once I scrolled and I click on another Item, I get the wrong value of that clicked item... Any idea what is causing this?
The parameter v is the current row. so use:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TextView c = (TextView) v.findViewById(R.id.tvPopUpItem);
String playerChanged = c.getText().toString();
Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();
}
(Or you could use getChildAt(position) but this would be slower.)
Understand you might be able to simplify this more depending on your layout.
Just Small Change is Required
TextView c = (TextView) v.findViewById(R.id.tvPopUpItem);
Since you are using custom view you need to pass the View argument in your OnItemClickListener then you need to use that value to get the Details of TextViews present in that
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i=new Intent(ListOfAstrologers.this,AstroProForUser.class);
i.putExtra("hello",adapter.getItem(position).getUsername() );
startActivity(i);
}
});
Here username is a string field declared in a class and you must over ride getItem() method in adapter class to get the details of inflated row when you click.
I dont have much experience but I think if you want to pass a variable from an OnClick (which is an anonimous class) to the outside of the onClick and to the other activity, you want to pass it via Intent intent.putExtra... (it's quite easy)
Otherwise you might end up using "public static" variable, which might be a memory leak...
I am new to android programming. I need some help here. I have used this site example on creating a listview. What I want to achieve is when the user clicks a particular row, the row clicked will perform its respective action. (Eg. When clicked row 1 will show a toast. When clicked row 2 will direct the user to another new view, etc.)
I have set a OnItemClickListener to the listview but am lost on how to do it. Any help will be appreciated. Thanks!
Below is my code:
.......
final ListView list = new ListView(this);
list.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(Adapter<?> arg0, View v, int i, long l){
// At implementation
}
});
.......
It looks like you're on the right track, everything should be clearer with onItemClick's parameter names :
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "Clicked row " + position,
Toast.LENGTH_SHORT).show();
}
So you actually get the position of the item clicked, and can have a different action for each item.
To get the index of the selected item:
int selected_item = myListView.getPositionForView(view);
or to get the string:
String chosen_item = (myListView.getItemAtPosition(selected_item).toString());
If your class is extending ListActivity replace "myListView" with "this".