【问题标题】:Why isn't my spinner updating with my arrayList items?为什么我的微调器没有用我的 arrayList 项目更新?
【发布时间】:2013-10-03 06:51:10
【问题描述】:

我有当前方法,并且 sActivity 有一个我想使用的公共 arrayList。 我只是想不通,我的微调器中没有任何值。这个微调器在另一个活动中,而不是我将项目添加到数组列表的地方。所以也许我必须intent.putExtra("arrayL", savedColors) 这样我必须将arraylist 发送到这个活动......我不知道只是猜测。任何帮助深表感谢。我已经将一些 ColorSaver 对象硬编码到 arrayList 中,所以我知道其中有值。

private void addColorNames()
{
    Spinner colorsSpinner = (Spinner) findViewById(R.id.colorsSpinner);

    ArrayAdapter<ColorSaver> dataAdapter 
        = new ArrayAdapter<ColorSaver>
        (RecallActivity.this, android.R.layout.simple_spinner_item, sActivity.savedColors);

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    colorsSpinner.setAdapter(dataAdapter);

}//End addColorNames()

public class RecallActivity extends Activity {


ArrayList<String> namesArray = new ArrayList<String>();
SaveActivity sActivity = new SaveActivity();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recall);
    // Show the Up button in the action bar.
    setupActionBar();
    final Intent intent1 = new Intent(this, MainActivity.class);
    final Spinner colorList = (Spinner) findViewById(R.id.colorsSpinner);
    Button grabButton = (Button) findViewById(R.id.grabButton);

    //Load the spinner with the saved colors
    addColorNames();


    //namesArray.addAll(sActivity.nameArray);
    //colorsSpinner.add;
    grabButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            //ColorSaver saver = new ColorSaver(colorName, redcolor, greencolor, bluecolor);
            //savedColors.add(saver);
            //startActivity(intent1);
            ColorSaver selectedItem = (ColorSaver) colorList.getSelectedItem();

            int redValue, greenValue, blueValue;
            String name;
            redValue = selectedItem.getRedValue();
            greenValue = selectedItem.getGreenValue();
            blueValue = selectedItem.getBlueValue();
            name = selectedItem.getColorName();
            intent1.putExtra("savedRValue", redValue);
            intent1.putExtra("savedGValue", greenValue);
            intent1.putExtra("savedBValue", blueValue);
            intent1.putExtra("savedName", name);
            startActivity(intent1);


        }//END onClick
    });
}

/**
 * Set up the {@link android.app.ActionBar}.
 */
private void setupActionBar() {

    getActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.recall, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}// END onOptionsItemSelected(MenuItem item)


public void addColorNames()
{
    Spinner colorsSpinner = (Spinner) findViewById(R.id.colorsSpinner);

    ArrayAdapter<ColorSaver> dataAdapter 
        = new ArrayAdapter<ColorSaver>
        (RecallActivity.this, android.R.layout.simple_spinner_item, sActivity.savedColors);

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    colorsSpinner.setAdapter(dataAdapter);

}//End addColorNames()

}//结束类

我将项目保存到数组的类是

public class SaveActivity extends Activity {

private static final String TAG = "Save Activity";
public ArrayList<ColorSaver> savedColors = new ArrayList<ColorSaver>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_save);
    // Show the Up button in the action bar.
    setupActionBar();

    final Intent intent1 = new Intent(this, MainActivity.class);
    Button saveButton = (Button) findViewById(R.id.saveButton1);
    final EditText nameField = (EditText) findViewById(R.id.colorNameField);
    final Intent intent = new Intent();

    //Making sure the savedColors arrayList has something in it.
    ColorSaver temp = new ColorSaver("TestColor", 180, 80, 255);
    savedColors.add(temp);


    saveButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click

            int redcolor, greencolor, bluecolor;
            redcolor = intent.getIntExtra("RedValue", 255);
            greencolor = intent.getIntExtra("GreenValue", 255);
            bluecolor = intent.getIntExtra("BlueValue", 255);
            String colorName = nameField.getText().toString();

            ColorSaver saver = new ColorSaver(colorName, redcolor, greencolor, bluecolor);
            savedColors.add(saver);
            Log.i(TAG, savedColors.get(savedColors.size()-1).getColorName());
            startActivity(intent1);

        }
    });

}//END OnCreate()

/**
 * Set up the {@link android.app.ActionBar}.
 */
private void setupActionBar() {

    getActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.save, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}//结束类

【问题讨论】:

  • 创建日志并打印 sActivity.savedColors 数组列表值,以检查这些值是否在该方法内部被访问。
  • 发布您的整个代码。

标签: android arraylist spinner loading


【解决方案1】:

当您从另一个活动访问它时,来自 sActivity 的ArrayList 可能是null

检查它是null还是里面有值,或者只是使用Intent将数组传递给Spinner的活动。 (使用Intent的额外数据更安全)

【讨论】:

  • 我确实使用了日志,我发现它在recallActivity中为空。我不太确定如何解决这个问题,因为我的数组列表包含 Color 对象,而不是像我可以通过意图传递的原始数据类型。
  • 尝试通过实现Serializable来序列化ColorSaver。像往常一样使用Intent.putExtra()来传输数据,但在检索数据时使用Intent.getSerializableExtra()并转换为ArrayList&lt;ColorSaver&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-16
  • 1970-01-01
  • 2022-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多