How to use Jackson to deserialise an array of objects

First create a mapper : import com.fasterxml.jackson.databind.ObjectMapper;// in play 2.3 ObjectMapper mapper = new ObjectMapper(); As Array: MyClass[] myObjects = mapper.readValue(json, MyClass[].class); As List: List<MyClass> myObjects = mapper.readValue(jsonInput, new TypeReference<List<MyClass>>(){}); Another way to specify the List type: List<MyClass> myObjects = mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, MyClass.class));

How to check if a String is numeric in Java

This is generally done with a simple user-defined function (i.e. Roll-your-own “isNumeric” function). Something like: public static boolean isNumeric(String str) { try { Double.parseDouble(str); return true; } catch(NumberFormatException e){ return false; } } However, if you’re calling this function a lot, and you expect many of the checks to fail due to not being a … Read more

How to create RecyclerView with multiple view types

Yes, it’s possible. Just implement getItemViewType(), and take care of the viewType parameter in onCreateViewHolder(). So you do something like: public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { class ViewHolder0 extends RecyclerView.ViewHolder { … public ViewHolder0(View itemView){ … } } class ViewHolder2 extends RecyclerView.ViewHolder { … public ViewHolder2(View itemView){ … } @Override public int getItemViewType(int position) { … Read more

Can’t start Eclipse – Java was started but returned exit code=13

Your version of Eclipse is 64-bit, based on the paths and filenames. However, the version of Java that it’s picking up is 32-bit, as indicated by where it is coming from, on this line: -vm C:\Program Files (x86)\Java\jre7\bin\javaw.exe Program Files (x86) is the folder where 64-bit Windows places 32-bit programs. Program Files is the folder … Read more

Reading a plain text file in Java

My favorite way to read a small file is to use a BufferedReader and a StringBuilder. It is very simple and to the point (though not particularly effective, but good enough for most cases): BufferedReader br = new BufferedReader(new FileReader(“file.txt”)); try { StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) … Read more

Why doesn’t RecyclerView have onItemClickListener()?

tl;dr 2016 Use RxJava and a PublishSubject to expose an Observable for the clicks. public class ReactiveAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { String[] mDataset = { “Data”, “In”, “Adapter” }; private final PublishSubject<String> onClickSubject = PublishSubject.create(); @Override public void onBindViewHolder(final ViewHolder holder, int position) { final String element = mDataset[position]; holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View … Read more