I am stuck with an issue in my project.The need is to show all data fetched from server using webservices.I successfully get data from server using json.but i want to show data on screen in tabular format 50 records at a time.Please suggest me how to do this or if you can guide me through a better way to implement paging in android.
The code of paging is here :
Implement Pagination on tab layout
and the function i use to append rows dynamically to tablelayout is :
private void appendRows(TableLayout table, String[] data) {
int rowSize = data.length;
int colSize = (data.length > 0) ? 1 : 0;
for (int i = 0; i < rowSize; i++) {
TableRow row = new TableRow(this);
for (int j = 0; j < colSize; j++) {
String[] rowVal = null;
rowVal = data[i].split(",");
for (int k = 0; k <= rowVal.length - 1; k++) {
TextView c = new TextView(this);
c.setText(rowVal[k]);
c.setTextColor(getResources().getColor(R.color.white));
c.setPadding(3, 3, 3, 3);
row.addView(c);
}
}
table.addView(row, new TableLayout.LayoutParams());
}
}
Please guide me I need to show hyperlink page numbers at the bottom of my window as shown in google
This might be of some help. Its a tutorial for pagination. Here is the source code.
I have had similar requirement like yours and I normally use the listview for it. Due to the recycling of views it is a lot more efficient than using a table layout.
You can create a custom adapter where you always return the size as 50+1, 100+1. So in your onItemClickListener you can check if the row position is more than the items, if so, then you add the next 50 items to the adapter. This is a basic idea of how to do it.
Related
I try to send several lists to other activity so I wrote the following code:
ArrayList<String> sections = new ArrayList<String>();
for(int i=1; i<=last; i++)
{
sections.clear();
for(j = 0; j < size; j++)
{
sections.add(someText);
}
ourIntent.putStringArrayListExtra("sections_"+i, sections);
}
As you can see for every loop cycle of i, the name I give to the sent list is different (sections_1, sections_2, ...).
The sections list is cleared in each loop cycle. And in debug mode I can see that in every loop cycle the sections have the right list.
The problem is in the next activity. When I take the list, with the following code:
sections1 = extras.getStringArrayList("sections_1");
sections2 = extras.getStringArrayList("sections_2");
sections1 and sections2 get the same list, which is the last list that was inserted in putStringArrayListExtra.
Anyone can explain this behavior?
I believe this is because it's storing a reference to your ArrayList, and not a copy of the current "state" of the list on each iteration of your loop.
For example, you first insert a reference to your ArrayList when it contains 1 item. Next, you insert a reference to your ArrayList when it contains 2 items. Both are just references, so when you actually transition to the next Activity, it copies the full ArrayList.
To fix this, you could actually make a local copy of the ArrayList each time you loop.
ArrayList<String> sections = new ArrayList<String>();
for(int i=1; i<=last; i++) {
sections.clear();
for(j = 0; j < size; j++){
sections.add(someText);
}
// Creating a new local copy of the current list.
ArrayList<String> newList = newArrayList<>(sections);
// Inserting the local copy instead.
ourIntent.putStringArrayListExtra("sections_"+i, newList);
}
I created a listview that connects to a database. I want to show some of the newest items (maybe 10 newest items) that will add in a random time? How can I accomplish this? This what I have tried:
int i = String.valueOf(mKondisiList));
for(i = 0; i<=5 ; i++ ){
mKondisiList = myDbHelper.getListKondisi();
adapterKondisi = new ListKondisiAdapter(this, mKondisiList);
lvKondisi.setAdapter(adapterKondisi);
Collections.reverse(mKondisiList);
}
But this doesn't work.
Sorry but, this code block makes no sense.
int i = String.valueOf(mKondisiList));
for(i = 0; i<=5 ; i++ ){
mKondisiList = myDbHelper.getListKondisi();
adapterKondisi = new ListKondisiAdapter(this, mKondisiList);
lvKondisi.setAdapter(adapterKondisi);
Collections.reverse(mKondisiList);
}
To explain what you have did;
you are getting the list from your database
initializing the list adapter
setting adapter to list
then reverse your order of the list (but you are not notifying the adapter)
You are doing this in a loop exactly 5 times.
What you need to do is:
get your list from database
then reverse your order of the list
initialize the list adapter
set adapter to list
I should look like this:
mKondisiList = myDbHelper.getListKondisi();
Collections.reverse(mKondisiList);
adapterKondisi = new ListKondisiAdapter(this, mKondisiList);
lvKondisi.setAdapter(adapterKondisi);
If you want to show only limited number of items, you can do something like below:
ArrayList<Object> fullList = new ArrayList<>();
fullList = myDbHelper.getListKondisi();
Collections.reverse(fullList);
//Add first 10 item to your mKondisiList
for (int i = 0; i < 10; i++) {
mKondisiList.add(fullList.get(i));
}
adapterKondisi = new ListKondisiAdapter(this, mKondisiList);
lvKondisi.setAdapter(adapterKondisi);
I created a loop that will get the data from my cursor, however I noticed that even though it is looping(with the correct count) it only shows the first value.
int vv = 0;
if ((CR3.moveToFirst()) || CR3.getCount() !=0){
while (CR3.isAfterLast() == false) {
vendoName[vv] = CR3.getString(0);
vendoEsch[vv] = CR3.getString(1);
vendoAsch[vv] = CR3.getString(2);
vendoTag[vv] = CR3.getString(3);
vv++;
CR3.moveToNext();
}}
and when I call all my data( I only need the first three records)
ArrayList<SearchResults2> results2 = new ArrayList<SearchResults2>();
SearchResults2 sr2 = new SearchResults2();
for(int j = 0;j < 3;j++)
{
sr2.setName(vendoName[j]);
sr2.setEsch(vendoEsch[j]);
sr2.setAsch(vendoAsch[j]);
sr2.setTag(vendoTag[j]);
results2.add(sr2);
}
I am putting inside a listview, when I check, it is showing always the first data.
This is an example I used as a reference to my code(It's almost the same except that I used an array to put my data from)
http://geekswithblogs.net/bosuch/archive/2011/01/31/android---create-a-custom-multi-line-listview-bound-to-an.aspx
Am I doing something wrong which is why it is only getting the first piece of data?
Is it not easier to do something like this (if you don't need more than 3 results):
ArrayList<SearchResults2> results2 = new ArrayList<SearchResults2>();
CR3.moveToFirst();
for (i = 0; i < 3; i++) {
SearchResults2 sr2 = new SearchResults2();
sr2.setName(CR3.getString(0));
sr2.setEsch(CR3.getString(1));
sr2.setAsch(CR3.getString(2));
sr2.setTag(CR3.getString(3));
results2.add(sr2);
CR3.moveToNext();
}
I think that maybe the cursor doesn't iterate properly through your results in your while-loop and that's why you become one and the same result for the three items
I have many controls in an activity layout in Android, and
i want to get their value in a simple for, the problem is that
Eclipse assigns the ID randomy, so I cannot get controls ID in a
sequential way to access the controls in a for loop because
they are not sequential IDs.
Here is why I am trying to do:
int visibleId = R.id.fieldVisible1;
int editId = R.id.editField2;
int spinnerId = R.id.spinner1;
for(int nIndex = 0; nIndex < 12; nIndex++)
{
CheckBox checkBox = (CheckBox)findViewById(visibleId + nIndex);
checkBox.setChecked(_fieldsInfoArray.get(nIndex).getVisible() == 1);
EditText edit = (EditText)findViewById(editId );
edit.setText(_fieldsInfoArray.get(nIndex).getName());
Spinner spinner = (Spinner)findViewById(spinnerId + nIndex);
spinner.setSelection(_fieldsInfoArray.get(nIndex).getOffset());
}
how can I make Eclipse to assign sequential IDs to controls so I can
just increment by 1 the id in a loop?
A better way of doing this may possibly to look into the use of:
int children = layout.getChildCount();
for (int i=0; i < children; i++){
View v = ll.getChildAt(i);
}
This would allow you to loop over all views inside the layout without needing to know their IDs.
I am trying to create a layout, that shows current 3 hour lessons along with the time and date with room number.
possible screens:
The room and time/date is always static at the top and the rest would be dynamic from calls in SQL from JSON.
data = idleResponse.getJSONArray("lecture");
ArrayList<String> lects = new ArrayList<String>();
for (int i = 0; i < data.length(); i++) {
JSONObject jObj = data.getJSONObject(i);
String time = jObj.getString("startTime"); // to do.substring(0, 4);
String moduleName = jObj.getString("moduleName");
lects.add(time + " " + moduleName);
}
String[] lectureList = new String[lects.size()];
for (int i = 0; i<lects.size();i++){
// fill with data
lectureList[i] = lects.get(i);
}
lecture.setText("Upcomming Lectures:");
//set to list view
ListView listitems=(ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,lectureList);
listitems.setAdapter(adapter);
I tried to implement this with a list view, however it just shrinks down to a small section of the screen based on number of elements.
as you can see it just shrinks down.
I was wondering what would be the best type of layout to use for this type of problem, all of my layouts are pretty basic and something like this seems quite a challenge for me thanks.
You would have to write your own Adapter for listview and use weight property for layout of each row