Create material design button via code - java

I'm trying to create material design buttons programmatically. (not via xml)
I'm trying to use the ButtonRectangle contractor and I get this error.
My code:
AttributeSet attr = new AttributeSet() {
#Override
public int getAttributeCount() {
return 0;
}
#Override
public String getAttributeName(int index) {
return null;
}
#Override
public String getAttributeValue(int index) {
return null;
}
#Override
public String getAttributeValue(String namespace, String name) {
return null;
}
#Override
public String getPositionDescription() {
return null;
}
#Override
public int getAttributeNameResource(int index) {
return 0;
}
#Override
public int getAttributeListValue(String namespace, String attribute, String[] options, int defaultValue) {
return 0;
}
#Override
public boolean getAttributeBooleanValue(String namespace, String attribute, boolean defaultValue) {
return false;
}
#Override
public int getAttributeResourceValue(String namespace, String attribute, int defaultValue) {
return 0;
}
#Override
public int getAttributeIntValue(String namespace, String attribute, int defaultValue) {
return 0;
}
#Override
public int getAttributeUnsignedIntValue(String namespace, String attribute, int defaultValue) {
return 0;
}
#Override
public float getAttributeFloatValue(String namespace, String attribute, float defaultValue) {
return 0;
}
#Override
public int getAttributeListValue(int index, String[] options, int defaultValue) {
return 0;
}
#Override
public boolean getAttributeBooleanValue(int index, boolean defaultValue) {
return false;
}
#Override
public int getAttributeResourceValue(int index, int defaultValue) {
return 0;
}
#Override
public int getAttributeIntValue(int index, int defaultValue) {
return 0;
}
#Override
public int getAttributeUnsignedIntValue(int index, int defaultValue) {
return 0;
}
#Override
public float getAttributeFloatValue(int index, float defaultValue) {
return 0;
}
#Override
public String getIdAttribute() {
return null;
}
#Override
public String getClassAttribute() {
return null;
}
#Override
public int getIdAttributeResourceValue(int defaultValue) {
return 0;
}
#Override
public int getStyleAttribute() {
return 0;
}
};
Context c = getApplicationContext();
ButtonRectangle btn = new ButtonRectangle(c, attr);
But I get this error:
java.lang.ClassCastException: maakesher.com.black.maakesher.psychoQuizzesActivity$1$1 cannot be cast to android.content.res.XmlBlock$Parser
Thanks in advance.

Related

Add items to ProtocolStringList

I am trying to add items to a ProtocolStringList.When I use the add function no error is returned,however when I try to print the list I get null as values.
I dont't know what I'm doing wrong.I tried printing the size to but that also prints 0.I can't figure out what to do.Please Help.Thank you.
Here is the code:
public class demo {
public static void main(String args[])
{
ProtocolStringList s=new ProtocolStringList() {
#Override
public List<ByteString> asByteStringList() {
return null;
}
#Override
public int size() {
return 0;
}
#Override
public boolean isEmpty() {
return false;
}
#Override
public boolean contains(Object o) {
return false;
}
#Override
public Iterator<String> iterator() {
return null;
}
#Override
public Object[] toArray() {
return new Object[0];
}
#Override
public <T> T[] toArray(T[] a) {
return null;
}
#Override
public boolean add(String s) {
return false;
}
#Override
public boolean remove(Object o) {
return false;
}
#Override
public boolean containsAll(Collection<?> c) {
return false;
}
#Override
public boolean addAll(Collection<? extends String> c) {
return false;
}
#Override
public boolean addAll(int index, Collection<? extends String> c) {
return false;
}
#Override
public boolean removeAll(Collection<?> c) {
return false;
}
#Override
public boolean retainAll(Collection<?> c) {
return false;
}
#Override
public void clear() {
}
#Override
public String get(int index) {
return null;
}
#Override
public String set(int index, String element) {
return null;
}
#Override
public void add(int index, String element) {
}
#Override
public String remove(int index) {
return null;
}
#Override
public int indexOf(Object o) {
return 0;
}
#Override
public int lastIndexOf(Object o) {
return 0;
}
#Override
public ListIterator<String> listIterator() {
return null;
}
#Override
public ListIterator<String> listIterator(int index) {
return null;
}
#Override
public List<String> subList(int fromIndex, int toIndex) {
return null;
}
};
s.add("a");
s.add("b");
for(int i=0;i<2;i++)
{
System.out.println(s.get(i));//prints null
}
}
}
to add item to your your list in Java you need to use a Builder and build the object.
You have an example on the reference guide:
https://developers.google.com/protocol-buffers/docs/javatutorial
Person john =
Person.newBuilder()
.setId(1234)
.setName("John Doe")
.setEmail("jdoe#example.com")
.addPhones(
Person.PhoneNumber.newBuilder()
.setNumber("555-4321")
.setType(Person.PhoneType.HOME))
.build();
In your example, your code should look something like:
s = ProtocolStringList.newBuilder()
.add("a")
.add("b")
.build();
ProtocolStringList is an Interface (like List). You need to instantiate implemented Data Structure to add value or use its functionality.
Example
ProtocolStringList plist = new LazyStringArrayList();
plist.add("E");

