Java 带Getter返回的Android TextView setText
我刚刚尝试了我的第一个Android应用程序,但遇到了一个我没有真正取得成功的问题。在我的应用程序中,我创建了一个ArrayList,其中将填充对象。每个对象有四个属性。现在我想把每个对象的每个属性写在它自己特定的TextView中。但这是我的问题。如果在普通Java应用程序中运行相同的代码,并且我使用System.out.Printgetter方法,我会得到我想要的值,但是如果我使用.setTextgetter方法,我会得到您在屏幕截图上看到的值 活动类:Java 带Getter返回的Android TextView setText,java,android,Java,Android,我刚刚尝试了我的第一个Android应用程序,但遇到了一个我没有真正取得成功的问题。在我的应用程序中,我创建了一个ArrayList,其中将填充对象。每个对象有四个属性。现在我想把每个对象的每个属性写在它自己特定的TextView中。但这是我的问题。如果在普通Java应用程序中运行相同的代码,并且我使用System.out.Printgetter方法,我会得到我想要的值,但是如果我使用.setTextgetter方法,我会得到您在屏幕截图上看到的值 活动类: public class ShowB
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;
}
}
BeaconListClass
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;
}
在我看来,.setTextgetter方法工作得很好。问题在于它设置的文本。检查填充BeaconList的函数,此处未显示该函数。
我尝试了你的代码,但使用了以下代码
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);
}
而且效果很好。请参见屏幕截图谢谢您的回答,但这没有帮助。我不知道为什么它会有帮助。也许我应该提一下。如果我单击我的按钮Show Beacons(显示信标),这将打开此活动,如果我没有添加至少4个信标,则这也是我列表的限制。因此,如果这个窗口弹出,我很确定在我的列表中每个TextView都有一个值:谢谢你为我指出了正确的方向:。我甚至没有想到我的方法可能是错误的。我所遇到的只是字符串uuid=String.valueOfTextView findViewByIdR.id.txtuid到字符串uuid=TextView findViewByIdR.id.txtuid.getText.toString,现在它工作得很好。