[英]Android TextView setText with Getter return
我只是嘗試了我的第一個 Android 應用程序,但遇到了一個我沒有真正取得成功的問題。 在我的應用程序中,我創建了一個將填充對象的ArrayList
。 每個對象有四個屬性。 現在我想在它自己特定的TextView
為每個 Object 編寫每個屬性。 但有我的問題。 如果在普通 Java 應用程序中運行相同的代碼並且我使用System.out.Print("getter method")
我得到我想要的值,但如果我使用.setText("getter method")
我得到你能得到的請參閱屏幕截圖。
活動類:
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"));
...
}
}
信標類:
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;
}
}
信標列表類
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));
}
嘗試這個
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"));
}
它可能因為空值而崩潰;
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;
}
從我看來, .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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.