1

I'm working on a small/basic android application that allows users to create items in a ListView and keep some statistics on these items (such as click frequency, etc.). Right now I have it so users can enter the name of their new item and once they click the create button it adds the element to the ListView. If the user selects one of the items in the list it opens a second activity where they will be able to analyze statistics, reset the click counter, etc.

This is what the application looks like

(* Side info *: MainActivity.java is the main activity as shown above. ClickerActivity.java is the second activity that is used to display the statistics for each clicker. Clicker.java is a class that holds name/click count/statistics for each clicker. )

My approach has been that when the 'New Clicker' button is pressed I should take the name they've given and add it to the ListView and create a Clicker class object (which is used to track statistics). These Clicker objects would be stored locally in an ArrayList, so that I can grow and shrink the list with ease. Then start the clickerActivity - passing the Clicker class object as a parameter.

This leaves me with a few questions, first: how can I create an instance of this class dynamically and give the variable representation a meaningful name (on the go). I was thinking of using the string variable the user enters to name the clicker but I am not sure if this is allowed.

ex.

// This is the string name the user chooses for the new clicker
String newClickerName = clickerName.getText().toString();
Clicker newClickerName = new Clicker();
// Start the activity and pass the clicker object.

I realize that the above code wouldn't work but it is my best way of showing what I am trying to do. I've seen some posts where people are suggesting using

Class var = Class.forName(classname);

But I am not sure that is what I am looking for.

Secondly If you find that this is not a good design choice (I don't mean graphical) then please voice your opinion on how it could be improved. I am new to this and appreciate the feedback.

Lastly, thank you for taking the time to read my question. If there is any confusion please voice it and I will do my best to clarify.

2
  • is clicker a activity? Commented Jan 9, 2014 at 10:05
  • clicker.java just contains a class Clicker which holds statistics for each individual 'clicker'. ClickerActivity is a general activity that is called when ANY of the clickers are opened and it will display the statistics and options. @Raghunandan Commented Jan 9, 2014 at 10:09

2 Answers 2

3

You're struggling with a problem which can't be solved in a way you want it to. I understand you want to achieve something like this:

User gives you "nameForMyVariable" String and you want to create a variable with that name: Clicker nameForMyVariable;

This can't be done.

You can however instead of creating simply Clicker object, create a Pair<Clicker, String>, where the String field will be used by you as a key. Then you can create an ArrayList of those Pairs and search for Clicker you need by looking at its key.

Note that you'd need to create your own Pair class as Java doesn't have it. It will not be hard though.

The above approach leads also to another solution - using a HashMap<String, Clicker>, so that String is a key. You can use it like this:

HashMap<String, Clicker> clickers = new HashMap<String, Clicker>();

// put some content to the map
clickers.put("nameForMyVariable1", new Clicker());
clickers.put("nameForMyVariable2", new Clicker());

// now you can find appropriate Clicker simply by specifying its key
clickers.get("nameForMyVariable1"); // returns Clicker with nameForMyVariable1 name

Tell me if something is not understandable.

EDIT:

According to question you asked in comment. Let's say you have your ArrayList<Pair<Clicker, String>> clickers created somewhere.

You can add new Pair like this:

clickers.add(new Pair<Clicker, String>(new Clicker(), "myVariableName1"));

Do you see now? Not using name for a Pair object at all.

Sign up to request clarification or add additional context in comments.

4 Comments

I understand what you mean by using a Pair class like a dictionary to identify each clicker. If I were to do this than I would need a new instance of Pair each time a clicker is created, correct? And I could keep the Pair instances in my ArrayList<Pair>. But I would still need to give each Pair instance a unique name, right? Does that not sort of land me in the same issue of naming the instances? or is there a way around that? Let me know if this reply is too confusing/jumbled and I will try and reword it.
I know what you mean. You don't have to name those Pairs uniquely as you store them in ArrayList. I will edit my answer to show you what I mean. Anyway, I'd really encourage you to try the HashMap solution as it's quicker and may teach you something - I just showed you how to use it, so you can see it's really simple.
AH YES! I just found something similar myself, concerning using Hashmap, and was going to ask what you thought. Perfect, Thank you.
No problem, I'm glad you found my answer useful. :-) Good luck!
1

Adding to the answer by Piotr. An alternative of using a dictionary or Pair is adding a member of type String ,say name inside the Clicker class.Then create a new instance of Clicker using Class.newInstance() and set the value of the name member as the string input by the user,so that you can later refer to this object using this name. Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.