Click on first item in a GridView is not working - android

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();
}

Related

Get Activity in Service

The code below works. But I know it is not the right way of doing things and I would like to know what is the right way to do this. I have a Keyboard service that that displays GIF images in a ListView. But the ListView needs an activity to show it. How do I get an activity in my ListView? (I have already tried casting the service as an activity. I get an invalid cast exception)
public override View OnCreateInputView()
{
View imageKeyboardView = (View)LayoutInflater.Inflate(Resource.Layout.ImageKeyboard, null);
listView = imageKeyboardView.FindViewById<ListView>(Resource.Id.List); // get reference to the ListView in the layout
Activity act = MainActivity.Instance;
// populate the listview with data
listView.Adapter = new ImageKeyboardAdapter(act, listOfIconUris);
listView.ItemClick += OnListItemClick; // to be defined
return imageKeyboardView;
}
You could try using CurrentActivityPlugin to get your Application’s current Activity that is being displayed.

Android - Removing Item from GridView/Adapter

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.

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.

how can we dynamically add only 20 images from webservice in the grid view

I want 20 images on one page and upon clicking on the next button, I will get another images on the same page.
That means, like in email programs, we have "older" & "oldest" by clicking "older" we will get the next emails.
same way i want images here by XML Parser.
GridView gView = (GridView)findViewById(R.id.gridview);
btnNext = (Button)findViewById(R.id.btnNext);
btnPrevious = (Button)findViewById(R.id.btnPrevious);
adap = new EfficientAdapter(this);
gView.setAdapter(adap);
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
adap.notifydatasetchanged();/*asking for create notifydatasetchanged()*/
EfficientAdapter adap = new EfficientAdapter(this);/* shows the error and asking for create constructor EfficientAdapter(OnClickListener)*/
gView.setAdapter(adap);
}
});
First and above all if you want to parse all the images from XML together at at time and show them, It will be a bed approach , Instead let your server file handle this i.e. each time you hit a url it should return you 20 records .
For example
String urlToHit=Config.basicUrl+"serverfile.php?showperpage="+Config.showPerPage+"&pageindex="+Config.currentPage;
where showPerPage is the number of records you want in per page and currentPage is the index of the page (it will be incremented for each new page).
Now suppose you have a button and now onClick of button parse the url(hit the url) and call setAdapter:
gridview.setAdapter(new ImageAdapter(getApplicationContext()));
and return 20 by getCount() of your BaseAdapter.
Hope this will solve your problem.
The error you mark in the code (EfficientAdapter adap = new EfficientAdapter(this);) is because this, in the context of that callback, refers to the new OnClickListener, instead of the view.
You can either create a new one as EfficientAdapter adap = new EfficientAdapter(v); or, much better, reuse the one you created before:
Remove the two lines
EfficientAdapter adap = new EfficientAdapter(this);/* shows the error and asking for create constructor EfficientAdapter(OnClickListener)*/
gView.setAdapter(adap);
and use the onClick callBack to ask the server for the next 20 images. Fill those images to your adapter and then adap.notifyDataSetChanged() (mind the Caps) "Notifies the attached View that the underlying data has been changed and it should refresh itself."

Images don't display but TextView contents is correct

I am working on an Android application that displays a list of items when a tab is selected, then displays detail information about an item that was selected from the list :
Activity1
Tab1 -(intent)-> Activity 2: item1
item2 -(intent)-> Activity 3: ImageView1
item3 TextView1
... ImageButton1
...
The details are to be displayed using ImageViews, ImageButtons, and TextViews. The data comes from a database query, so the TextView text and ImageView drawables have to be assigned dynamically.
The correct text from the database query gets assigned to each of the Views. I can see everything being added when I step through the code in the onCreate method of Activity3 in the debugger.
BUT, the drawables are only displayed when I select the first item in the ListActivity. If I select another item, all TextViews display the correct information but the drawables are not displayed for the ImageViews or ImageButtons. It looks like only the backgrounds are being displayed.
The ImageButton drawables are not being dynamically assigned, only the ImageView drawables are. The ImageButton drawables are set in the TableLayout XML that is associated with the activity. Here is one entry :
<TableRow android:id="#+id/row11" >
<ImageButton android:id="#+id/phonebutton"
android:maxHeight="50px"
android:maxWidth="50px"
android:background="#drawable/ic_phone"
android:adjustViewBounds="true"
style="?android:attr/buttonStyleSmall"
android:src="#drawable/phoneselector" >
</ImageButton >
<TextView android:id="#+id/phonenumber"/>
(note - Originally, I had android_drawable="#drawable/ic_phone", then tried adding android:background="#drawable/ic_phone", and finally created the following selector (phoneselector.xml) :
- end note)
If I select the first item again, its proper ImageView drawables are displayed along with the proper text in the TextViews. The ImageView drawables that belong to the other items have no problems being displayed in Activity2's ListView.
The code successfully sets an onClickListener for the button (also in Activity3.onCreate). If I click on what is displayed on the spot where the drawable should be, the phone number is dialed.
The code successfully sets an onClickListener for the button (also in DetailActivity.onCreate).
Has anyone seen anything like this before? I would appreciate any help figuring out what is wrong.
Here are more details because I wonder if the problem has to do with the use of the Views :
The Activity1 extends TabActivity and sets a TabHost view that contains a FrameLayout along with the TabWidget. Activity1 creates the tabs dynamically.
Activity2 extends ListActivity and sets the content to its ListView (via setContentView(getListView()).
When ListActivity.onListItemClick is invoked (item in list is selected), a new Intent is created that contains an identifier in a Bundle and Activity3 is started.
The started activity (in Activity3.onCreate...) sets the content to a TableLayout view defined in a .xml file (via setContentView(R.layout.details.xml).
Here is the code from Activity3.onCreate :
Cursor c = null;
DatabaseHelper myDbHelper = null;
ImageButton button = null;
TextView phoneText = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Resources res = getResources();
setContentView(R.layout.details);
Bundle bundle = this.getIntent().getExtras();
Integer id = (Integer) bundle.getInt("id");
myDbHelper = new DatabaseHelper(this);
c = myDbHelper.getWinery(id);
if (c != null) {
c.moveToFirst();
ImageView image1 = (ImageView) this.findViewById(R.id.iconimage);
String wholeString = c.getString(c.getColumnIndex("PhotoIcon"));
if (wholeString != null) { String[] splitString = wholeString.split(".jpg");
String sString = splitString[0];
int path = res.getIdentifier(sString, "drawable", "com.winerytour" );
Drawable drawImage1 = this.getResources().getDrawable(path);
drawImage1.setVisible(true, true); //added to try to fix problem
try {
image1.setImageDrawable(drawImage1);
image1.refreshDrawableState(););
} catch (RuntimeException e) {
e.getMessage();
}
image1.setAdjustViewBounds(true); }
.. set other TextView text in same way ...
phoneText = (TextView) this.findViewById(R.id.phonenumber);
String phone = c.getString(c.getColumnIndex("Telephone"));
phoneText.setText(phone);
button = (ImageButton) this.findViewById(R.id.phonebutton);
button.setOnClickListener(new ImageButton.OnClickListener() {
public void onClick(View v) {
performDial(); } });
((ImageButton) button).refreshDrawableState(); // attempt to fix problem
} // end c != null
}
content to its ListView (via setContentView(getListView()). Don't do that. If you extend listactivity, the view is built it. If you don't want that, don't 'extend listview

Categories

Resources