Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Suggestion on how to implement dynamic list with onClick listeners

 
Ben Alex Barreto
Greenhorn
Posts: 15
Java Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Me and my group are trying to come up with a good implementation for this week's case study. We will be querying data from a database, we need to have a list of dates that must be dynamic, and by that I mean, you gotta be able to click on a date and then see what's in the database according to that date.

Well, I figured we could create TextViews dynamically, using a loop, but then, how do you assign the listeners to them? Do you have a pre-written list of listeners or would we have to implement them using reflection? I know we can set the textviews to clickable and then assign a listener to them.

Well, if anyone has any suggestions, I are listening :P so far, we are just trying to figure out how to handle the listeners. Another questions, however, how do I update the view with the new elements without having previously created them in the XML file? Is that even possible?

Please excuse my ignorance :P I'm still getting familiar with the android environment.

-Ben
 
Ben Alex Barreto
Greenhorn
Posts: 15
Java Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far I've been thinking about doing something similar to this example:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ScrollView2.html

And for the listeners, I was thinking about using reflection, which would then allow us to create just one listener, and assign it to however-many textviews we create.



Would this work?

-Ben
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well try it and tell us
 
Przemek Boryka
Ranch Hand
Posts: 51
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Why don't you using ListView with ListAdapter which is pumped with data from database? To get any row - setOnItemClickListener(listener) ? Below is example :

Your Activity can implement OnItemClickListener interface.

... // onCreate()
ListView listView = new ListView(context);
listView.setOnItemClickListener(context);
...


... //your method that set listview
String[] data = new String[cursor.getCount()];
int index = 0;
...


while (cursor.moveToNext()) { // cursor is to get data from database
data[index++] = cursor.getString(0); // you get the string value from column number 0 from resultset
}
...

ArrayAdapter<String> myList = new ArrayAdapter<String>(context, R.id.your_defined_row, data); // setup ListView with data

listView.setAdapter(myList); // set listview by invoking setAdapter method with myList which contain view of row and data.
listView.invalidate(); // rebuild listview

...
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) { // to get any row of list

}
...

Bye
 
Ben Alex Barreto
Greenhorn
Posts: 15
Java Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Przemek Boryka wrote:Hi

Why don't you using ListView with ListAdapter which is pumped with data from database? To get any row - setOnItemClickListener(listener) ? Below is example :

Your Activity can implement OnItemClickListener interface.

... // onCreate()
ListView listView = new ListView(context);
listView.setOnItemClickListener(context);
...


... //your method that set listview
String[] data = new String[cursor.getCount()];
int index = 0;
...


while (cursor.moveToNext()) { // cursor is to get data from database
data[index++] = cursor.getString(0); // you get the string value from column number 0 from resultset
}
...

ArrayAdapter<String> myList = new ArrayAdapter<String>(context, R.id.your_defined_row, data); // setup ListView with data

listView.setAdapter(myList); // set listview by invoking setAdapter method with myList which contain view of row and data.
listView.invalidate(); // rebuild listview

...
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) { // to get any row of list

}
...

Bye


That's such a cool idea! I'm definitely going to give it a try and see how it goes! Meanwhile, let me show you guys what I had in mind previously. It's not as elegant as your solution, so please don't laugh :P

After getting data from the server, the textviews would be created dynamically according to however many entries we got, similar to the example I posted earlier. They are populated with text as they are created, since we would have, let's say, an ArrayList of entries already saved (by the way, this is the other part of this project. We are also implementing a server using hibernate to talk to our DBMS).

This is how we could assign listeners dynamically. This is what me and my team used a couple weeks ago for a different project.


But there is a problem To use reflection as we did, the elements we are assigning listeners to must have an id, however, by creating the textviews as we did, how can we assign an id to each of the textviews as they are created? Because they must be named ListenerClass_element in order for the right listener to be instantiated and then assigned. Also, when a textview is selected for the second time, it should be resized, so I gotta have a reference to each textview in order to get their dimensions and handle the event accordingly.

What do you think?

-Ben
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic