How to auto-complete an object instantiation in Android Studio? - intellij-idea

For example, when I type ArrayList<String> data =, how can I auto-complete this so that it shows ArrayList<String> data = new ArrayList<>();?
PS: I do remember there is a way to do so in eclipse, but I don't know how to do it in android studio.

Type Object obj = new and then
Ctrl+Shift+Space bar
Example:
String str = new (ctrl+shift+space)
will give you
String str = new String();
For Objects that have paramters it will show suggestion as in while creating Collection objects like List<E> and ArrayList<E> like,
Reference: Here is a full list of Key map for Windows

Assuming you have the default keymap, it's Ctrl + Shift + A
Reference:
https://developer.android.com/sdk/installing/studio-tips.html
http://www.jetbrains.com/idea/documentation/index.jsp

Related

Replacing Type with var for all 'Class class = new Class()' usages in Java project

I recently switched to Java 11 for a rather big project, and would like to switch to using var class = new Class() instead of Class class = new CLass().
I tried using Intellij Structural Search (and replace) for this, but its turning out to be more complex than expected.
My first attempt was $Type$ $Inst$ = new $Constructor$($Argument$);, which also matches global variables (which don't allow var).
My second attempt is:
class $Class$ {
$ReturnType$ $Method$ ($ParameterType$ $Parameter$) throws $ExceptionType$ {
$Statements$;
final $Type$ $Inst$ = new $Constructor$($Argument$);
$Statements2$;
}
}
Which misses all calls inside e.g. try blocks (since they get matched by the expressions)
Any help would be greatly appreciated!
Use your first template
$Type$ $Inst$ = new $Constructor$($Argument$);
But add a Script modifier on the $Inst$ variable with the following text:
Inst instanceof com.intellij.psi.PsiLocalVariable
Alternatively you may want to try the Local variable type can be omitted inspection that is available in IntelliJ IDEA.

Is it possible to add selector text to a By object

I am wondering if it is possible to add selector text to an existing By object.
That is, suppose I have a popup with a class name ".popup". But then I want to be a little more specific as the webapp is utilized. Now, our popup has an added class name ".cart". My new selector wants to be ".popup.cart". Here is what I have. Is there a better or more efficient way to do this? (I'm coding in C#, but it can be relevant to any language.)
By selector= By.CssSelector("div.popup");
selector = By.CssSelector(
selector.ToString().Remove(0, selector.ToString().LastIndexOf(":") + 1)
+ ".cart");
What is going on above, selector becomes "div.popup".
selector.ToString() returns "By.CssSelector: div.popup"
So I remove all text from the beginning to the last ":" and add the remaining text to ".cart".
selector then becomes "div.popup.cart", which is what I want.
The problem comes when I try to combine any selectors that have been "ByChained." The ToString() method will return By.Chained([By.CssSelector: .popup, By.CssSelector: .title])
If it helps, I am also Sizzle. So, if there's a JQuery way to do this, I'm open.
Put the "div.popup" in a string say "str" like this:-
String str = div.popup
Then you can append ".cart" to the string, make it "div.popup.cart", and use the cssSelector as:
By selector= By.cssSelector(str+".cart");
Note:- Above is a java code.
As mentioned above, I'm basically solving this by unrolling the By.CssSelector().ToString() output and/or the ByChained().ToString() output. There are some caveats. Mainly that the last selector in a ByChained() must be of type By.CssSelector (since I'm adding a JQuery pseudo class):
private static By AddPseudoFunction(By selector, string pseudoFunc)
{
string type = selector.GetType().ToString();
string selectorValue = selector.ToString();
if (type.Contains("Chained"))
{
selectorValue = selectorValue.Remove(0, selector.ToString().IndexOf("[") + 1);
int start = selectorValue.LastIndexOf("]");
int delta = selectorValue.Length - start;
selectorValue = selectorValue.Remove(start, delta);
}
StringBuilder builder = new StringBuilder();
foreach (string s in selectorValue.Split(','))
{
builder.Append(" ");
builder.Append(s.ToString().Remove(0, s.ToString().LastIndexOf(":") + 2));
}
selectorValue = builder.ToString().TrimStart(' ') + pseudoFunc;
return new ByJQuery.ByJQuerySelector(selectorValue, true);
}
Of course, if you want to make this so that it will return a By.CssSelector, just replace the return with:
return By.CssSelector(selctorValue + addSelector);
Where addSelector would be a valid CSS Selector value (like what I describe in the question).

Using dynamic types with expresso

I would like to use a dynamic value as a parameter.
E.g.
dynamic dyn = new ExpandoObject();
dyn.Foo = "bar";
var bar = new Interpreter().Eval("d.Foo", new Parameter("d", dyn));
Assert.AreEqual("bar", bar.ToString());
But I get an error saying "No property or field 'Foo' exists in type 'ExpandoObject'" ?
Is this supposed to be possible?
Regards, Niels
Unfortunately for now dynamics (ExpandoObject) are not supported. I will consider this feature for the next release.
A possible workaround is to use anonymous objects:
dynamic dyn = new ExpandoObject();
dyn.Foo = "bar";
var bar = new Interpreter().Eval("d.Foo", new Parameter("d", new { Foo = dyn.Foo }));
Consider that in this case the property is evaluated when you create the parameter.
You can also convert a dynamic into an anonymous type (see Cast ExpandoObject to anonymous type) but the result is not very different.
Disclaimer: I'm the creator of Dynamic Expresso library.
Expression Evaluator supports dynamics (ExpandoObject). It supports method calls, property and index accessors, get and set. If you do encounter an error with dynamics please let me know as dynamics is relatively newly supported.

Creating new smartform data using Ektron ContentTypes

Ektron 8.0.1 SP1
I am using SmartForms and Content Types to read (and hopefully write) data. I can read data but now I am attempting to write a new record similar to the following.
ContentTypeManager<member> contentTypeManager = new ContentTypeManager<member>();
ContentType<member> newmem = new ContentType<member>();
newmem.SmartForm.details.field1 = "Chuck"; // This line throws 'Object reference not set to an instance of an object.' error
newmem.SmartForm.details.field2 = "Norris";
contentTypeManager.Update(newmem);
I get the error "Object reference not set to an instance of an object." for that first assignment line. What am I missing?
I am having trouble finding good documentation on ContentTypes for 8.0.1 now that the Ektron website has been redesigned.
Thx.
Thanks for clarifying, to ADD content to a folder that has a smartform assigned to it, the basic code block should get you started: (Note: the Html attribute of the content is simply the xml matched to the schema you created)
Ektron.Cms.Framework.Content.ContentManager cmanager = new Cms.Framework.Content.ContentManager();
Ektron.Cms.ContentData cdata = new ContentData();
cdata.FolderId = 0;
cdata.XmlConfiguration.Id = 0; //SMARTFORM ID HERE
cdata.Html = "<root><field1>field1 value</field1><field2>field2 value</field2></root>";
cmanager.Add(cdata);
You could update ContentTypes.cs to include an Add method. Just copy the Update method and change contentManager.Update to contentManager.Add.
public void Add(ContentType<T> contentType)
{
Initialize();
contentType.Content.Html = Ektron.Cms.EkXml.Serialize(typeof(T), contentType.SmartForm);
contentManager.Add(contentType.Content);
}
Unfortunately, contentManager.Add returns void. Ideally it should return the new content ID.

Problem saving a new item to custom SharePoint 2007 list using Silverlight 4 UI control

I am hoping someone might be able to help me. I'm working on a Contact Manager built using a custom SharePoint 2007 list with a Silverlight 4 UI embedded in a content editor web part.
I am currently able to retrieve the data from the list and display it in a datagrid on the UI and everything works well.
Now I am trying to add the the ability to add new items to the list using the following code but the items do not save.
I've remotely debugged the following code using the Debug -> Attach to Process option and everything seems to execute successful without any errors but it does not save the item to SharePoint.
In order to simplify and get a working insert function I changed all the SharePoint fieds to single line text with the exception of the notes (multiline) and none of the fileds are required.
The sharepoint site does require Windows authentication but it seems to be working correctly as I am able to display it as well as add new items manually using the standard SharePoint forms.
Lastly, I have added the xml for the Batch element at the bottom which I copied as output while debuging.
Please let me know if there is any additional information I might be missing.
Thanks in advance for any assistance you might be willing to provide.
Charles
public string sharepoint_soap_namespace = "http://schemas.microsoft.com/sharepoint/soap/";
public string sharepoint_rowset_namespace = "#RowsetSchema";
public string service_lists_url = "http://myDomain/_vti_bin/lists.asmx";
public string listName = "MyContacts";
public void TestCreateContact()
{
Uri serviceUri = new Uri(service_lists_url);
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
binding.MaxReceivedMessageSize = 2147483647; // This has to be the same as in the ServiceReferences.ClientConfig file.
EndpointAddress endpoint = new EndpointAddress(serviceUri);
ListsSoapClient testCreateClient = new ListsSoapClient(binding, endpoint);
XElement batch = new XElement("batch",
new XElement("Method",
new XAttribute("ID", "1"),
new XAttribute("Cmd", "New"),
CreateFieldElement("ows_ID", "New"),
CreateFieldElement("ows_Title", "John"),
CreateFieldElement("ows_SupportFor","USA"),
CreateFieldElement("ows_LastName","Doe")
));
testCreateClient.UpdateListItemsCompleted +=
new EventHandler<UpdateListItemsCompletedEventArgs>(createSoapClient_UpdateListItemsCompletedEventArgs);
testCreateClient.UpdateListItemsAsync(listName, batch);
testCreateClient.CloseAsync();
}
private XElement CreateFieldElement(string fieldName, string fieldValue)
{
XElement element = new XElement("Field",
new XAttribute("Name", fieldName),
fieldValue);
return element;
}
Just a quick update to let everyone know I was able to answer my own question.
It seems that in the batch XElement I was using the wrong field names.
CreateFieldElement("ows_SupportFor","USA"),
I was using "ows_SupportFor" instead of "SupportFor" without the "ows_" prefix.
Cheers,
Charles