0

我正在尝试将来自两个不同来源的数据添加到数组适配器。一个来源来自包含硬编码字符串的微调器,另一个来源是允许用户创建自己的字符串以传递给数组(通过适配器)。下面是我的代码。在我看来,数组适配器根据可以传递给它的参数只能排除一个数据源......即

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource
(this, R.array.alarmList, android.R.layout.simple_spinner_item );

在这里我只能猜测实际的字符串输入是从............ android.R.layout.simple_spinner_item 我需要使用另一个数组适配器还是有办法将我的字符串变量添加到adaoter以及用户从微调器中选择的项目?我在这里做了一些研究,但画了一个空白!非常感谢。这是我的代码,它尝试将字符串输入到适配器............

public class NewAlarm extends Activity {

Spinner alarms;
//private Button b =  (Button) findViewById(R.id.btnAddCustom);
final EditText et =  (EditText) findViewById(R.id.edittext);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newalarm);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.alarmList, android.R.layout.simple_spinner_item );
    adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
    //et.setText("name");
    alarms = (Spinner) findViewById(R.id.cmbAlarms);
    alarms.setAdapter(adapter);
}

public void addAlarm(View view) {
    MainMenu.alarmList.add(new Alarm(alarms.getSelectedItem().toString()));
    Toast.makeText(getApplicationContext(), "Added " +      alarms.getSelectedItem().toString() + " alarm.", Toast.LENGTH_SHORT).show();
    NewAlarm.this.finish();
}

public void addCustomAlarm (View view){

    MainMenu.alarmList.add(new Alarm(et.getText().toString())); 
}

}

这是我的阵列适配器代码............

  public View getView(final int position, View convertView, ViewGroup viewGroup) {
      if (convertView == null) {
          LayoutInflater inflater = (LayoutInflater)     context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          convertView = inflater.inflate(R.layout.alarm_item, null);
      }

多谢你们!!

      // Get a handle on the UI controls
      TextView name = (TextView) convertView.findViewById(R.id.txtAlarmName);
      final TextView timeStamp = (TextView) convertView.findViewById(R.id.txtTimeStamp);
      // if the value of the timestamp from the alarm at the position selected is not a null value then set the text label to the alarm timestamp value
      if (MainMenu.alarmList.get(position).getTimeStamp() != null)
          {
            timeStamp.setText(MainMenu.alarmList.get(position).getTimeStamp().toString());
          }
      // Set the alarm name 
      name.setText(listItems.get(position).getName());
      // Get a handle on the button
      Button btnCheckNow = (Button) convertView.findViewById(R.id.btnCheckNow);

      btnCheckNow.setOnClickListener(new OnClickListener() {
          public void onClick(View view) {
              // Set the timestamp of the alarm object at the selected position
              MainMenu.alarmList.get(position).setTimeStamp();
              // Set the timestamp label 
              timeStamp.setText(MainMenu.alarmList.get(position).getTimeStamp().toString());
             }
      });
      return convertView;
}
4

1 回答 1

0

在创建适配器之前,不要调用ArrayAdapter.createFromResource(),而是导入数组并添加字符串:

CharSequence[] array = Arrays.asList(
        getResources().getTextArray(R.array.alarmList));
List<CharSequence> list = new ArrayList<CharSequence>(array);

list.add("custom string");

ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, 
        android.R.layout.simple_spinner_item, list);
于 2012-07-25T19:07:58.307 回答