Custom Object intent Parcelable element arraylist

This code works but I do not pass the data contained in the arrayList.
public class Percorso implements Parcelable {
int id;
String nome;
double distanza;
int numero_commenti;
double valutazione;
ArrayList<Commento> commenti;
ArrayList<Cordinate> cordinate;
public Percorso(){
}
protected Percorso(Parcel in) {
id = in.readInt();
nome = in.readString();
distanza = in.readDouble();
numero_commenti = in.readInt();
valutazione = in.readDouble();
}
public static final Creator<Percorso> CREATOR = new Creator<Percorso>() {
#Override
public Percorso createFromParcel(Parcel in) {
return new Percorso(in);
}
#Override
public Percorso[] newArray(int size) {
return new Percorso[size];
}
};
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public double getDistanza() {
return distanza;
}
public void setDistanza(double distanza) {
this.distanza = distanza;
}
public int getNumero_commenti() {
return numero_commenti;
}
public void setNumero_commenti(int numero_commenti) {
this.numero_commenti = numero_commenti;
}
public double getValutazione() {
return valutazione;
}
public void setValutazione(double valutazione) {
this.valutazione = valutazione;
}
public ArrayList<Commento> getCommenti() {
return commenti;
}
public void setCommenti(ArrayList<Commento> commenti) {
this.commenti = commenti;
}
public ArrayList<Cordinate> getCordinate() {
return cordinate;
}
public void setCordinate(ArrayList<Cordinate> cordinate) {
this.cordinate = cordinate;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(id);
parcel.writeString(nome);
parcel.writeDouble(distanza);
parcel.writeInt(numero_commenti);
parcel.writeDouble(valutazione);
}
}
//
public class Cordinate implements Parcelable {
double latitudine;
double longitudin;
public Cordinate(){}
protected Cordinate(Parcel in) {
latitudine = in.readDouble();
longitudin = in.readDouble();
}
public static final Creator<Cordinate> CREATOR = new Creator<Cordinate>() {
#Override
public Cordinate createFromParcel(Parcel in) {
return new Cordinate(in);
}
#Override
public Cordinate[] newArray(int size) {
return new Cordinate[size];
}
};
public double getLatitudine() {
return latitudine;
}
public void setLatitudine(double latitudine) {
this.latitudine = latitudine;
}
public double getLongitudin() {
return longitudin;
}
public void setLongitudin(double longitudin) {
this.longitudin = longitudin;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeDouble(latitudine);
parcel.writeDouble(longitudin);
}
}
INTENT
Percorso percorsoClick = listaPercorsi.get(i);
Intent intent = new Intent(getBaseContext(), PercorsoActivity.class);
intent.putExtra("percorso", percorsoClick);
startActivity(intent);
GET INTENT
percorso = getIntent().getParcelableExtra("percorso");
System.out.println(percorso.getNome());
getNome() work because is not a arrayList.
instead get getCordinate not work.
percorso.getCordinate().size() // null
I hope it was clear. I have set the Parcelable implementation on all classes.
Thank you very much for the help
ArrayList<Cordinate> cordinate; is not being parcelled. You still have to take care of write/read in/from Parcel parcel. Eg
protected Percorso(Parcel in) {
id = in.readInt();
nome = in.readString();
distanza = in.readDouble();
numero_commenti = in.readInt();
valutazione = in.readDouble();
cordinate = new ArrayList<Cordinate>();
in.readList(cordinate,Cordinate.class.getClassLoader());
}
and
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(id);
parcel.writeString(nome);
parcel.writeDouble(distanza);
parcel.writeInt(numero_commenti);
parcel.writeDouble(valutazione);
parcel.writeList(cordinate);
}

