作为一个苦苦挣扎的新手,我设置了3个微调器,它们根据选择的项目拉动字符串数组。这是“年份,品牌和型号”设置,用于对车辆进行分类,这将根据所选车辆从sqlite3数据库中提取信息。因此Year是基本的微调器(2000年至2019年)。但是,根据选定的年份填充下一个微调器。此外,“模型”微调器取决于所选的“制造”。我大约。模型的80个字符串数组,仅在某些年份可用。综上所述,我已经将富兰克斯坦斯坦的怪兽代码从研究中拼凑起来。我能想到的唯一解决方案是使用所有选项填充这些微调器的if语句。下面是已减少的代码,以防止发生重复时浪费空间。 任何批评,帮助或想法都将受到赞赏。
MainActivity.java
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener,
AdapterView.OnItemSelectedListener {
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinStrYear = getResources().getStringArray(R.array.year);
spinStrMake = getResources().getStringArray(R.array.make);
spinStrModel = getResources().getStringArray(R.array.model);
spinYear = findViewById(R.id.spinnerYear);
spinMake = findViewById(R.id.spinnerMake);
spinModel = findViewById(R.id.spinnerModel);
ar = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, spinStrYear);
ar2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, spinStrMake);
ar3 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, spinStrModel);
spinYear.setAdapter(ar);
spinMake.setAdapter(ar2);
spinModel.setAdapter(ar3);
spinYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long id) {
if (spinYear.getSelectedItem().equals("2000"))
ar.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinMake.setAdapter(ar2);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spinMake.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long id) {
if (spinMake.getSelectedItem().equals("Acura")) {
ar2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinModel.setAdapter(ar3);
}
if (spinMake.getSelectedItem().equals("Audi")) {
}
if (spinMake.getSelectedItem().equals("BMW")) {
}
if (spinMake.getSelectedItem().equals("Cadillac")) {
}
Strings.xml
<resources>
<string-array name="year"> // Year spinner population
<item>2000</item>
<item>2001</item>
<item>2002</item>
<item>2003</item>
<item>2004</item>
<item>2005</item>
<item>2006</item>
<item>2007</item>
<item>2008</item>
<item>2009</item>
<item>2010</item>
<item>2011</item>
<item>2012</item>
<item>2013</item>
<item>2014</item>
<item>2015</item>
<item>2016</item>
<item>2017</item>
<item>2018</item>
<item>2019</item>
</string-array> // End of year spinner population
<string-array name="make"> // Make spinner population
<item>Acura</item>
<item>Audi</item>
<item>BMW</item>
<item>Buick</item>
<item>Cadillac</item>
</string-array> // End of Make spinner population
<string-array name="Acura2000"> // Model spinner population
<item>Integra</item>
<item>NSX</item>
<item>RL</item>
<item>TL</item>
</string-array>
<string-array name="Acura2001">
<item>CL</item>
<item>Integra</item>
<item>MDX</item>
<item>NSX</item>
<item>RL</item>
<item>TL</item>
</string-array>
<string-array name="Acura2002_03"> // Same data for 02 and 03
<item>CL</item>
<item>MDX</item>
<item>NSX</item>
<item>RL</item>
<item>RSX</item>
<item>TL</item>
</string-array>
<string-array name="Acura2004_05"> // Same data for 04 and 05
<item>MDX</item>
<item>NSX</item>
<item>RL</item>
<item>RSX</item>
<item>TL</item>
<item>TSX</item>
</string-array>
<string-array name="Acura2006">
<item>MDX</item>
<item>RL</item>
<item>RSX</item>
<item>TL</item>
<item>TSX</item>
</string-array>
答案 0 :(得分:0)
而不是在每个年中的每个制造中创建字符串数组。我重新安排了制造和模型的生产周期,然后在运行时生成列表。请尝试以下操作:
MainActivity.java:
let sorted = items.sorted { $0.category_id < $1.category_id }
strings.xml:
public class MainActivity extends AppCompatActivity {
Spinner spinYear, spinMake, spinModel;
ArrayAdapter ar, ar2, ar3;
String[] spinStrYear;
ArrayList<String> spinStrMake, spinStrModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinYear = findViewById(R.id.spinner1);
spinMake = findViewById(R.id.spinner2);
spinModel = findViewById(R.id.spinner3);
spinStrYear = getResources().getStringArray(R.array.year);
spinStrMake = new ArrayList<>();
spinStrModel = new ArrayList<>();
ar = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinStrYear);
ar2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinStrMake);
ar3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinStrModel);
spinYear.setAdapter(ar);
spinMake.setAdapter(ar2);
spinModel.setAdapter(ar3);
spinYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) {
updateSpinnerMake();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
spinMake.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) {
updateSpinnerModel(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
private void updateSpinnerMake() {
int selectedYear = Integer.parseInt(spinYear.getSelectedItem().toString());
spinStrMake.clear();
String[] tmpMakeArray = getResources().getStringArray(R.array.make);
for (String tmpMake : tmpMakeArray) {
String[] tmpMakeDetails = getResources().getStringArray(getResources().getIdentifier(
tmpMake, "array", getPackageName()));
int startYear = Integer.parseInt(tmpMakeDetails[0]);
int stopYear = Integer.parseInt(tmpMakeDetails[1]);
if (selectedYear >= startYear && selectedYear <= stopYear) spinStrMake.add(tmpMake);
}
if (spinStrMake.size() == 0) spinStrMake.add("No make on that year!!");
ar2.notifyDataSetChanged();
if (spinMake.getSelectedItemPosition() >= 0) updateSpinnerModel(spinMake.getSelectedItemPosition());
else updateSpinnerModel(0);
}
private void updateSpinnerModel(int position) {
spinStrModel.clear();
if (!spinStrMake.get(0).equals("No make on that year!!")){
int selectedYear = Integer.parseInt(spinYear.getSelectedItem().toString());
String[] tmpModelArray = getResources().getStringArray(getResources().getIdentifier(
spinStrMake.get(position), "array", getPackageName()));
String tmpModel;
for (int j=2; j<tmpModelArray.length; j++) {
tmpModel = tmpModelArray[j];
String[] period = getResources().getStringArray(getResources().getIdentifier(
tmpModel, "array", getPackageName()));
int startYear = Integer.parseInt(period[0]);
int stopYear = Integer.parseInt(period[1]);
if (selectedYear >= startYear && selectedYear <= stopYear)
spinStrModel.add(tmpModel);
}
if (spinStrModel.size() == 0) spinStrModel.add("No Match Model Found!!");
}
ar3.notifyDataSetChanged();
}
}
希望有帮助!