Dynamically load table view content in Titanium for Android - android

I am creating an appliction which uses Google Books API. So whenever I search a book it gives a JSON response and I load those results in my table view. There will be thousands of books results when I search. But I don't want to load everything in my tableview. Whenever I scroll down it only has to load next books.
Can anyone give me a code or rough idea on how to do this in Android using Titanium? I have checked this post: https://github.com/appcelerator/KitchenSink/blob/master/Resources/examples/table_view_dynamic_scroll.js But this is for iPhone, I need it for Android as well. Help me out...

After looking around I have implemented following solution for android:
tableView.addEventListener('scroll',
function(e) {
if (!e.source.__doneUpdating && e.totalItemCount % e.source.__pageSize === 0) {
var distance = e.totalItemCount - e.firstVisibleItem;
if (distance <= e.visibleItemCount) {
if (!e.source.__updating) {
e.source.__updating = true;
e.source.fireEvent('beginUpdate', e);
}
}
}
Ti.API.info('-------------------');
Ti.API.info( 'e.firstVisibleItem: ' + e.firstVisibleItem);
Ti.API.info( 'e.totalItemCount: ' + e.totalItemCount);
Ti.API.info( 'e.visibleItemCount: '+ e.visibleItemCount);
}
);
Where e.source.__pageSize, e.source.__doneUpdating, and e.source.__updating are internal variables that are maintained by the code inserting rows into the tableView.

Related

create book reader in android using simple text

I need your knowledge
I have to create book reader applications like the amazon kindle but, using simple text formate not any file formate like pdf, epub or any. Load simple text in textview.
I have one idea, assume we have 1000 lines in one book. Load this text in textview with android:layout_height="match_parent". when text crosses the limit of texview hight, it auto load remaining text in the next textview but, how to know the text crosses the limit (eclipsing) of textview.
To implement this I have written this code but l.getEllipsisCount(lines - 1) return 0.
lLayout.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
// your code here. `this` should work
val l = tvHtml.getLayout();
if (l != null) {
var lines = l.getLineCount()
if (lines > 0) {
if (l.getEllipsisCount(lines - 1) > 0) {//2147483647
Log.d("log ---------", "Text is ellipsized")
}
}
}
}
})
This is my logic but, if you have any new ideas to perform this task, pls inform me.
Thanks

Android App stuck for a few seconds while generating list of informations with images