How to set parameter List in retrofit2

I get problem about set parameter in retrofit2
I would like to set group_id and user_id
public class ChatRequest {
private String group_id;
private List<Userbean> users;
public String getGroup_id() {
return group_id;
}
public void setGroup_id(String group_id) {
this.group_id = group_id;
}
public List<Userbean> getUsers() {
return users;
}
public void setUsers(List<Userbean> users) {
this.users = users;
}
public static class Userbean {
private int user_id;
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
}
}
and my retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
ChatRequest chatRequest = new ChatRequest();
chatRequest.setGroup_id("dsdfjlds9u");
chatRequest.setUsers ///// How to set user_id = 8(int)
How to set user_id = 8 ??
I have no idea to set it
thanks for your help!
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
ChatRequest chatRequest = new ChatRequest();
chatRequest.setGroup_id("dsdfjlds9u");
UserBean userBean = new UserBean();
userBean.setUser_Id(8);
chatRequest.setUsers(Collections.singletonList(userBean));
Steps Need to follow to set user_id
* Need to initialize a object for ChatRequest
* Declare the list which contains Userbean class as params
* Initialize the Userbean class's object
* Set the user id as 8 using setter class
* Add the user to Userbean list
* Set the Userbean list to ChatRequest
int user_id=8;
ChatRequest chatRequest = new ChatRequest();
List<ChatRequest.Userbean> userbeanList =new List<ChatRequest.Userbean>() {
#Override
public int size() {
return 0;
}
#Override
public boolean isEmpty() {
return false;
}
#Override
public boolean contains(Object o) {
return false;
}
#NonNull
#Override
public Iterator<ChatRequest.Userbean> iterator() {
return null;
}
#NonNull
#Override
public Object[] toArray() {
return new Object[0];
}
#NonNull
#Override
public <T> T[] toArray(#NonNull T[] ts) {
return null;
}
#Override
public boolean add(ChatRequest.Userbean userbean) {
return false;
}
#Override
public boolean remove(Object o) {
return false;
}
#Override
public boolean containsAll(#NonNull Collection<?> collection) {
return false;
}
#Override
public boolean addAll(#NonNull Collection<? extends ChatRequest.Userbean> collection) {
return false;
}
#Override
public boolean addAll(int i, #NonNull Collection<? extends ChatRequest.Userbean> collection) {
return false;
}
#Override
public boolean removeAll(#NonNull Collection<?> collection) {
return false;
}
#Override
public boolean retainAll(#NonNull Collection<?> collection) {
return false;
}
#Override
public void clear() {
}
#Override
public ChatRequest.Userbean get(int i) {
return null;
}
#Override
public ChatRequest.Userbean set(int i, ChatRequest.Userbean userbean) {
return null;
}
#Override
public void add(int i, ChatRequest.Userbean userbean) {
}
#Override
public ChatRequest.Userbean remove(int i) {
return null;
}
#Override
public int indexOf(Object o) {
return 0;
}
#Override
public int lastIndexOf(Object o) {
return 0;
}
#Override
public ListIterator<ChatRequest.Userbean> listIterator() {
return null;
}
#NonNull
#Override
public ListIterator<ChatRequest.Userbean> listIterator(int i) {
return null;
}
#NonNull
#Override
public List<ChatRequest.Userbean> subList(int i, int i1) {
return null;
}
};
ChatRequest.Userbean userBean=new ChatRequest.Userbean();
userBean.setUser_id(user_id);
userbeanList.add(userBean);
chatRequest.setUsers(userbeanList);
Log.e("Users",chatRequest.getUsers().toString());
Log.e("Users id",String.valueOf(userBean.getUser_id()));

