Android - Removing Item from GridView/Adapter - android

I have an GridView filled by an Adapter
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
The dataset of the Adapter is basicly a shuffled List
static List<Integer> mTlist = Arrays.asList(
R.drawable.ac_pre, R.drawable.acc_pre,
R.drawable.acd_pre, R.drawable.ach_pre,
R.drawable.acs_pre, R.drawable.ad_pre,
R.drawable.ah_pre, R.drawable.as_pre,
R.drawable.dc_pre, R.drawable.dd_pre,
R.drawable.dh_pre, R.drawable.ds_pre,
R.drawable.fc_pre, R.drawable.fd_pre,
R.drawable.fh_pre, R.drawable.fs_pre,
R.drawable.jc_pre, R.drawable.jd_pre,
R.drawable.jh_pre, R.drawable.js_pre,
R.drawable.kc_pre, R.drawable.kd_pre,
R.drawable.kh_pre, R.drawable.ks_pre,
R.drawable.nc_pre, R.drawable.nd_pre,
R.drawable.nh_pre, R.drawable.ns_pre,
R.drawable.qc_pre, R.drawable.qd_pre,
R.drawable.qh_pre, R.drawable.qs_pre,
R.drawable.sec_pre, R.drawable.sed_pre,
R.drawable.seh_pre, R.drawable.ses_pre,
R.drawable.sic_pre, R.drawable.sid_pre,
R.drawable.sih_pre, R.drawable.sis_pre,
R.drawable.vc_pre, R.drawable.vd_pre,
R.drawable.vh_pre, R.drawable.vs_pre,
R.drawable.xc_pre, R.drawable.xd_pre,
R.drawable.xh_pre, R.drawable.xs_pre,
R.drawable.zc_pre, R.drawable.zd_pre,
R.drawable.zh_pre, R.drawable.zs_pre
);
private void mischen() {
Collections.shuffle(mTlist);
}
from an diffrent Activity after pressing a button i want to remove an item
ImageAdapter imad = new ImageAdapter(getBaseContext());
Bundle extras = getIntent().getExtras();
Integer pos = extras.getInt("draw");
ImageAdapter.mTlist.remove(pos);
imad.notifyDataSetChanged();
finish();
pos is the position of the item in the List (mTlist); with finish() i go back to the GridView-Activity.
But the problem is, nothing changes at all! I checked with a toast the item on the position after the removing and it was the same as before.
Please help!
Greetings ueen

You're asking the list to delete an object of type Integer and that's not correct 'cause you use ArrayList. Try the following to delete your object
ImageAdapter.mTlist.remove(pos.intValue());
Regards.

Related

Click on first item in a GridView is not working

I have an activity which contains a GridView, each element of the GridView contains an ImageView and when the activity starts I want to perform a click on the first item's ImageView. I have tried several things which did not work:
gridView.performItemClick(gridView.getChildAt(0), 0, gridView.getAdapter().getItemId(0));
and with a performClick() directly.
However I think a found a way to do it by calling a function (performEmptyClick()) inside my custom GridViewAdapter which performs a click on the first item's ImageView.
My problem is that performEmptyClick() is called before the adapter's getView() is called (from debugging) and therefore the onClickListener() of the ImageView is not set up yet. Here is the code:
gridView = (GridView) findViewById(R.id.folderGridView);
folderViewAdapter = new FolderViewAdapter(this, R.layout.folder_item_layout, folderItems);
gridView.setAdapter(folderViewAdapter);
Bundle extras = getIntent().getExtras();
noFolder = extras.getBoolean("noFolder");
if (noFolder) {
folderViewAdapter.performEmptyClick();
}
Can anyone help me fix this?
Try
gridView = (GridView) findViewById(R.id.folderGridView);
folderViewAdapter = new FolderViewAdapter(this,
R.layout.folder_item_layout, folderItems);
gridView.setAdapter(folderViewAdapter);
Bundle extras = getIntent().getExtras();
noFolder = extras.getBoolean("noFolder");
if (noFolder) {
gridView.getChildAt(0).performClick();
}

Get the text of listview Android

I have listview that populated from database.
When I click a row in that listview, it will pass data to another activity with the content of activity depend on the passing data.
This is my code:
private final ArrayList<String> results = new ArrayList<>();
private final AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(ContentPick.this, Content.class);
String text = results.get(position);
i.putExtra(ID_EXTRA, String.valueOf(text));
startActivity(i);
}
};
The activity contain listview also have edittext that have filtering (when I type some text in the edittext it will filter the listview according to the text). The problem is, if I'm click a row in listview after I filtered it, the activity that showing is same result as if I'm not filter the listview.
Example:
When the edittext is empty, the first row of the listview is Cell structure . When I click the Cell structure it will show information about Cell structure.
I click Back, and I type Blood in EditText, it will filter the listview, and the first row of the listview is Blood Cell. When I click the row Blood Cell, it still show information about Cell Structure.
What I'm trying to do is with this code:
String text = results.get(position);
But I think it's the cause of the problem.
Please help me to solve this problem. Thank you very much.
change position in your code.like below
final int position
Use mAdapter.notifyDataSetChanged() to actual notify your dataset position. It might help you to solve problem.
After some trial and error I finally solve the problem.
I make a method displayResult and I set the adapter like this
dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, results);
In the method AdapterView.OnItemClickListener, I change variable text to this
String text = dataAdapter.getItem(position);
And it works!

How to refresh GridView without moving it?

public void UpdateData(ArrayList<HashMap<String,String>> array_list){
GridView glist = (GridView) findViewById(R.id.tipss_grid)
adapter2 =new CurrentAdapter(CurrentChanels.this,array_list);
glist.setAdapter(adapter2);
}
I call this method to populate data in gridview. I m displaying currently running programs. After every 1 min I call this method to refresh the data. The problem is that when user is on the last element of gridview and mean while I refresh it then control move to top of the screen. I do not want the screen to move,it must stay where it is before refresh. Any suggestions?
This is because every one minute you are creating a new Adapter and assigning it to the GridView.
Implement a new method resetData() in CurrentAdapter:
public void resetData(List<HashMap<String,String>> list) {
_list.clear();
_list.addAll(list);
notifyDataSetChanged();
}
Call resetData() whenever you want to refresh the grid:
GridView glist = (GridView) findViewById(R.id.tipss_grid);
if (glist.getAdapter() == null) {
CurrentAdapter adapter2 = new CurrentAdapter(CurrentChanels.this,
array_list);
glist.setAdapter(adapter2);
} else {
CurrentAdapter adapter2 = ((CurrentAdapter)glist.getAdapter());
adapter2.resetData(array_list);
}
use this line after setadapter line
glist.setSelection(adapter2.getCount() - 1) ;
You can call adapter.notifyDataSetChanged() but if you creating new ArrayList you have to set new adapter. You should change data in old ArrayList if you want notifyDataSetChanged() work.

List view back on first position after updating from custom adapter

List view is refreshed every N minutes of interval using a web service call. After refresh my current position goes back to the first item in list view.
Here is my code.
private void updateListUI(String response){
arrList = new ArrayList<Object>(); // to store array list
Object obj;
obj = some data; // some parsing of data and store in obj instance
arrList.add(obj);
if(listview.getAdapter()==null){
adapter = new customAdapter(myActivity,layout,arrList);
listview.setAdapter(adapter);
}else{
HeaderViewListAdapter adapterwrap =
(HeaderViewListAdapter) listview.getAdapter();
BaseAdapter basadapter = (BaseAdapter) adapterwrap.getWrappedAdapter();
customAdapter ad = (customAdapter)basadapter;
ad.setData(arrList); // it is a func in custom adapter that add items
ad.notifyDataSetChanged();
// here i want to set the position of list view,as it goes back to first item
}
}
BroadcastReceiver serviceReceiver = new BroadcastReceiver() {
public void onReceive( final Context context, final Intent intent ) {
updateListUI(intent.getStringExtra(response_from_service));
}
}
// Intent service calls webservice and broadcast response to receiver as finishes call. My question is on every updateListUI call list view goes back to first position. Is any way to solve this..?
use listLead.setSelectionFromTop(index, top); to set the position in list view
You can restore the posistion of your ListView using:
int position = 12; // your position in listview
ListView listView = (ListView) findViewById(R.id.listView);
listView.setSelectionFromTop(position, 0);
Instead of loading setAdapter during refresh
use the below code ... it will work
yourAdapterName.notifyDataSetChanged();
Just use the method notifyDataSetChanged() at the point where you want to refresh your list view.

Android ListView items loose listener if too many items added

I have a listview on which I have onItemLongClickListener attached. Everything works fine but when I add too many (e.g. 100) items to the list, some of the views lose the listener. Not sure what the problem is, I also debugged the problem and still can't figure it out.
There is no exception being thrown either, it just stops working.
Then if I remove everything from the list and start to add things, it works fine as long as I don't add to many item.
Is it the memory issue?
I searched around everywhere but no luck.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_frame);
// Set text view for orderTotal
orderTotal = (TextView) findViewById(R.id.orderTotal);
orderTotal.setText("0.00");
// List view from layout
itemsListView = (ListView) this.findViewById(R.id.item_list);
// Current order to be consistent throughout activities.
currentOrder = ((OrderIt)this.getApplication()).getCurrentOrder();
databaseCategories = (ArrayList<Category>) this.getIntent().getExtras()
.getSerializable("databaseCategories");
// List of items already in order.
itemsListView.setAdapter(new ItemAdapter(this, R.layout.list_item,
currentOrder.getItems()));
itemsListView.setOnItemLongClickListener(this);
this.registerForContextMenu(itemsListView);
// Gridview from layout
gridView = (GridView) findViewById(R.id.item_grid);
ArrayList<Item> items = getItems();
gridView.setAdapter(new ImageAdapter(this, items));
// Set click adapter to grid view items
gridView.setOnItemClickListener(this);
gridView.setOnItemLongClickListener(this);
}
I think:
Your item contains the EditText or Button which vies auto get focus from the item or listView.
you need to setFocus(false) or listView.setItemsCanFocus(false);
let the item views can't take the focus.
found the solution to the problem,
I was asking
adapterView.getChildAt(position)
which is not right way to do it if you looking to get underneath data,
right way is to call
adapterView.getItemAtPosition(position)
which returns underneath object which was used to populate listView.

Categories

Resources