I wrote a class similar to .NET GridView for android but my mind went blank for one action! when I loop through rows (or data whatever) I want to fire an event which includes selected row on every row. You can think as if you are reaching each row separately with helping by rowdatabound on .NET.
for (int m = 0; m < this.columns.length; m++) {
do some stuff
fire()
}
Do I need to create a new thread or listener? If yes how do I do these and how do I catch it on the otherside? Please give me an idea.
with this codes which I wrote below, I can create a gridview and display it without any problem. I need to get every rows one by one but how?
DataGrid dgSiparis = new DataGrid(this,lnSiparis,dt); //CREATING THE DATAGRID, argumants: context, linearlayout, a class named datatable which runs similar to DataTable on .NET, dt object has rows)
dgSiparis.setDataBind();
The answer:
create an interface for listener
implement your class with it (for this example DataGrid class will be implement it)
set it where you loop through rows
catch the listener you create on the instance class (for this ex dgSiparis)
There are no events in Java, instead it uses Observer - Listener pattern. Have a look at this article for details:
http://www.vogella.de/articles/DesignPatternObserver/article.html
Related
This question is about Couchbase lite (no Sync Gateway).
I'm new to Couchbase, I managed to use the demo app, but I don't understand it completely.
It contains this code which (as far as I understand, since I'm not native English speaker) retrieve views to populate a listview with the indexes:
// This code can be found in ListsActivity.java
// in the setupViewAndQuery() method
com.couchbase.lite.View listsView = mDatabase.getView("list/listsByName");
if (listsView.getMap() == null) {
listsView.setMap(new Mapper() {
#Override
public void map(Map<String, Object> document, Emitter emitter) {
String type = (String) document.get("type");
if ("task-list".equals(type)) {
emitter.emit(document.get("name"), null);
}
}
}, "1.0");
}
listsLiveQuery = listsView.createQuery().toLiveQuery();
Could anyone give me a hand with what each part is doing?
In which step is the listview populated
Can I change "list/listsByName" in the code (line 3)? What would happen?
Can I emit more than one element?
The code is a little bit convoluted. Let's answer the easy parts first.
Can I change "list/listsByName" in the code (line 3)?
Yes. That's just the name of the Couchbase View. You choose the View name. Unfortunately the terms used in Couchbase and Android overlap some. A Couchbase View is a kind of static index of your database.
Can I emit more than one element?
Yes. You can emit most anything you want. Take a look at the documentation here
Now, tracing how the Android ListView gets updated:
In ListsActivity.java notice in the onCreate method a ListAdapter instance gets added to the ListView. This ListAdapter is a private inner class that extends LiveQueryAdapter.
LiveQueryAdapter is in the utils subpackage. If you look at its constructor, you'll see it adds a change listener to the query passed in. When triggered, this change listener sets an enumerator equal to the rows passed back by the live query, then calls notifyDataSetChanged to tell the list to refresh itself. That, in turn, causes getView in ListAdapter to get called. That's where the data is pulled from the database and used to populate a list entry.
I am trying to add rows in a tableLayout dynamically with click events trigerring some action for each row. The data is dynamic because it is coming from the server.
I have come upon two different ways to use the dynamic approach and registering the click events.
For dynamic approach the easiest way would be to put the rowAdditon() as a dedicate function like
tl.addView(row1);
tl.addView(row2);
and to integrate click events i would require each TableRow object for onCLickListener().
But i am not able to find the best way to integrate them together.
First thing that comes to my mind is a ArrayList. Is there any other easy way to acheive this.
i tried to implement this like the following
ArrayList a1 = new ArrayList();
DataBaseReceiverForConnectToTeachers received=new DataBaseReceiverForConnectToTeachers(this);
int count=received.getNoOfData();
for(int i=0;i<count;i++){
a1.add("t"+i);
}
for(int i=0;i<count;i++){
TableRow a1.get(i)=new TableRow(this);
//getting error in this line
//cannot resolve method get(int)
//a1 is already define in the scope
}
The solution was creating a for loop that runs for the size of the items in database.
Why you want to put into table view , add the same in linear layout by creating it in xml and whenever you want to add button call a method and keep the id of this button static so that you can make every button on click method separately
Let me know if my answer completes your query else I can provide you code for this
I am new in programming Android. I am developing an app that you want to capture sound and analyzes it. The amplitude of the sound for each time is stored in an array. These are the data needed to analyze . My question is: How I show the values of the array in wich the sound is saved?
for (int i = 0; i < array.length ; i++ )
Log.i("value" , Integer.toString(array[i]))
This depends on how you want to output the values. You could choose to create a RecyclerView instance in your GUI and connect the array with an adapter (read here) or you could choose to format that information into a String and insert the text into a TextView using the setText(String) method.
Edit: Based on your comment it sounds like you should look at the Recylcer View. You will need the following things.
A layout file containing a Recycler View.
A ViewHolder class that extends RecyclerView.ViewHolder - this is used to display your content in individual items of the list
An adapter class that extends RecyclerView.Adapter - this connects your array to the ViewHolder
Some links to help you get on the way with RecyclerView:
A first glance at RecyclerView
TwoWay View
They do a much better job explaining and showing how the code will work then I will writing it again here.
I want to create a custom list in which items are added or removed dynamically (say when a button is tapped). The problem is I have very little knowledge of lists in android. I have gone through various tutorials on creating a custom list in android but none of them shows how to dynamically add contents to it
What I know so far:
1) I have to create a model class to store data.
2) I have to create an adapter class.
3) Pass the objects of the model class as an arraylist to the adapter.
3) Bind listview to the adapter
What's confusing me:
1) I know I have to create an apapter class, but what's really confusing me is what kind of adapter ? i.e. ArrayAdapter, BaseAdapter ??
2) What and How will I feed adapter? I will be fetching data from the Sql lite database and I want the results to be displayed in my custom made list.
3) How will I update my list when a new record is added to the database ? I know how to populate listview from a static array but its of no use in my project.
I need little guidance where should I start from ?
1) You can use ArrayAdapter.
2) After you create your own arraylist, you can pass it for first time, listview.setAdapter(...
3) After you refresh your data you can call this method, ((ArrayAdapter)listView.getAdapter).notifyDataSetChanged(). This will ensure your listview refresh.
The below link is a good example:
https://github.com/thecodepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
I have implmented pagination and it display 5 records per page. Now suppose I am on page 3 and click 3'rd element then 3'rd element of page-1 is selected.
I am not able to figure out problem as I always create new list object while setting data.
I used below code
temp = new ArrayList();
this.someListAdapter = new SomeListAdapter(this, R.layout.row_facet,temp);
setListAdapter(this.someListAdapter );
Below is signature of SomeListAdapter class.
public class SomeListAdapter extends ArrayAdapter<VoNeighborhood> {
}
Please help....
There really aren't enough details here about how you do pagination with your ListView.
So I might guess you're overriding onListItemClick and using the position variable it sends you, but you then don't take into account the page you're on?
Alternatively, just don't use pagination as you have an infinite canvas to scroll your list within — I don't think I've recall seeing an Android app so far that uses pagination!