【问题标题】:How to modify a string array and save it to use for a listview?如何修改字符串数组并将其保存以用于列表视图?
【发布时间】:2016-07-12 23:40:33
【问题描述】:
public class MainActivity extends AppCompatActivity {
private Button addcar;

String[]foods={"carone","cartwo","carthree"};


   @Override
   protected void onCreate(Bundle savedInstanceState) {
    if(savedInstanceState!=null){             <----- Here is where I thought I could get the updated array to be added to the listview.             
        String [] foods = savedInstanceState.getStringArray("foods");

        ListAdapter myadapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,foods);
        ListView mylistview = (ListView)findViewById(R.id.list);
        mylistview.setAdapter(myadapter);

    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addcar = (Button) findViewById(R.id.button);
    addcar.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    Intent myintent = new Intent("com.example.husse.profilesalgortihm.Main2Activity");
                startActivity(myintent);
                }
            }
    );

  ListAdapter myadapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,foods);
 ListView mylistview = (ListView)findViewById(R.id.list);
    mylistview.setAdapter(myadapter);
   mylistview.setOnItemClickListener(
           new AdapterView.OnItemClickListener() {
               @Override
               public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                   if (id == 0) {
                       Toast.makeText(MainActivity.this, foods[0], Toast.LENGTH_SHORT).show();

                   }
               }
           }
   );
}
}

我的第二个活动

    public class Main2Activity extends MainActivity   {
    public int number = 0;
    public EditText Model;
    public  EditText Body;
    public  EditText color;
    public String M; 
    public String B;
    public String C;
    private final String tag = "Main2activity";
    private Button add;
    Car_information[] cars = new Car_information[10];



     @Override
     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
   Model = (EditText)findViewById(R.id.editText);
   Body = (EditText)findViewById(R.id.editText2);
   color = (EditText)findViewById(R.id.editText3);
   add = (Button)findViewById(R.id.button2);
   M  =  Model.getText().toString();
    B = Body.getText().toString();
    C = color.getText().toString();
     // may run into issues with the final
    add.setOnClickListener(
       new View.OnClickListener() {
           @Override
           public void onClick(View v) {  <--- Here is where I tried to save the information about the car but for the listview purpose I only wanted the name to be added to the Listview in the previous activity.


               cars[number] = new Car_information();

               cars[number].Model = M;
               cars[number].Body = B;
               cars[number].Color = C;
               foods[number]= M;<----- Updating the array with the name the of the vehicle the user put in
               number ++;

                    Intent intent = new      
                  Intent(Main2Activity.this,MainActivity.class);        
               intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
               startActivity(intent);

           }
       }
        );

}

@Override
public void onSaveInstanceState(Bundle outState) { <----- where I tried to save the new updated array to be used in the listview, so the user could see the new listview when the user goes back to the firstactivity.
    super.onSaveInstanceState(outState);
outState.putStringArray("foods",foods);
}

我想做的是,当用户进入第二个活动时,他/她将输入有关他/她的车辆的信息,但是当他们单击添加按钮时,它会将他们带回上一个活动,并且将保存他们输入的汽车的名称,并将其显示在列表视图中。但是当他们回到第一个活动时,列表并没有更新。

【问题讨论】:

  • Main2Activity 中的食物定义在哪里?
  • 你想拥有一个固定大小的数组吗?或者您想灵活地添加和删除列表中的项目?
  • 灵活的@SalvatoryBaya
  • 我从第一个继承,所以在第一个活动@Thomas 中使用相同的字符串数组

标签: java android listview


【解决方案1】:

您可以使用startActivityForResult() 函数来实现您的任务。

示例:您在 Activity1 中,想从 Activity2 获取数据。

在Activity1中,调用intent以特定请求启动Activity2

static final int PICK_CONTACT_REQUEST = 1;  // The request code
...
private void pickContact() {
    Intent pickContactIntent = new Intent(this, Activity2.class);
    pickContactIntent.putExtra(...) <-- put some data to send to Activity2
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

然后覆盖onActivityResultlike

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // get data sent from Activity2 via data parameter
        }
    }
}

在Activity2中,当你完成流程后,通过流向Activity1发送数据

setResult (int resultCode, Intent data)>finish()

这个数据将发送到上面的onActivityResult

希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-20
    • 2014-03-16
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多