I made an Android App with Titanium Appcelerator where you can search for products in a local SQLite database. The list I generate when the user clicks "Show all" takes the first 50 entries and displays the info as a list. All works fine as long as i have no images. When I include the images to the app (about > 10.000 images in a folder), the app is stuck the first time I make a database request and generate the listview.
If I go back and repeat the step, it will work immediately. If I restart the app it will stuck. I don't know how to fix that. Could it be, that one folder with thousands of files is the problem? Could it help to split the images into different folders?
I create the window and send data to it, before I open it
if (!onlineSearchContainerWindow) {
var onlineSearchContainerWindow = SearchWindow();
}
onlineSearchContainerWindow.fireEvent('setDataArr', {
first : self.first
});
onlineSearchContainerWindow.open();
I had a Template for the Listview with a imageview included
childTemplates : [{
type : 'Ti.UI.View',
bindId : 'artPicContainer',
properties : {
left : '0dp',
width : Number((dpuDeviceWidth / 2) - 25) + 'dp',
height : Number((dpuDeviceWidth / 2) - 25) + 'dp',
borderWidth : 2,
borderColor : '#6c6c6c',
borderRadius : 4,
backgroundColor : '#fff'
},
childTemplates : [{
type : 'Ti.UI.ImageView', // artPic ICON
bindId : 'artPic',
properties : {
isLoaded : true,
size : {
width : Number((dpuDeviceWidth / 2) - 25) + 'dp',
height : Number((dpuDeviceWidth / 2) - 25) + 'dp',
}
}
}]
},
I open the database, execute the statement with a limit of 50
db = Titanium.Database.open("productsDB");
var dbData = db.execute(statement + " LIMIT " + String(number * maxDisplay) + ", " + Number(maxDisplay + 1));
Then I create a data object with the image path for id artPic and other values and push it into an array
var listviewData = {
firstLevelContainer : {
artikelnummer : xxx,
},
artNrLabel : {
text : xxx
},
artPic : {
image : "/images/path/" + imagename + ".jpg"
}
};
I create a Listviewsection and link the array with the datasets. Then I create the Listview and set the template and the section
itemListView = Ti.UI.createListView({
top : 0,
templates : {
'template' : myTemplate
},
defaultItemTemplate : 'template',
footerView : endRow,
sections : [itemListSection],
});
mainCon.add(itemListView);
After that I hide the preloader for the window. But till then, the Preloader stucks for a long time, stop turn around an freezing.
I dont link the building process to the open event.
The image size is around 1-3 kb each.
All together:
According to the information that you have provided their can be two possibilities causing delay of action :
You are saving the image files in the SQLite database
Solution : If this is the case then try saving the Image URL in the database and not the images. You can get the image URL by using the FileSystem API.
Your Database is Installed when you click on the 'Show All' button
Solution : If this is the case then remove the install method from the eventListener and place it in the alloy.js or index.js, this would help you speed up the process when you click on the 'Show All' button.
Good Luck & Cheers
Ashish Sebastian

Google play game services Leaderboard with custom UI using Unity3d plugin of google

We want to display all leaderboard data into our custom created UI for the game. for that we want to access top scores info such as Profile Picture,Score and Name of the player.
All data will be shown in our custom created UI
we are using following unity3d plugin of google
https://github.com/playgameservices/play-games-plugin-for-unity
Please let us know how to access all players data of our game leaderboard
The you link provided has the actual documentation on how to use the plugin. From Accessing Leaderboard data (where you could get the Score) to Getting Player names, where it is mentioned that you could
use Social.LoadUsers() to load the player profile
from there you could get an IUserProfile to get the image and username. Sample from the same link:
internal void LoadUsersAndDisplay(ILeaderboard lb)
{
// get the user ids
List<string> userIds = new List<string>();
foreach(IScore score in lb.scores) {
userIds.Add(score.userID);
}
// load the profiles and display (or in this case, log)
Social.LoadUsers(userIds.ToArray(), (users) =>
{
string status = "Leaderboard loading: " + lb.title + " count = " +
lb.scores.Length;
foreach(IScore score in lb.scores) {
IUserProfile user = FindUser(users, score.userID);
status += "\n" + score.formattedValue + " by " +
(string)(
(user != null) ? user.userName : "**unk_" + score.userID + "**");
}
Debug.log(status);
});
}
With all that said, if you were hoping for a more detailed and precise sample, it'd be considered as too broad here in Stack Overflow and may be voted to be closed.

Count query with large amount of objects

I am using parse.com to populate a listview in android. Each item in the list view has a textview that shows the like count and another one that shows the comment count.
Now, according to parse.com
"For classes with over 1000 objects, count operations are limited by timeouts. They may routinely yield timeout errors or return results that are only approximately correct. Thus, it is preferable to architect your application to avoid this sort of count operation."
what would be the recommended/ideal way of going about it then?
What I did is I created a column called commentCount, and a column called likeCount. Then in afterSave, I modified the appropriate cell.
Parse.Cloud.afterSave("Activity", function(request) {
Parse.Cloud.useMasterKey(); //bypasses ACL requirements
//After commenting, increment commentCount
if(request.object.get("type") == "comment"){
query = new Parse.Query("Posts");
query.get(request.object.get("post").id, {
success: function(post) {
post.increment("commentCount", 1);
post.save();
},
error: function(error) {
console.error("Got an error " + error.code + " : " + error.message);
}
});
}
});

select dropdown not active on Android browser

I have a problem where a drop-down select box does not drop-down (it's essentially inactive) when viewed on an Android mobile device. It works fine on desktop browsers as well as ios browser - bringing up a picker wheel on ios and a dropdown select list from the desktop.
Sample code is;
<select id = "log_or_norm" autofocus>
<option value="1">Lognormal</option>
<option value="2">Normal</option>
</select>
I tried the suggestion found at;
http://youngliferamblings.wordpress.com/2011/08/09/select-dropdown-in-android-webview/
which was
The select tag just doesn’t work sometimes in Android, especially in an app using webview. This drove me nuts for a long long time. The main fix I found, even if your select is buried deep in divs and rows and what ever, is this css:
select {
visibility: visible;
-webkit-appearance: menulist-text;
}
The -webkit-appearance might be the only one actually needed and setting it to ‘listbox’ works too.
That’s all. This deserved its own post.
Without luck....
Am hoping that one of the gurus here can provide an elegant solution that will avoid me having to go down the route of making discrete buttons for each option. I'm not fussed as to whether the Android experience gets a nice picker wheel or not, but need to be able to allow Android users to select an option.
Thanking you in advance
Try
getDriver().createElement(By.id("your locator"));
getDriver().createElement(By.id("your locator")).sendKeys("option Name you want to give");
var divCreated = false;
$(document).ready(function () {
var value = "";
$("select").each(function (i) {
$(this).click(function () {
//alert(($(this).is(":focus")));
if (!($(this).is(":focus"))) {
if (!divCreated) {
$("body").append('<div class="for_select"></div>');
divCreated = true;
}
$(this).clone().appendTo(".for_select");
open($(this));
}
});
});
function open(obj) {
var pos = $(obj).offset();
$(".for_select select").css("position", "absolute");
$(".for_select select").css("zIndex", "9999999999999");
var toAdd = $(obj).innerHeight();
$(".for_select select").offset({
top: pos.top + toAdd,
left: pos.left
});
$(".for_select select").attr("size", ($(obj).children("option").length > 10 ? 10 : $(obj).children("option").length));
$(".for_select select").change(function () {
value = $(".for_select select").val();
$(obj).val(value);
$(obj).children("option").each(function () {
if ($(this).text() == value)
$(this).attr("selected", "selected");
else if ($(this).attr("selected")) {
$(this).removeAttr("selected");
}
});
var parentHeight = $(obj).parent().innerHeight();
$(obj).parent().css("height", parentHeight + "px");
$(obj).parent().css("position", "relative");
$(obj).css("position", "absolute");
$(obj).css("left", "0px");
var prevElementsHeight = 0;
var isSelect = false;
$(obj).parent().children("*").each(function () {
if ($(this) == $(obj))
isSelect = true;
if (!isSelect)
prevElementsHeight += $(this).innerHeight();
});
$(obj).css("top", (prevElementsHeight / 2) + "px");
$(obj).css("zIndex", "9999");
close($(".for_select select"));
});
}
function close(obj) {
$(obj).css("position", "static");
$(obj).attr("size", "1");
$(".for_select").empty();
}
});
The solution came rather simple after I battled this for days.
Try debugging by bringing out the select from nested divs until you find the problematic div.
In my case, the problem was that I wrapped all the data-role="page" divs in a parent div (for loading sake on low-end devices). For some reason, < Android 2.3 has a problem with that.

Categories

Resources