|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On April 23 2014 07:41 Fawkes wrote: Yeah Blisse, it was. I'm just so disappointed in myself, barely can even bring it up to people T.T It really sucks right now.
I had that same experience for a while. Good luck though.
|
I want to do the following with Java. I don't look for great efficiency, rather the most painless solution.
Here's basically what I want:
while (true) // because I like infinite loops { doTask1(); // do some stuff doTask2(); // do some stuff doTask3(); // do some stuff
// TODO: wait for user input for (say) 10 seconds. // TODO: if user gives input, break the loop. // TODO: if user gives no input in 10 seconds, continue the loop }
|
minor rant:
holy shit html emails are pain in the ass. gmail and outlook have different ways of handling this and i just want to blow my brains off
|
On April 23 2014 11:37 Sufficiency wrote:I want to do the following with Java. I don't look for great efficiency, rather the most painless solution. Here's basically what I want:
while (true) // because I like infinite loops { doTask1(); // do some stuff doTask2(); // do some stuff doTask3(); // do some stuff
// TODO: wait for user input for (say) 10 seconds. // TODO: if user gives input, break the loop. // TODO: if user gives no input in 10 seconds, continue the loop }
On April 23 2014 11:37 Sufficiency wrote:I want to do the following with Java. I don't look for great efficiency, rather the most painless solution. Here's basically what I want:
while (true) // because I like infinite loops { doTask1(); // do some stuff doTask2(); // do some stuff doTask3(); // do some stuff
// TODO: wait for user input for (say) 10 seconds. // TODO: if user gives input, break the loop. // TODO: if user gives no input in 10 seconds, continue the loop }
I want to say you're looking at something like this (replace the buffered reader from a process file with stdin):
http://stackoverflow.com/questions/13808792/program-hangs-when-trying-to-read-from-childs-process-stdout
However I didn't look into it that much, and I generally loathe doing any kind of stdin in java because it is nightmare inducing.
public Optional<Character> getChar() throws EVERYTHING { final BufferedReader input = new BufferedReader() // forgot how to get this to work with stdin
ExecutorService executor = Executors.newFixedThreadPool(2); Callable<Character> readTask = new Callable<Character>() { @Override public Character call() throws Exception { // This line is probably horribly unsafe, I dunno. return Character.valueOf((char)input.read()); } }; Future<Character> future = executor.submit(readTask); char readVal = null; try { readVal = future.get(10L, TimeUnit.SECONDS); } finally { executor.shutdownNow(); }
return Optional.fromNullable(readVal); }
That gives you a relatively expensive way to read a character from stdin, and it sort-of-kind-of doesn't block (at the very least, it doesn't burn CPU).
However this shit might not work at all if I'm forgetting something unique about java system.in behavior. Perhaps the static nature of that causes serious problems with the input.
Also you'd need to extend that to read whatever you want (if you want more than a single character, for example, you'd need to have the 10L you pass in be relative to whatever time you have left. You can use a Timer or Stopwatch for that).
Also you probably want to clean that code up to use ListenableFuture instead, I didn't look at the code to closely: https://code.google.com/p/guava-libraries/wiki/ListenableFutureExplained And finally it's late, so everything I just wrote might be total bullshit lol.
|
|
|
When overriding the equals method in Java,
public boolean equals(Object otherObject) /*to access I do something like obj1.equals(obj2) how would I then access obj1(@param Object) in the equals method?*/ //for example String first = (String)otherObject; //works String second = (String)Object; //fails
|
On April 23 2014 21:12 3FFA wrote:When overriding the equals method in Java, public boolean equals(Object otherObject) /*to access I do something like obj1.equals(obj2) how would I then access obj1(@param Object) in the equals method?*/ //for example String first = (String)otherObject; //works String second = (String)Object; //fails I think 'this' should do it?
|
On April 23 2014 21:26 Vorenius wrote:Show nested quote +On April 23 2014 21:12 3FFA wrote:When overriding the equals method in Java, public boolean equals(Object otherObject) /*to access I do something like obj1.equals(obj2) how would I then access obj1(@param Object) in the equals method?*/ //for example String first = (String)otherObject; //works String second = (String)Object; //fails I think 'this' should do it? No that doesn't work.
|
On April 23 2014 21:30 3FFA wrote:Show nested quote +On April 23 2014 21:26 Vorenius wrote:On April 23 2014 21:12 3FFA wrote:When overriding the equals method in Java, public boolean equals(Object otherObject) /*to access I do something like obj1.equals(obj2) how would I then access obj1(@param Object) in the equals method?*/ //for example String first = (String)otherObject; //works String second = (String)Object; //fails I think 'this' should do it? No that doesn't work.
String second = this; Note that declaring second is redundant and you can just use this wherever you were going to use second.
this should already be a String so you don't need to cast it as a String.
|
If you have Eclipse, just generate equals() from:
Source --> Generate equals() and hashCode()
|
On April 23 2014 23:55 Blitzkrieg0 wrote:Show nested quote +On April 23 2014 21:30 3FFA wrote:On April 23 2014 21:26 Vorenius wrote:On April 23 2014 21:12 3FFA wrote:When overriding the equals method in Java, public boolean equals(Object otherObject) /*to access I do something like obj1.equals(obj2) how would I then access obj1(@param Object) in the equals method?*/ //for example String first = (String)otherObject; //works String second = (String)Object; //fails I think 'this' should do it? No that doesn't work. String second = this; Note that declaring second is redundant and you can just use this wherever you were going to use second. this should already be a String so you don't need to cast it as a String. Eclipse says it is a syntactical bug. I'll post exactly what it says tomorrow, I don't have it with me atm.
|
To be fair, reading non-blocking-ish keyboard I/O in a lot of languages doesn't look super pretty (basically any time you need to fall back to callbacks it starts to look really ugly). And there's almost certainly a cleaner way to do that, given I half copy/pasted that from something and cooked it up while half asleep.
But yea, wish it was more like Go.
On April 23 2014 21:12 3FFA wrote:When overriding the equals method in Java, public boolean equals(Object otherObject) /*to access I do something like obj1.equals(obj2) how would I then access obj1(@param Object) in the equals method?*/ //for example String first = (String)otherObject; //works String second = (String)Object; //fails
@Override public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof MyClass)) { return false; } MyClass that = (MyClass) other; return Objects.equals(this.foo, that.foo) && Objects.equals(this.bar, that.bar) .... ; }
Also don't forget to override hashCode at the same time, otherwise hilarity ensues.
@Override public int hashCode() { return Objects.hashCode(foo, bar, ...); }
|
On April 23 2014 07:41 Fawkes wrote: Yeah Blisse, it was. I'm just so disappointed in myself, barely can even bring it up to people T.T It really sucks right now.
Graphics and modelling just ain't up my aisle. I took a course on graphics in Uni, barely passed it. I just can't visualize the transforms and fancy math that goes on behind it. I found many people writing little libraries here and there for Android related things, but I haven't really looked into Android development...maybe I could a little bit. The only real thing I know about it is that people usually choose Java for it.
Cool! I'm there now doing Software Engineering :3 Just finished 3A (fu feedback control systems...)
Did you mean the crazy Graphics Big 3 course? Because not doing well in that class isn't something to be ashamed of, it's one of the hardest courses in Waterloo (or so I've been told).
A lot of my friends do Android for a term and then tell me they're sick of it, so they get immediately out of mobile development. What does interest you?
|
Hi guys Android/Java question here. I managed to set onClickListeners for my AutocompleteTextViews (ACTV) by setting it in my Java code instead of my XML one. However, my issue now is that whenever I click on any of my ACTVs, I have to click it again before the onClickFunction fires successfully.
In my onCreate, I initialize my ACTVs as such:
brandACTV = (AutoCompleteTextView) findViewById(R.id.actvBrand); itemACTV = (AutoCompleteTextView) findViewById(R.id.actvItemName); partACTV = (AutoCompleteTextView) findViewById(R.id.actvPart); barcodeACTV = (AutoCompleteTextView) findViewById(R.id.actvBarcode); brandACTV.setOnClickListener(actvClicked); itemACTV.setOnClickListener(actvClicked); partACTV.setOnClickListener(actvClicked); barcodeACTV.setOnClickListener(actvClicked);
and I create my onClickListener function as such:
OnClickListener actvClicked = new OnClickListener(){ @Override public void onClick(View view){ switch (view.getId()) { case R.id.actvBrand: Log.d("Hi", "Brand pressed"); break; case R.id.actvItemName: Log.d("Hi", "Item name pressed"); break; case R.id.actvPart: Log.d("Hi", "Part pressed"); break; case R.id.actvBarcode: Log.d("Hi", "Barcode pressed"); break; } } };
Any ideas anyone?
|
No clue, except exactly what you trying to do with the OnClickListener? Are you sure you're not supposed to be using the OnItemClickListener?
|
On April 26 2014 03:35 Blisse wrote: No clue, except exactly what you trying to do with the OnClickListener? Are you sure you're not supposed to be using the OnItemClickListener?
What I want to do is that I'll check if the other ones have input. If so, I have to change the ArrayList of the selected one. For example, if Brand has input 3M then the user clicks on Item Name, I have to requery my database to look for all items with brand 3M and use those results instead of the one where I just give all the Item Names.
Wait, now that you mention it, I might be able to use the onItemClickListener.
|
On April 26 2014 09:35 SilverSkyLark wrote:Show nested quote +On April 26 2014 03:35 Blisse wrote: No clue, except exactly what you trying to do with the OnClickListener? Are you sure you're not supposed to be using the OnItemClickListener? What I want to do is that I'll check if the other ones have input. If so, I have to change the ArrayList of the selected one. For example, if Brand has input 3M then the user clicks on Item Name, I have to requery my database to look for all items with brand 3M and use those results instead of the one where I just give all the Item Names. Wait, now that you mention it, I might be able to use the onItemClickListener.
Hi - it's been a long time since I've even touched Android, but I thought I'd glance over the API docs to see. It seems that the Adapter you pass into "setAdapter" on a AutoCompleteTextView is supposed to implement the Filterable interface.
The AutoCompleteTextView actually will automatically ask the Adapter to filter it's result based on what you type in. You can set the threshold of actions for the filtering, but you shouldn't actually need to be listening for the ClickEvents yourself.
From my understanding, what you have is multiple of these AutoCompleteTextViews and you want the filtered result set of one to depend on the value you have in another. I think that the way to implement these is with customer adapters that implement Filterable, and count on AutoCompleteTextView to call these adapters to get the proper result set.
I made a small demo that shows how this works, but since you talk about talking to a database, you'll probably want to use CursorAdapters and the whole mess of setting up a ContentProvider. Damn all I remember that being was a lot of code, but felt super satisfying when it all worked. Also let's you hook up stuff like a SearchWidget and all.
This demo is a combination of the AutoCompleteTextView code on the Android site and some code I saw from SO.
Demo Activity Demo Layout
You can see that I never actually listen for a click event, but if you have selected a Country in the first text view, it will automatically provide autocomplete results in the second text view.
Also note that this example won't perform any further filtering on the second AutoCompleteTextView beyond limiting the suggestions to cities from that country. This is because as you see in the "performFiltering" method, I only check the value of the mCountriesAutoCompleteTextView, and never actually the value of the constraint. If you wanted to, you'd probably filter the result array but the values that startsWith constraint.
As an aside: if you're testing on Android and don't have a device, use genymotion! It's so useful - I wish I knew about it when I actually did Android stuff for one of my classes. It gives you an Android virtual device for VirtualBox and a plugin for Eclipse/Intellij to hook up adb and all.
Hope this helps a bit!
|
Thanks for the help guys. Actually I already felt that something was off when it takes 2 clicks for onClickListener to proc. i used onFocusChangeListener instead an it now works.
|
On April 26 2014 15:58 SilverSkyLark wrote: Thanks for the help guys. Actually I already felt that something was off when it takes 2 clicks for onClickListener to proc. i used onFocusChangeListener instead an it now works. That's unfortunate. I guess it's a little detail that you find when debugging.
|
On April 25 2014 17:49 SilverSkyLark wrote:Hi guys Android/Java question here. I managed to set onClickListeners for my AutocompleteTextViews (ACTV) by setting it in my Java code instead of my XML one. However, my issue now is that whenever I click on any of my ACTVs, I have to click it again before the onClickFunction fires successfully. In my onCreate, I initialize my ACTVs as such: brandACTV = (AutoCompleteTextView) findViewById(R.id.actvBrand); itemACTV = (AutoCompleteTextView) findViewById(R.id.actvItemName); partACTV = (AutoCompleteTextView) findViewById(R.id.actvPart); barcodeACTV = (AutoCompleteTextView) findViewById(R.id.actvBarcode); brandACTV.setOnClickListener(actvClicked); itemACTV.setOnClickListener(actvClicked); partACTV.setOnClickListener(actvClicked); barcodeACTV.setOnClickListener(actvClicked);
and I create my onClickListener function as such: OnClickListener actvClicked = new OnClickListener(){ @Override public void onClick(View view){ switch (view.getId()) { case R.id.actvBrand: Log.d("Hi", "Brand pressed"); break; case R.id.actvItemName: Log.d("Hi", "Item name pressed"); break; case R.id.actvPart: Log.d("Hi", "Part pressed"); break; case R.id.actvBarcode: Log.d("Hi", "Barcode pressed"); break; } } };
Any ideas anyone?
your code is wrong. You can't use it on a anonymous class, you need to create a private class. example from internet.
package com.example.android.accelerometerplay;
import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.os.Bundle; import android.widget.Button; import android.widget.Toast; import android.content.Context;
public class StudentFormsActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
// To specify the actions of the Buttons Button accept = (Button) findViewById(R.id.myButton1); Button reject = (Button) findViewById(R.id.myButton2);
accept.setOnClickListener(clickFunction); reject.setOnClickListener(clickFunction); }
private OnClickListener clickFunction = new OnClickClass();
private class OnClickClass implements OnClickListener{ public void onClick(View v){ Context context = getApplicationContext(); CharSequence text;
switch(v.getId()){ case R.id.myButton1: text="accept was pushed"; break; case R.id.myButton2: text="reject was pushed"; break; default: text="We didn't know what was pressed :("; }
int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context,text,duration); toast.show();
} } }
|
|
|
|
|
|