I have a ListView implementing onItemClickListener and on its first click, I am highlighting the list item and adding it to a arrayList (Which I am sending it to next activity). What I want is to remove the ListItem from the arraylist when I click on the listitem second time, That is when deselecting the list item. This is my code that I implemented for adding. Please guide how can I remove elements.
`listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String present =listView.getItemAtPosition(position).toString();
presentStudent.add(present);
}
});`
You need to check if the list contains the object, add it if not and remove it if it's in.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String present = listView.getItemAtPosition(position).toString();
if (presentStudent.contains(present)) {
presentStudent.remove(present);
} else {
presentStudent.add(present);
}
}
});
Related
Hello every one i have a listview of array data of checkboxes which the user will select as the hobbies below is preview
and the hobbies will be shown upside but i am stuck of removing the hobbies by clicking it on up side and also when i select the 2 hobies then in show list it shows 2 times
bellow is the preview
also if i select the 3rd hobby then it show 3 times so please fix this for me and also if i uncheck the box it should remove from the view list also if i click on any hobby in the show list it still remove and uncheck automatically and if i click on save button all the hobbies should be selected
please fix this for me bellow is my code
Hobbies_Hobbies_List.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (!Hobbies_Hobbies_List.isItemChecked(i))
{
Hobbies_Hobbies_List.setItemChecked(i,false);
}
else
{
Object object = Hobbies_Hobbies_List.getItemAtPosition(i);
String Hobby = String.valueOf(object);
for (int j=0;j<Hobbies_Hobbies_List.getCount();j++)
{
if (Hobbies_Hobbies_List.isItemChecked(j))
{
TextView text = new TextView(HobbiesActivity.this);
text.setText(Hobby+" ");
text.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Hobbies_Show.addView(text);
}
}
}
}
});
hobbies_save_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
It seems you don't have an adapter.
In an adapter you can modify your list (add or remove item, change text, color etc)
If you want to make your own list you will have to make your own adapter for your list. You can remove items in the adapter.
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View rowView = inflater.inflate(resource, parent, false);
TextView textView = rowView.findViewById(R.id.textView);
CheckBox checkBox = rowView.findViewById(R.id.checkBox);
textView.setText(dataSet.get(position).myText);
checkBox.setChecked(dataSet.get(position).myBool);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
// You can remove item here
}
});
return rowView;
}
For this you need your own data type.
Here a project with an example:
https://github.com/Asuki/SimpleListView
You also have to add the remove items to the other one list (maybe from the adapter)
I got short question. I got ListView that displays my objects from ArrayList. I want to have Toast that can display one of the values from my Object when I click on the item on a list (for instance when my object is called Product i want to have displayed the name of this product). My code looks like this below and I don't know where to put getProductName() or something.
final ArrayList<Product> productList = new ArrayList<>();
for(Product p: md.takeAllProducts()){
productList.add(p);
}
ProductListAdapter adapter = new ProductListAdapter(this,R.layout.adapter_view_layout,productList);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ProductsList.this, mListView.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();
You can use the parameters passed to onItemClick() to accomplish this:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Product p = (Product) parent.getAdapter().getItem(position);
Toast.makeText(parent.getContext(), p.toString(), Toast.LENGTH_SHORT).show();
}
Of course, you can replace p.toString() with whatever methods you have available in the Product class, like p.getName() etc.
I have to remove the single item from list view in android while clicking long click. Please let me suitable code for removing a item in list view?
Here is the my code
this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto - generated method stub
}
}
Use remove() method in your ArrayAdapter.
yourarrayAdapter.remove(yourarrayAdapter.getItem([POSITION]));
OR
yourarrayList.remove([POSITION]);
yourarrayAdapter.notifyDataSetChanged();
Try this in your click callback, to remove the view from its parent:
((ViewManager) parent).removeView(view);
I have a problem. In my activity i have a ListView whose values are compiled from a Database. The database returns three columns: _Id, AccountName and Comments. What i want is that the when the user clicks on one of the items in the listView, that Item's position will be extracted and then that position will be compared with the postion in the ArrayList which has the data and then when the specific field and row is found out, the _Id field will be taken out and passed to another activity in the form of a String.
You may not have understood much; this might help:
public void search(View view){
final ListView vLst_IDCommName=(ListView)findViewById(R.id.lst_IDCommName);
EditText vTxt_searchTxt=(EditText)findViewById(R.id.search_txt);
String srch_str= vTxt_searchTxt.getText().toString();
if(srch_str.length()>0){
//The DataSource for the LstView
List<Comment> values = datasource.getContacts(srch_str);
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this, android.R.layout.simple_list_item_1, values);
vLst_IDCommName.setAdapter(adapter);
}
else{
Toast.makeText(getBaseContext(), "Please type in a value to proceed!", Toast.LENGTH_LONG).show();
}
vLst_IDCommName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Here I want to create a function that will take the position of the item clicked
//and then searched for that position in the ArrayList and then return the _Id of
//the account whose value was clicked.
}});
}
This code I used to get data:
public List<Comment> getContacts(String search_str) {
List<Comment> comments = new ArrayList<Comment>();
String srch_str=(String) search_str;
Cursor cur_comments= database.rawQuery("Select * from comments where acc_name like('%"+srch_str+"%')"+" "+ "or comment like('%"+srch_str+"%')", null);
cur_comments.moveToFirst();
while (!cur_comments.isAfterLast()) {
Comment comment = cursorToComment(cur_comments);
comments.add(comment); cur_comments.moveToNext();
}
cur_comments.close();
return comments;
}
Any feedback would be appreciated.
you can try
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String currentId=yourArrayList.get(position).toString();
}
vLst_IDCommName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Object s=parent.getItemAtPosition(position);
String j=s.toString();
}});
How can I get a grid-view's item tag by its position and have to get from another grid-view's item click listener function.
gridview2.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id)
{
int i=(Integer)gridview1.getChildAt(gridview1s_position).getTag();---> it is returning Null
}
});
MY gridview1.setOnitemClicklistener and gridview2.setonitemclicklistener are in the same file (MainActivity.java)
But the gridview1 adapter is a ImageAdapter.java(this is for setting the images in the gridview1) and gridview2 adapter is imageadapter2.java(this is for setting black images so that the gridview2 looks like a box with rows and columns, otherwise it is showing like a Bold line because we didn't put anything in the gridview)
gridview1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,int position, long id)
{
int tag=(Integer)gridview1.getChildAt(position).getTag();//this returning the tag
}
});
ImageAdapter1.java
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
if(arg1!=null)
{
imv=(ImageView)arg1;
}
else
{
imv=new ImageView(cont);
imv.setLayoutParams(new GridView.LayoutParams(40,40));
imv.setScaleType(ScaleType.CENTER_CROP);
imv.setPadding(0,0,0,0);
}
imv.setImageResource(imageid[arg0]);//imageid aray is having drawable images
imv.setTag(imageid[arg0]);
return imv;
}
gridview2
gridview2.setOnItemClickListener(new OnItemClickListener()
{
ImageAdapter ia=new ImageAdapter(getApplicationContext());
public void onItemClick(AdapterView<?> parent, View v,int position, long id)
{
ImageView imv;
if(v!=null)
{
imv=(ImageView)v;
}
else
{
imv=new ImageView(getApplicationContext());
imv.setLayoutParams(new GridView.LayoutParams(40,40));
imv.setScaleType(ScaleType.CENTER_CROP);
imv.setPadding(0,0,0,0);
imv.setBackgroundColor(5555);
}
imv.setImageResource(ia.imageid[tt]);
}
});
using getChildAt might be too cumbersome for something like this because it only useful for making references to views that are in view and re-orders its indexes to match this. I don't know enough about where your other GridView is positioned to pursue this avenue further.
But regardless, the way i'd do something like this is to make an intermediary collection between the two adapters. as in one adapter holds all the tags in an indexed collection and then you could make some sort of reader get method. at the very least, this way of doing it would put my mind at ease.
gridview2.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
int i = gridview1.getTagFromPosition(position);
}
});
then in the gridView1 adapter something like:
private int[] tagCollection;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// .....
convertView.setTag(tagCollection[position]);
return convertView;
}
public int getTagFromPosition(int position) {
return tagCollection[position];
}
i at least hope the idea comes across so that you can modify it for however is practical for how your objects are getting modified and accessed.
Finally myself i got the solution for my question.Thank you for all who gave their valuable suggestions.
i saved the tag of a particullar cell of grid-view1 in a variable and used that variable in the grid-view2.