TreeView doesn't displays items

I'm trying to set an initial input to a treeview but I must forget something because It does not displays anything.
public class TreeEditor extends OperationDetailsAspectEditor {
private TreeViewer fConditionField;
private BooleanOperator root;
#Override
public void createControl(Composite parent, FormToolkit toolkit) {
createConditionControls(parent, toolkit);
}
private void createConditionControls(final Composite parent, FormToolkit toolkit) {
fConditionField = new TreeViewer(parent);
fConditionField.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 5));
fConditionField.setLabelProvider(new ConditionsLabelProvider());
fConditionField.setContentProvider(new ConditionsContentProvider());
fConditionField.setUseHashlookup(true);
fConditionField.setInput(getInitialTreeConditions());
fConditionField.expandAll();
}
private BooleanOperator getInitialTreeConditions(){
root = new BooleanOperator(Operator.AND);
return root;
}
}
public class ConditionsLabelProvider implements ILabelProvider {
#Override
public void addListener(ILabelProviderListener paramILabelProviderListener) {}
#Override
public void dispose() { }
#Override
public boolean isLabelProperty(Object paramObject, String paramString) {return false;}
#Override
public void removeListener(
ILabelProviderListener paramILabelProviderListener) {}
#Override
public Image getImage(Object paramObject) {return null;}
#Override
public String getText(Object element) {
if(element instanceof Model){
return ((Model) element).getDisplayName();
} else {
throw unknownElement(element);
}
}
protected RuntimeException unknownElement(Object element) {
return new RuntimeException("Unknown type of element in tree of type " + element.getClass().getName());
}
}
public class ConditionsContentProvider implements ITreeContentProvider {
private static Object[] EMPTY_ARRAY = new Object[0];
protected TreeViewer viewer;
#Override
public void dispose() {
}
#Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
this.viewer = (TreeViewer) viewer;
}
#Override
public Object[] getElements(Object inputElement) {
return getChildren(inputElement);
}
#Override
public Object[] getChildren(Object parentElement) {
if(parentElement instanceof BooleanOperator){
BooleanOperator operator = ((BooleanOperator) parentElement);
return concat(operator.getOperators().toArray(),
operator.getExpresions().toArray());
}
return EMPTY_ARRAY;
}
#Override
public Object getParent(Object element) {
if(element instanceof Model) {
return ((Model)element).getParent();
}
return null;
}
#Override
public boolean hasChildren(Object element) {
return getChildren(element).length > 0;
}
protected Object[] concat(Object[] a, Object[] b) {
int aLen = a.length;
int bLen = b.length;
Object[] c = new Object[aLen + bLen];
System.arraycopy(a, 0, c, 0, aLen);
System.arraycopy(b, 0, c, aLen, bLen);
return c;
}
protected Object[] concat(Object[]... arrays) {
Object[] array = arrays[0] != null? arrays[0] : null;
for(int i = 1; i < arrays.length; i++) {
array = concat(arrays[i-1], arrays[i]);
}
return array;
}
}
Models
public abstract class Model {
protected BooleanOperator parent;
protected String displayName;
public String getDisplayName(){ return displayName; }
public void setDisplayName(String displayName){
this.displayName = displayName;
}
public BooleanOperator getParent() { return parent; }
public void setParent(BooleanOperator parent) { this.parent = parent; }
public Model(String displayName) { this.displayName = displayName; }
public Model(){}
}
public class BooleanOperator extends Model {
private Operator operator;
private List<BooleanOperator> operators;
private List<Expression> expressions;
public Operator getOperator() {
return operator;
}
public List<BooleanOperator> getOperators() {
return operators;
}
public List<Expresion> getExpresions() {
return expresions;
}
public BooleanOperator() {
operators = new ArrayList<BooleanOperator>();
expresions = new ArrayList<Expresion>();
}
public BooleanOperator(Operator operator) {
this();
this.operator = operator;
this.displayName = operator.toString();
}
}
The idea is that Tree contains something such as:
AND
|- Attribute_1 == value_1
|- Attribute_2 == value_2
|- OR
|- Attribute_3 == value_3
The nodes are Boolean operators and the leaves are expressions.

