我只是尝试了我的第一个Android应用程序并遇到了一个我没有真正领先的问题。在我的应用程序中,我创建了一个ArrayList
,它将填充对象。每个对象都有四个属性。现在我想在每个Object的特定TextView
中编写每个属性。但是有我的问题。如果相同的代码在普通的Java应用程序中运行,并且我使用System.out.Print("getter method")
我得到了我想要的值,但如果我使用.setText("getter method")
,我会得到你在屏幕截图上看到的内容。
ActivityClass:
public class ShowBeaconActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_showbeacons);
setUpShowBeacons();
}
public void setUpShowBeacons(){
//
//Beacon 1
//
TextView beaconOneUUID = (TextView)findViewById(R.id.txtBeaconOneUUIDValue);
beaconOneUUID.setText(BeaconListe.getBeacon(0,"UUID"));
TextView beaconOneMajor = (TextView)findViewById(R.id.txtBeaconOneMajorValue);
beaconOneMajor.setText(BeaconListe.getBeacon(0,"Major"));
TextView beaconOneMinor = (TextView)findViewById(R.id.txtBeaconOneMinorValue);
beaconOneMinor.setText(BeaconListe.getBeacon(0,"Minor"));
TextView beaconOneRSSI = (TextView)findViewById(R.id.txtBeaconOneRSSIValue);
beaconOneRSSI.setText(BeaconListe.getBeacon(0,"RSSI"));
//
//Beacon 2
//
TextView beaconTwoUUID = (TextView)findViewById(R.id.txtBeaconTwoUUIDValue);
beaconTwoUUID.setText(BeaconListe.getBeacon(1,"UUID"));
...
}
}
BeaconClass:
public class Beacon {
private String UUID;
private String Major;
private String Minor;
private int RSSI;
public Beacon(String UUID, String Major,String Minor, int RSSI){
this.UUID = UUID;
this.Major = Major;
this.Minor = Minor;
this.RSSI = RSSI;
}
//
//Getter
//
public String getUUID(){
return UUID;
}
public String getMajor(){
return Major;
}
public String getMinor() {
return Minor;
}
public int getRSSI() {
return RSSI;
}
//
//Setter
//
public void setRSSI(int RSSI){
this.RSSI = RSSI;
}
}
BeaconListeClass
public class BeaconListe {
private static ArrayList<Beacon>Liste = new ArrayList<Beacon>();
//
//Getter
//
public static String getBeacon(int index, String value){
String result = "";
switch(value){
case "UUID": result = Liste.get(index).getUUID();
break;
case "Major": result = Liste.get(index).getMajor();
break;
case "Minor": result = Liste.get(index).getMinor();
break;
case "RSSI": int resultTemp = Liste.get(index).getRSSI();
result = String.valueOf(resultTemp);
break;
}
return result;
}
//
//Setter
//
public static void addBeacon(String UUID, String Major, String Minor, int RSSI){
Liste.add(new Beacon(UUID, Major, Minor, RSSI));
}
答案 0 :(得分:0)
试试这个
TextView beaconOneUUID = (TextView)findViewById(R.id.txtBeaconOneUUIDValue);
if(BeaconListe.getBeacon(0,"UUID") != null){
beaconOneUUID.setText(BeaconListe.getBeacon(0,"UUID"));
}
TextView beaconOneMajor = (TextView)findViewById(R.id.txtBeaconOneMajorValue);
if(BeaconListe.getBeacon(0,"Major") != null){
beaconOneMajor.setText(BeaconListe.getBeacon(0,"Major"));
}
TextView beaconOneMinor = (TextView)findViewById(R.id.txtBeaconOneMinorValue);
if(BeaconListe.getBeacon(0,"Minor") != null){
beaconOneMinor.setText(BeaconListe.getBeacon(0,"Minor"));
}
TextView beaconOneRSSI = (TextView)findViewById(R.id.txtBeaconOneRSSIValue);
if(BeaconListe.getBeacon(0,"RSSI") != null){
beaconOneRSSI.setText(BeaconListe.getBeacon(0,"RSSI"));
}
//
//Beacon 2
//
TextView beaconTwoUUID = (TextView)findViewById(R.id.txtBeaconTwoUUIDValue);
if(BeaconListe.getBeacon(1,"UUID")){
beaconTwoUUID.setText(BeaconListe.getBeacon(1,"UUID"));
}
答案 1 :(得分:0)
因为空值而崩溃;
public static String getBeacon(int index, String value)
{
String result = "";
switch(value){
Beacon becon = Liste.get(index);
case "UUID":
result = becon!=null?becon.getUUID():"";
break;
case "Major":
result = becon!=null?becon.getMajor():"";
break;
case "Minor":
result = becon!=null?becon.getMinor():"";
break;
case "RSSI":
resultTemp = becon!=null?becon.getRSSI():0;
result = String.valueOf(resultTemp);
break;
}
return result==null?"":result;
}
答案 2 :(得分:0)
从我看到的情况来看,.setText("getter methods")
工作正常。问题是它设置的文本。检查您填充BeaconListe
的功能,您在此处未显示。
我尝试了您的代码,但使用以下
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_showbeacons);
populateBeaconListe();
setUpShowBeacons();
}
public void populateBeaconListe(){
BeaconListe.addBeacon("B1UUID","B1MAJOR","B1MINOR",1111);
BeaconListe.addBeacon("B2UUID","B2MAJOR","B2MINOR",2222);
BeaconListe.addBeacon("B3UUID","B3MAJOR","B3MINOR",3333);
BeaconListe.addBeacon("B4UUID","B4MAJOR","B4MINOR",4444);
}