Android 将一个数组复制到另一个数组
我有两个数组,第一个是optionlist,它是这样的,由json填充Android 将一个数组复制到另一个数组,android,Android,我有两个数组,第一个是optionlist,它是这样的,由json填充 public class Options { private String age; private String optionNo; private String installmentAmount; private String noInstallment; public String getNoInstallment() { return noInstallme
public class Options {
private String age;
private String optionNo;
private String installmentAmount;
private String noInstallment;
public String getNoInstallment() {
return noInstallment;
}
public void setNoInstallment(String noInstallment) {
this.noInstallment = noInstallment;
}
public String getInstallmentAmount() {
return installmentAmount;
}
public void setInstallmentAmount(String installmentAmount) {
this.installmentAmount = installmentAmount;
}
public String getOptionNo() {
return optionNo;
}
public void setOptionNo(String optionNo) {
this.optionNo = optionNo;
}
second one is templst
public class TempList {
private String tage;
private String toptionNo;
private int tinstallmentAmount;
private int tnoInstallment;
private String tdescription;
private String tto;
private String tmode;
private String tfrom;
public String getTage() {
return tage;
}
public void setTage(String tage) {
this.tage = tage;
}
public String getToptionNo() {
return toptionNo;
}
public void setToptionNo(String toptionNo) {
this.toptionNo = toptionNo;
}
public int getTinstallmentAmount() {
return tinstallmentAmount;
}
public void setTinstallmentAmount(int tinstallmentAmount) {
this.tinstallmentAmount = tinstallmentAmount;
}
public int getTnoInstallment() {
return tnoInstallment;
}
public void setTnoInstallment(int tnoInstallment) {
this.tnoInstallment = tnoInstallment;
}
public String getTdescription() {
return tdescription;
}
public void setTdescription(String tdescription) {
this.tdescription = tdescription;
}
public String getTto() {
return tto;
}
public void setTto(String tto) {
this.tto = tto;
}
public String getTmode() {
return tmode;
}
public void setTmode(String tmode) {
this.tmode = tmode;
}
public String getTfrom() {
return tfrom;
}
public void setTfrom(String tfrom) {
this.tfrom = tfrom;
}
}
在我的主要活动中,当用户输入年龄时,我想将一些详细信息复制到模板中这是我的代码
int age=18;
int amount = 10000;
int installamt;
int tempIndx=0;
for(int i=0; i < optionlst.length; i++){
if (age == Integer.parseInt(optionlst[i].getAge()))
{
installamt = (amount/10000)*(Integer.parseInt(optionlst[i].getInstallmentAmount()));
templst[tempIndx].setTinstallmentAmount(installamt);
templst[tempIndx].setTnoInstallment(Integer.parseInt(optionlst[i].getNoInstallment()));
tempIndx++ ;
}
}
我的主要活动
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.*;
import org.apache.http.impl.client.DefaultHttpClient;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
import android.support.v7.app.ActionBarActivity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
HttpClient httpclient1 = new DefaultHttpClient();
HttpClient httpclient2 = new DefaultHttpClient();
Packages[] packagelst;
Options[] optionlst;
TempList[] templst;
int age=18;
int amount = 10000; // to get the expected monthly benefit
int installamt;
int tempIndx=0;
private Thread t1 = null;
private Thread t2 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = new Thread(new Runnable() {
public void run() {
try {
HttpGet httpGet = new HttpGet("here my web service address");
// httpGet.setHeader("Content-Type", "application/json");
// httpGet.setHeader("Accept", "JSON");
HttpResponse httpResponse = httpclient1.execute(httpGet);
HttpEntity resEntity = httpResponse.getEntity();
if (resEntity != null) {
String resppack = EntityUtils.toString(resEntity);
Log.i("Response", resppack);
GsonBuilder gsonb = new GsonBuilder();
Gson gson = gsonb.create();
// EDineOpeningHoursServerResponse serverResponse = null;
JsonParser parser = new JsonParser();
if (parser.parse(resppack).isJsonArray()) {
packagelst = gson.fromJson(resppack, Packages[].class);
} else if (parser.parse(resppack).isJsonArray()) {
}
// for(int i = 0; i<packagelst.length; i++)
// {
// Log.i("Option No" , packagelst[i].getOptionNo());
// }
// return serverResponse;
}
else{ Log.i("TAG", "null");}
//return null;
} catch (ClientProtocolException e) {
e.printStackTrace();
// throw new EDineDeliveryException(100, "Error Occurred while processing the request");
} catch (IllegalArgumentException e) {
e.printStackTrace();
// throw new EDineDeliveryException(101, "Invalid user input");
} catch (IOException e) {
e.printStackTrace();
// throw new EDineDeliveryException(102, ApplicationConstants.CONNECTION_LOST_ERROR_MSG);
} catch (ParseException e) {
e.printStackTrace();
// throw new EDineDeliveryException(103, ApplicationConstants.CONNECTION_LOST_ERROR_MSG);
} catch (Exception e) {
e.printStackTrace();
// throw new EDineDeliveryException(106, "Error Occurred while processing the request");
}
}
});
t2 = new Thread(new Runnable() {
public void run() {
try {
HttpGet httpGet = new HttpGet("here my web service address");
// httpGet.setHeader("Content-Type", "application/json");
// httpGet.setHeader("Accept", "JSON");
HttpResponse httpResponse = httpclient2.execute(httpGet);
HttpEntity resEntity = httpResponse.getEntity();
if (resEntity != null) {
String resp = EntityUtils.toString(resEntity);
Log.i("Response", resp);
GsonBuilder gsonb = new GsonBuilder();
Gson gson = gsonb.create();
// EDineOpeningHoursServerResponse serverResponse = null;
JsonParser parser = new JsonParser();
if (parser.parse(resp).isJsonArray()) {
optionlst = gson.fromJson(resp, Options[].class);
} else if (parser.parse(resp).isJsonArray()) {
}
// for(int i = 0; i<optionlst.length; i++)
// {
// Log.i("Age" , optionlst[i].getAge());
// }
// return serverResponse;
}
else{ Log.i("TAG", "null");}
//return null;
} catch (ClientProtocolException e) {
e.printStackTrace();
// throw new EDineDeliveryException(100, "Error Occurred while processing the request");
} catch (IllegalArgumentException e) {
e.printStackTrace();
// throw new EDineDeliveryException(101, "Invalid user input");
} catch (IOException e) {
e.printStackTrace();
// throw new EDineDeliveryException(102, ApplicationConstants.CONNECTION_LOST_ERROR_MSG);
} catch (ParseException e) {
e.printStackTrace();
// throw new EDineDeliveryException(103, ApplicationConstants.CONNECTION_LOST_ERROR_MSG);
} catch (Exception e) {
e.printStackTrace();
// throw new EDineDeliveryException(106, "Error Occurred while processing the request");
}
}
});
t1.start();
t2.start();
for(int i=0; i < optionlst.length; i++){
if (age == Integer.parseInt(optionlst[i].getAge()))
{
installamt = (amount/10000)*(Integer.parseInt(optionlst[i].getInstallmentAmount()));
templst[tempIndx].setTinstallmentAmount(installamt);
templst[tempIndx].setTnoInstallment(Integer.parseInt(optionlst[i].getNoInstallment()));
tempIndx++ ;
}
}
/*
tempIndx = 0;
for(int k=0; k < optionlst.length; k++){
templst[k].setTto(packagelst[k].getTo());
templst[k].setTfrom(packagelst[k].getFrom());
templst[k].setTmode(packagelst[k].getMode());
}
*/
// opp("http://172.20.106.125:8080/BOCService/webresources/abhimana/packages");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
import java.io.IOException;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.util.EntityUtils;
导入org.apache.http.client.*;
导入org.apache.http.impl.client.DefaultHttpClient;
导入com.google.gson.gson;
导入com.google.gson.GsonBuilder;
导入com.google.gson.JsonParser;
导入android.support.v7.app.ActionBarActivity;
导入android.net.ParseException;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
公共类MainActivity扩展了ActionBarActivity{
HttpClient httpclient1=新的DefaultHttpClient();
HttpClient httpclient2=新的默认HttpClient();
包装[]包装材料;
期权[]期权清单;
圣殿骑士[]圣殿骑士;
年龄=18岁;
int amount=10000;//以获得预期的每月福利
国际金额;
int-tempIndx=0;
私有线程t1=null;
私有线程t2=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=新线程(新可运行(){
公开募捐{
试一试{
HttpGet-HttpGet=newhttpget(“这里是我的web服务地址”);
//setHeader(“内容类型”、“应用程序/json”);
//setHeader(“接受”、“JSON”);
HttpResponse HttpResponse=httpclient1.execute(httpGet);
HttpEntity当前状态=httpResponse.getEntity();
if(最近性!=null){
字符串resppack=EntityUtils.toString(最近性);
Log.i(“响应”,resppack);
GsonBuilder gsonb=新的GsonBuilder();
Gson-Gson=gsonb.create();
//EdineopingHoursServerResponse服务器响应=null;
JsonParser=新的JsonParser();
if(parser.parse(resppack.isJsonArray()){
packagelst=gson.fromJson(respack,Packages[].class);
}else if(parser.parse(resppack.isJsonArray()){
}
//对于(int i=0;i您正在尝试从以前从未初始化过的templst数组中访问数据,以从中访问数据。初始化变量templst:
如:
TempList[] templst = new TempList[size];
ArrayList<TempList> templst = new ArrayList<TempList>();
或:
TempList[] templst = new TempList[size];
ArrayList<TempList> templst = new ArrayList<TempList>();
ArrayList templast=new ArrayList();
您的数组模板从未初始化过。您可以在循环之前对其进行初始化。数组需要一个固定的大小进行实例化。发布您的日志……MainActivity.java:170?post MainActivity.onCreate(MainActivity.java:170)行是什么?在哪里初始化模板数组?for(int i=0;i