I created a button to save two BigInteger numbers to file, I wrote code as below, but when I click the button "save test", it can't write, anyone can give me any advice?
public final static BigInteger numberN = new BigInteger("17855404456822874936847712749012221703363767319600747738346509047988764141076203590602575545059230348630206930110136685391297486715740834967023383072460701006180246289301165056178499131397684281330821144935962852141623402858240677770202677489833679398319826337183170013049969533314399531024754572058094731892694381452545462801033432474024224220241635573572538943432828608673126747833227351200161776312900643626121058023519446087845640534470254939863451117935546083448482346472288049441087336233290409696349030267736371540043446170340719650007973597139760511408444411001327893735548336735821557222262768663537785969181");
public final static BigInteger numberE = new BigInteger("3674911");
private void savetestActionPerformed(java.awt.event.ActionEvent evt) {
File file=new File("E:\testghikey.txt");
key a = new key();
a.setName("Number N:");
a.setNumber(numberN);
key b = new key();
b.setName("Number E");
b.setNumber(numberE);
try {
FileOutputStream f = new FileOutputStream(file);
ObjectOutputStream oOT = new ObjectOutputStream(f);
oOT.writeObject(a); // Ghi Object là đối tượng a xuống file
oOT.writeObject(b);
oOT.close();
f.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Can not write the data to file "+ file.getName(),"Error",JOptionPane.ERROR_MESSAGE);
}
}
public class key implements Serializable {
private String Name;
private BigInteger Number;
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public BigInteger getNumber() {
return Number;
}
public void setNumber(BigInteger Number) {
this.Number = Number;
}
}
Related
This Is Main Fragment
Fragment:
private void getStock() {
dialog.show();
Retrofit retrofit = RetrofitClient.getRetrofitInstance();
apiInterface api = retrofit.create(apiInterface.class);
Call<List<Blocks>>call = api.getVaccineBlocks();
call.enqueue(new Callback<List<Blocks>>() {
#Override
public void onResponse(Call<List<Blocks>>call, Response<List<Blocks>> response) {
if (response.code() == 200) {
block = response.body();
spinnerada();
dialog.cancel();
}else{
dialog.cancel();
}
}
#Override
public void onFailure(Call<List<Blocks>> call, Throwable t) {
dialog.cancel();
}
});
}
private void spinnerada() {
String[] s = new String[block.size()];
for (int i = 0; i < block.size(); i++) {
s[i] = block.get(i).getBlockName();
final ArrayAdapter a = new ArrayAdapter(getContext(), android.R.layout.simple_spinner_item, s);
a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spinner.setAdapter(a);
}
}
This Is Blocks Model
model:
package com.smmtn.book.models;
import java.io.Serializable;
public class Blocks implements Serializable {
public String id;
public String blockName;
public String blockSlug;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBlockName() {
return blockName;
}
public void setBlockName(String blockName) {
this.blockName = blockName;
}
public String getBlockSlug() {
return blockSlug;
}
public void setBlockSlug(String blockSlug) {
this.blockSlug = blockSlug;
}
}
here i need onitemclick with blockslug please any one can help, am new to android so i need some example.when on click i want take blockslug and load another method with that blockslug,like will get data from u "http://example.com/block/"+blockslug
i want to get blockslug from selected block
i hope guys i will get help
and sorry for my bad English,
First of all, you need to implement setOnItemSelectedListener. Refer to this https://stackoverflow.com/a/20151596/9346054
Once you selected the item, you can call them by making a new method. Example like below
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
final String itemSelected = parent.getItemAtPosition(pos).toString();
showBlockSlug(itemSelected);
}
And then, at the method showBlockSlug() , you can call Retrofit.
private void showBlockSlug(final String blockslug){
final String url = "http://example.com/block/"+ blockslug;
//Do your stuff...
}
I generated xml through jaxb , but the problem is when the data is empty then the rendered node is <node />. I want it to be <node></node>
here is my codes :
#XmlRootElement(name = "record")
public class ReglementXMLBean {
private String CODE_FOUR;
private String NUM_FACT;
private String FACT_FOU;
private String DTE_REG;
private String REF_REG;
private String MODE_REG;
private String MT_REG_DEV;
private String MT_REG;
private String DEVISE;
private String TYPE_REG;
public String getCODE_FOUR() {
return CODE_FOUR;
}
#XmlElement
public void setCODE_FOUR(String cODE_FOUR) {
CODE_FOUR = cODE_FOUR;
}
public String getNUM_FACT() {
return NUM_FACT;
}
#XmlElement
public void setNUM_FACT(String nUM_FACT) {
NUM_FACT = nUM_FACT;
}
public String getFACT_FOU() {
return FACT_FOU;
}
#XmlElement
public void setFACT_FOU(String fACT_FOU) {
FACT_FOU = fACT_FOU;
}
public String getDTE_REG() {
return DTE_REG;
}
#XmlElement
public void setDTE_REG(String dTE_REG) {
DTE_REG = dTE_REG;
}
public String getREF_REG() {
return REF_REG;
}
#XmlElement
public void setREF_REG(String rEF_REG) {
REF_REG = rEF_REG;
}
public String getMODE_REG() {
return MODE_REG;
}
#XmlElement
public void setMODE_REG(String mODE_REG) {
MODE_REG = mODE_REG;
}
public String getMT_REG_DEV() {
return MT_REG_DEV;
}
#XmlElement
public void setMT_REG_DEV(String mT_REG_DEV) {
MT_REG_DEV = mT_REG_DEV;
}
public String getMT_REG() {
return MT_REG;
}
#XmlElement
public void setMT_REG(String mT_REG) {
MT_REG = mT_REG;
}
public String getDEVISE() {
return DEVISE;
}
#XmlElement
public void setDEVISE(String dEVISE) {
DEVISE = dEVISE;
}
public String getTYPE_REG() {
return TYPE_REG;
}
#XmlElement
public void setTYPE_REG(String tYPE_REG) {
TYPE_REG = tYPE_REG;
}
}
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
ReglementXMLBean reglementXMLBean = new ReglementXMLBean();
while ((line = br.readLine()) != null) {
String[] colonnes = line.split(cvsSplitBy);
reglementXMLBean.setCODE_FOUR(colonnes[0]);
reglementXMLBean.setNUM_FACT(colonnes[1]);
reglementXMLBean.setFACT_FOU(colonnes[2]);
reglementXMLBean.setDTE_REG(colonnes[3]);
reglementXMLBean.setREF_REG(colonnes[4]);
reglementXMLBean.setMODE_REG(colonnes[5]);
reglementXMLBean.setMT_REG_DEV(colonnes[6]);
reglementXMLBean.setMT_REG(colonnes[7]);
reglementXMLBean.setDEVISE(colonnes[8]);
reglementXMLBean.setTYPE_REG(colonnes[9]);
try {
JAXBContext jaxbContext = JAXBContext.newInstance(ReglementXMLBean.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(reglementXMLBean, out);
jaxbMarshaller.marshal(reglementXMLBean, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
long story short, I have one settlementItemeBase class as my father and two children. I want to make the father class parcelable so as extension happens, my two child classes be parcelable as well. I don't exactly know what I'm doing right or wrong. I searched little bit but nothing helped me.
here are my classes:
SettlementItemBase:
public class SettlementItemBase implements Parcelable{
public SettlementItemBase(){}
protected SettlementItemBase(Parcel in) {
}
public static final Creator<SettlementItemBase> CREATOR = new Creator<SettlementItemBase>() {
#Override
public SettlementItemBase createFromParcel(Parcel in) {
return new SettlementItemBase(in);
}
#Override
public SettlementItemBase[] newArray(int size) {
return new SettlementItemBase[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
}
}
first child class:
public class FirstClass extends SettlementItemBase {
private int id;
private int cardId;
private String cardNumber;
private String expDate;
private String currency;
private String url;
public FirstClass(){
id = 0;
cardId = 0;
cardNumber = "";
expDate = "";
currency = "";
url = "";
}
protected FirstClass(Parcel in) {
super(in);
id = in.readInt();
cardId = in.readInt();
cardNumber = in.readString();
expDate = in.readString();
currency = in.readString();
url = in.readString();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeInt(cardId);
dest.writeString(cardNumber);
dest.writeString(expDate);
dest.writeString(currency);
dest.writeString(url);
}
public int getId() {
return id;
}
public int getCardId() {
return cardId;
}
public String getCardNumber() {
return cardNumber;
}
public String getExpDate() {
return expDate;
}
public String getCurrency() {
return currency;
}
public String getUrl() {
return url;
}
}
second child class:
public class SecondClass extends SettlementItemBase{
private int id;
private String currency;
private String accountNumber;
private String ibanNumber;
public SecondClass(int id, String currency,
String accountNumber, String ibanNumber){
this.id = id;
this.currency = currency;
this.accountNumber = accountNumber;
this.ibanNumber = ibanNumber;
}
protected SecondClass(Parcel in){
super(in);
id = in.readInt();
currency = in.readString();
accountNumber = in.readString();
ibanNumber = in.readString();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(currency);
dest.writeString(accountNumber);
dest.writeString(ibanNumber);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCurrency() {
return currency;
}
public String getAccountNumber() {
return accountNumber;
}
public String getIbanNumber() {
return ibanNumber;
}
}
I'm passing and getting an ArrayList of both child class' items with intent putParcelableArrayListExtra and getParcelableArrayListExtra methods and get the following error:
Parcel android.os.Parcel#2d0fde33: Unmarshalling unknown type code 3276849 at offset 172
any help would be appreciated 3>
well I'm posting this for those who may run into the same problem as me.
the problem was solved after adding the Creator statement to the child classes.
for one of the child classes it would be like:
public static final Creator<FirstClass> CREATOR = new Creator<FirstClass>() {
#Override
public FirstClass createFromParcel(Parcel in) {
return new FirstClass(in);
}
#Override
public FirstClass[] newArray(int size) {
return new FirstClass[size];
}
};
hope this helps. 3>
I have a Combo Bx (Dropdown box) with an index range of 0-20. If there anyways I can use that index to specify which object I want data from? All of the objects use the same naming convention obj0, obj1, obj2, etc. Basically something like this...
public abstract class Person {
private String name;
private String title;
private String email;
private String job;
public Person(String name, String title, String email, String job){
this.name = name;
this.title = title;
this.email = email;
this.job = job;
}
//Getters and Setters
}
public class main extends javax.swing.JFrame {
...misc code...
private void btn_startActionPerformed(java.awt.event.ActionEvent evt) {
Person obj0 = new Person("Jon Doe",
"Program Coordinator",
"jon.doe#test.com",
"Faculty");
Person obj1 = ...
...
Person obj20 = ...
/*
Onclick it uses the index of the current index in the combobox (dropdown)
to specify which object to get the data from.
*/
private void btn_GetActionPerformed(java.awt.event.ActionEvent evt) {
//Uses the obj naming convention plus the index
string foo = "obj" + toString(combobox_Name.getSelectedIndex());
//Fills the textbox using the above string and the getName method
txtbox_username.setText(ToObject(foo).getName);
}
I have created a basic design of what I think you want:
This code creates 20 objects, adds them to a combobox and uses their predefined name when selected to change a textfield.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
class ObjExample {
String name;
public ObjExample(String name) {
this.name = name;
}
#Override
public String toString() {
return name;
}
}
public class Main extends JFrame implements ActionListener {
JComboBox jcb = new JComboBox();
JTextField jtf = new JTextField("Text Field");
public Main() {
setSize(200, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
for (int i = 0; i <= 20; i++) {
jcb.addItem(new ObjExample(Integer.toString(i)));
}
jcb.addActionListener(this);
add(jcb);
add(jtf);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jcb) {
ObjExample obj = (ObjExample) jcb.getSelectedItem();
jtf.setText(obj.toString());
}
}
}
Activity1
private Cursor model = null;
private ClientAdapter adapter = null;
private ClientHelper helper = null;
private SharedPreferences prefs = null;
private ArrayAdapter<String> adapters;
private ArrayAdapter<String> adaptera;
private String[] available_locations;
private String[] selected_locations;
private ListView list1;
private ListView list2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locations);
list1 = (ListView) findViewById(R.id.available_locations);
list2 = (ListView) findViewById(R.id.selected_locations);
available_locations = getIntent().getStringExtra("List");
.....
Activity 2
....
public String getID(Cursor c) {
return (c.getString(0));
}
public String getclientName(Cursor c) {
return (c.getString(1));
}
public String getAddress(Cursor c) {
return (c.getString(2));
}
public String getTelephone(Cursor c) {
return (c.getString(3));
}
public String getCuisineType(Cursor c) {
return (c.getString(4));
}
public double getLatitude(Cursor c) {
return (c.getDouble(5));
}
public double getLongitude(Cursor c) {
return (c.getDouble(6));
}
public ArrayList<String> getclient;
getclient.add("clientName");
getclient.add("Address");
getclient.add("Telephone");
getclient.add("cuisineType");
getclient.add("lat");
getclient.add("lon");
public Intent intenti;
intenti = new Intent(ClientHelper.this, SetDestination.class);
intenti.putExtra("List", getclient);
startactivity(intenti);
How do I pass information from Activity2 to Activity1?
I want to do a Listview where I can select clients from the list that I have already added (hence two activity, list1 and list2, in activity1)
Use can use the Bundle to pass the data from one activity to other.
First make the object parcelable.You can do that using the plugin of Parcelable in android studio.
Example.
Intent intent=new Intent(getActivity(),targetclassname.class);
HomeScreenData homeScreenData=new HomeScreenData();//Pojo class
intent.putExtra("categorydesc",homeScreenData);
startActivity(intent);
{
//Pojo Class`enter code here`
public class HomeScreenData implements Parcelable {
private String imagePath;
private String imageTitle;
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
public String getImageTitle() {
return imageTitle;
}
public void setImageTitle(String imageTitle) {
this.imageTitle = imageTitle;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.imagePath);
dest.writeString(this.imageTitle);
}
public HomeScreenData() {
}
protected HomeScreenData(Parcel in) {
this.imagePath = in.readString();
this.imageTitle = in.readString();
}
public static final Parcelable.Creator<HomeScreenData> CREATOR = new Parcelable.Creator<HomeScreenData>() {
public HomeScreenData createFromParcel(Parcel source) {
return new HomeScreenData(source);
}
public HomeScreenData[] newArray(int size) {
return new HomeScreenData[size];
}
};
}