代码改进:视图列表

时间:2018-08-14 14:21:15

标签: java android arrays textview

我有以下代码:

public class Klasse {
  TextView[] tvAuftrag = new TextView[5];
  ImageView[] ivStatus = new ImageView[5];
  CardView[] cvAuftrag = new CardView[5];

  private void initialize() {
    tvAuftrag[0] = findViewById(R.id.tvAuftrag1);
    tvAuftrag[1] = findViewById(R.id.tvAuftrag2);
    tvAuftrag[2] = findViewById(R.id.tvAuftrag3);
    tvAuftrag[3] = findViewById(R.id.tvAuftrag4);
    tvAuftrag[4] = findViewById(R.id.tvAuftrag5);
    ivStatus[0] = findViewById(R.id.ivStatus1);
    ivStatus[1] = findViewById(R.id.ivStatus2);
    ivStatus[2] = findViewById(R.id.ivStatus3);
    ivStatus[3] = findViewById(R.id.ivStatus4);
    ivStatus[4] = findViewById(R.id.ivStatus5);
    cvAuftrag[0] = findViewById(R.id.cvAuftrag1);
    cvAuftrag[1] = findViewById(R.id.cvAuftrag2);
    cvAuftrag[2] = findViewById(R.id.cvAuftrag3);
    cvAuftrag[3] = findViewById(R.id.cvAuftrag4);
    cvAuftrag[4] = findViewById(R.id.cvAuftrag5);
  }
}

是否有更好的解决方案将Views初始化为数组?我试过像这样的ArrayList:

public class Klasse {
  List<TextView> tvAuftrag = ArrayList<>();

  private void initialize() {
    tvAuftrag.add(findViewById(R.id.tvAuftrag1));
    tvAuftrag.add(findViewById(R.id.tvAuftrag2));
    tvAuftrag.add(findViewById(R.id.tvAuftrag3));
    tvAuftrag.add(findViewById(R.id.tvAuftrag4));
    tvAuftrag.add(findViewById(R.id.tvAuftrag5));
  }
}

也许比上面的解决方案要好一些,但是它不起作用。还是有可能使用循环?那么我认为ID必须是可迭代的,但是怎么可能呢? 任何帮助或要学习的新信息将不胜感激。

2 个答案:

答案 0 :(得分:0)

///如果不是数组,要做的就是将其保存在列表中;

public class Klasse {
  List<TextView> tvAuftrag = new ArrayList<>();

  private void initialize() {
    tvAuftrag.add((TextView)findViewById(R.id.tvAuftrag1));
    tvAuftrag.add((TextView)findViewById(R.id.tvAuftrag2));
    tvAuftrag.add((TextView)findViewById(R.id.tvAuftrag3));
    tvAuftrag.add((TextView)findViewById(R.id.tvAuftrag4));
    tvAuftrag.add((TextView)findViewById(R.id.tvAuftrag5));
  }
}

答案 1 :(得分:0)

循环添加视图,例如:

public class MyApplicationName extends Application implements BootstrapNotifier {
private static final String TAG = "MyApplicationName";
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "App started up");
    BeaconManager beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(getApplicationContext());

    // By default the AndroidBeaconLibrary will only find AltBeacons.  If you wish to make it
    // find a different type of beacon, you must specify the byte layout for that beacon's
    // advertisement with a line like below.  The example shows how to find a beacon with the
    // same byte layout as AltBeacon but with a beaconTypeCode of 0xaabb.  To find the proper
    // layout expression for other beacon types, do a web search for "setBeaconLayout"
    // including the quotes.
    //
    beaconManager.getBeaconParsers().clear();
    beaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));


    // Uncomment the code below to use a foreground service to scan for beacons. This unlocks
    // the ability to continually scan for long periods of time in the background on Andorid 8+
    // in exchange for showing an icon at the top of the screen and a always-on notification to
    // communicate to users that your app is using resources in the background.
    //
    Notification.Builder builder = new Notification.Builder(this);
    builder.setContentTitle("Scanning for Beacons");
    Intent intent = new Intent(this, MonitoringActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(
            this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT
    );
    builder.setContentIntent(pendingIntent);
    beaconManager.enableForegroundServiceScanning(builder.build(), 456);

    // For the above foreground scanning service to be useful, you need to disable
    // JobScheduler-based scans (used on Android 8+) and set a fast background scan
    // cycle that would otherwise be disallowed by the operating system.
    //
    beaconManager.setEnableScheduledScanJobs(false);
    beaconManager.setBackgroundBetweenScanPeriod(0);
    beaconManager.setBackgroundScanPeriod(1100);

    Log.d(TAG, "setting up background monitoring for beacons and power saving");
    // wake up the app when a beacon is seen
    Region region = new Region("backgroundRegion",
            Identifier.parse("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6"), null, null);
    regionBootstrap = new RegionBootstrap(this, region);

    // simply constructing this class and holding a reference to it in your custom Application
    // class will automatically cause the BeaconLibrary to save battery whenever the application
    // is not visible.  This reduces bluetooth power usage by about 60%
    backgroundPowerSaver = new BackgroundPowerSaver(getApplicationContext());
}


@Override
public void didDetermineStateForRegion(int arg0, Region arg1) {
    // Don't care
}

@Override
public void didEnterRegion(Region arg0) {
    Log.d(TAG, "Got a didEnterRegion call");
    // This call to disable will make it so the activity below only gets launched the first time a beacon is seen (until the next time the app is launched)
    // if you want the Activity to launch every single time beacons come into view, remove this call.  
    //regionBootstrap.disable();
    Intent intent = new Intent(this, RangingActivity.class);
    // IMPORTANT: in the AndroidManifest.xml definition of this activity, you must set android:launchMode="singleInstance" or you will get two instances
    // created when a user launches the activity manually and it gets launched from here.
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    this.startActivity(intent);
}

@Override
public void didExitRegion(Region arg0) {
    // Don't care
}

如果3个数组的长度不同,则必须具有3个相似但截然不同的循环。