java jlist - AbstractListModel - fireContentsChanged does not work properly

I have a simple ListModel, that is filterable and is used in a JList...
It uses the following code...
public class FilteredListModel extends AbstractListModel
{
private List<LineData> data = null;
private final ArrayList<Integer> indices = new ArrayList<Integer>();
public FilteredListModel()
{
}
public void setData(List<LineData> data)
{
this.data = data;
doFilter();
}
public void doFilter()
{
int oldSize = indices.size();
indices.clear();
if (data != null)
{
int count = data.size();
for (int i = 0; i < count; i++)
{
IFiltererListObject element = (IFiltererListObject) data.get(i);
if (element.isVisible())
indices.add(i);
}
}
fireContentsChanged(this, 0, getSize() - 1);
if (oldSize > getSize())
fireIntervalRemoved(this, getSize(), oldSize - 1);
}
#Override
public int getSize()
{
return indices.size();
}
#Override
public Object getElementAt(int index)
{
return data.get(indices.get(index));
}
#Override
public void addListDataListener(ListDataListener l)
{
// TODO Auto-generated method stub
//doFilter();
}
#Override
public void removeListDataListener(ListDataListener l)
{
// TODO Auto-generated method stub
//doFilter();
}
}
The strange thing about it is, that it is not working, just if I click for example outside the window, the JList with the ListModel get's correctly updated...
What am I missing here?
The problem is that the addListDataListener and removeListDataListener methods are empty. This means the JList can no longer attach its listener to the model. The call fireContentsChanged will do nothing, as the super class isn't aware of any listeners.
Either do not override those methods, or make sure you call super.addListDataListener as well.
#Robin please DYM???
import java.util.ArrayList;
import javax.swing.AbstractListModel;
import javax.swing.MutableComboBoxModel;
//usage == new JComboBox(new SectionComboBoxModel(new ArrayList());
public class SectionComboBoxModel extends AbstractListModel implements MutableComboBoxModel {
private static final long serialVersionUID = 1L;
private Object selectedItem;
private ArrayList<Object> sections;
public SectionComboBoxModel(ArrayList<Object> arrayList) {
sections = arrayList;
}
#Override
public Object getSelectedItem() {
return selectedItem;
}
#Override
public void setSelectedItem(Object newValue) {
selectedItem = newValue;
}
#Override
public int getSize() {
return sections.size();
}
#Override
public Object getElementAt(int i) {
return sections.get(i);
}
public void setElementAt(Object newValue, int i) {
this.fireContentsChanged(newValue, i, i);
this.sections.set(i, newValue);
}
#Override
public void addElement(Object obj) {
sections.add(obj);
this.fireIntervalAdded(obj, this.getSize() - 1, this.getSize() - 1);
}
#Override
public void removeElement(Object obj) {
this.fireIntervalRemoved(obj, sections.indexOf(obj), sections.indexOf(obj));
sections.remove(obj);
}
#Override
public void insertElementAt(Object obj, int index) {
sections.add(index, obj);
this.fireIntervalAdded(obj, index, index);
}
#Override
public void removeElementAt(int index) {
this.fireIntervalRemoved(sections.get(index), index, index);
sections.remove(index);
}
public void print() {
System.out.println("\nPrinting List");
for (int i = 0; i < this.sections.size(); i++) {
System.out.println(this.sections.get(i));
}
}
public boolean contains(Object o) {
return sections.contains(o);
}
public Object[] toArray() {
return this.sections.toArray();
}
}

Categories

Resources