如何修改字符串数组并将其保存以用于列表视图?

时间:2016-07-12 23:40:33

标签: java android listview

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);
}

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

1 个答案:

答案 0 :(得分:0)

您可以使用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);
}

然后覆盖onActivityResult,如

@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)&gt;光洁度()

此数据将发送至onActivityResult以上

希望有所帮助!