Is listview has any attribute (e.g. productId, like hidden field of html form) can be set for later use? The following is to build a listview by using an array
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, catlist);
ListView listView = (ListView)findViewById(R.id.ListView01);
listView.setAdapter(adapter);
The listview will display the text inside of the catlist, i want to pass the productid which corresponding the category text to another activity when users click on every item.
listview.setOnItemClickListener(new OnItemClickListner()) {
}
You can do something like this:
You will have two arrays catlist and catIds, catlist will have name to show in ListView and catIds will have ids at same indices, now when the user click any item, you will get its position and grab the id from catIds at that position like this:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> pr, View v, int position, long id) {
String id = catIds.get(position);
//use this id for any purpose.
}
});
Related
I have CustomAdapter which I am using for populating ListView with some data.
Each element in ListView has two variables. For each listview (in onItemClick method) I must check this variables and If they are the same - do some code and If they are different - do another code, for example Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();
So I have tried this:
private List<SomeItem> items = new ArrayList();
//items were created
SomeAdapter adapter = new SomeAdapter(this, R.layout.list_item, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for(int i=0; i<=items.size(); i++) {
SomeItem item = items.get(position);
String tmpCI = item.getFirstVariable();
String tmpPCI = item.getecondVariable();
if (!tmpCI.equals(tmpPCI)) {
//some code
} else {
Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();
}
}
}
});
But all of my listview elements have values of the first element in those two variables.
So how can I do something like item.next(); for validating all of items in listview?
UPD:
Sorry, I will provide more information about what I am doing after checking variables of listview items for understanding my issue.
I have one more adapter:
SomeAnotherAdapter adapterPr = new SomeAnotherAdapter(this, R.layout.list_tem_another, itemsAnother);
and one more listview:
listViewAnother.setAdapter(adapterPr);
First of all I understood, that first variable should be from first listview and the second variable from another listview.
In this listViewAnother I have many items, which has some "id". For example 1st, 5th and 20th elements have id 90 and other elements have id 100.
We can say, that items from the first listview also have "id".
So I must check if(first variable = second variable) and then show in listViewAnother only items that have id which equals ID from clicked item in listView.
I tried: adapterPr.remove(item2); but then I understood, that I need all of items because I can go back to listView and press another item which will need those removed elements.
Now, hope I provided full information and you will be able to help me improve my code.
Do you need to perform the check on every element of the adapter when you click on one element of the adapter? If not, you don't need a loop. If you do, your loop should be iterating over the original list, and does not need adapter position at all.
In general when using adapters and lists, you should use the adapter's position and the adapter's data set to perform any tasks. It's not good practice to use the adapter position to get an item from the original list.
Simply set one onItemClickListener which gets the corresponding item from the adapter, and do what you need to from there:
private List<SomeItem> items = new ArrayList();
//items were created
SomeAdapter adapter = new SomeAdapter(this, R.layout.list_item, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SomeItem item = adapter.getItem(position);
String tmpCI = item.getFirstVariable();
String tmpPCI = item.getecondVariable();
if (!tmpCI.equals(tmpPCI)) {
//some code
} else {
Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();
}
}
});
I have a listview binded to an arrayadapter of strings... How do i set different clicklistener on each item in the list view
Register an item click listener on your ListView with http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)
In your AdapterView.OnItemClickListener#onItemClick you will get the view and an id.
You can use the id if you have a given order in your arraylist, or you could use View#findViewById and get the content of the view to figure out which item it is.
That means, you would not set a listener per item, rather you would have one listener for all items, and then do different things based on which item it was.
I think you could use it like this.
adapter = new ArrayAdapter(getContext(), R.layout.list_item_forecast, R.id.list_item_forecast_textView, new ArrayList<String>());
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//what ever you want to give on item click
}
});
here by checking the position inside onItemClick you can assign specific tasks.
i have a multi column listview that has been populated with json data. now on clicking a list item i need the datas of that particular list item to be displayed in the textView.
In onItemClickListener of listview get the item from list which is set to the listview adapter using position variable of onItemClickListener method. Then set it to the textview.
Something like this:
items = getListView();
items.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Display content in Textview
}
});
I have Listview which is showing different images of animals,birds,reptiles. The list view is working fine. Now I want when user click to any picture it should appear in the ImageView.The ImageView is just below the listview. so when ever user will click any image in the listview it should appear in the Imageview.
Also there is a button. Now I want to achieve that when user select any image and press Ok button that paticular image should show on the image view of other activity also. I Know I can send the Id through intent.putextra(), but the problem is first place how to get the id of particular picture.
Source code
public class MainActivity2 extends ActionBarActivity {
private TypedArray ListIcons;
private HorizontalListView listView;
private ArrayList<AnimalsListItems> SuitItems;
private AnimalsListAdapter adapter1 = null;
/** An array of strings to populate dropdown list */
String[] actions = new String[] {
"Bookmark",
"Subscribe",
"Share"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
listView = (HorizontalListView) findViewById(R.id.suits_list);
AnimalsItems = new ArrayList<AnimalsListItems>();
/** Create an array adapter to populate dropdownlist */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, actions);
/** Enabling dropdown list navigation for the action bar */
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setDisplayShowTitleEnabled(false)
;
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(" "+" "+" ");
/** Defining Navigation listener */
android.support.v7.app.ActionBar.OnNavigationListener navigationListener = new android.support.v7.app.ActionBar.OnNavigationListener() {
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Toast.makeText(getBaseContext(), "You selected : " + actions[itemPosition], Toast.LENGTH_SHORT).show();
ListIcons = getResources()
.obtainTypedArray(R.array.ic_launcher);// load icons from
// strings.xml
for (int i = 0; i<=ListIcons.length(); i++) {
AnimalsItems.add(new AnimalsListItems(ListIcons.getResourceId(i,-1)));
}
adapter1 = new SuitsListAdapter(getApplicationContext(),SuitItems);
Log.v(adapter1.getCount()+"");
listView.setAdapter(adapter1);
return false;
}
};
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//With the position parameter, you can fetch the item at the clicked position like below. Cast it to whatever type your ListView is.
Object yourItem = (Object) listView.getItemAtPosition(position);
//Now you can assign the image to an imageview
}
});
/** Setting dropdown items and item navigation listener for the actionbar */
getSupportActionBar().setListNavigationCallbacks(adapter, navigationListener);
this.overridePendingTransition(R.anim.anim_slide_in_left,
R.anim.anim_slide_out_left);
}
}
For your ListView, you can set an onItemClickListener as below
ListView listView1 = (ListView) findViewById(R.id.listView1);
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//With the position parameter, you can fetch the item at the clicked position like below.
AnimalsListItems yourItem = (AnimalsListItems) listView1.getItemAtPosition(position);
//Now you can assign the image to an imageview
}
}
Not sure what you mean by "id", if you're talking about drawable id's, path to image files, or just any way to identify your image. It depends a lot on how you populate your list. You didn't supply any code at all.
But since you have a ListView I suppose you have an Adapter, and that you're backing that adapter with some data, such as an array, database etc. When you click on an item, you get an onItemClick callback with the index of the item you clicked on. You should be able to use this index to find which image you clicked on from your data source.
Then when it comes to passing that to another activity, well, again it depends on how you represent your images but you should be able to pass whatever data you need to represent the image in an intent extra to the second activity.
You can use the setOnItemClickListener method of ListView to get witch item clicked.Because one item one image, you can get the target image.
1.Step
Rather getting resource id you can create one integer array containing resource id's like,
int resourceID[] = [R.drawable.image1, R.drawable.image2, ...]
Assign this array to your ListView image item
2.Step
Now can easily get the postion of selected list item or image using Lists on item click method
listView.setOnItemClickListener(new OnItemClickListener() {} )
3.Step
Just pass your ResourceID array & tapped position to next/target activity & use it, rather passing resourceID
or
Otherwise make your ResourceID as static, pass only tapped position to next Activity & use this static ResourceID from previous Activity
i.e -> yourImageView.setImageResource(FirstActivity.ResourceID[position])
I have an array of Strings I'm populating a Spinner object with. However, I'd like to attach an ID to each element of the Spinner, so when the user selects an item, I have its ID to use to save to some other piece of data. How can I do this?
Create a class StringWithTag and use in place of the string name in the list like so :-
public class StringWithTag {
public String string;
public Object tag;
public StringWithTag(String stringPart, Object tagPart) {
string = stringPart;
tag = tagPart;
}
#Override
public String toString() {
return string;
}
}
in the add items to spinner part :-
List<StringWithTag> list = new ArrayList<StringWithTag>();
list.add(new StringWithTag("Oldman", "12345"));
list.add(new StringWithTag("Umpire", "987654"));
list.add(new StringWithTag("Squad", "ABCDEE"));
ArrayAdapter<StringWithTag> adap = new ArrayAdapter<StringWithTag> (this, android.R.layout.simple_spinner_item, list);
....
....
in the listener :-
public void onItemSelected(AdapterView<?> parant, View v, int pos, long id) {
StringWithTag s = (StringWithTag) parant.getItemAtPosition(pos);
Object tag = s.tag;
}
voila!
}
What do you mean by id. You can use ArrayAdapter to populate the Spinner. When item is selected just get the element from the adapter and save the data you want.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<MyObject> adapter = ... // initialize the adapter
adapter.setDropDownViewResource(android.R.layout.some_view);
spinner.setAdapter(adapter);
and when item is selected
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
MyObject selected = parent.getItemAtPosition(pos);
// save any data relevant with selected item
}
If you are storing your data in db you can use CursorAdapter and in onItemSelected to fetch the selected item id from the cursor.
I don't think you can attach an arbitrary ID to elements of a text array resource, if that's what you're using.
I think the simplest way to attach such an ID would be to either hard-code (if you're using a static text resource) or dynamically build (if you get the strings at runtime) a mapping from (String position in array)->(primary key).
EDIT: On the other hand, Mojo Risin has a point - you should check to see if the CursorAdapter API already does what you need for you.
Andrew Hi, it's been a long time but it's worth to write.
You can set a tag for each row when you'r inflating spinnerLayout in SpinnerAdapter:
spinnerView = inflater.inflate(spinnerLayout, parent, false);
spinnerView.setTag("Your Tag");
And then you can get the tag with:
yourSpinner.getSelectedView().getTag();
I think The best solution is to add one more spinner and fill it with the ids but make the visibility of it to gone