从编辑文本中获取输入并保存在列表1中,但它将在其他列表中显示列表1的结果

ugmeyewa  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(279)

我有两节课。首先是 MainActivity.java 其次是 EditBox.java 我想做的是 MainActivity.java 创建4个列表,我想从editbox.java中的编辑框中获取数据并保存在 list 。当我在中的编辑框中输入 list 它将显示 list 1在其他列表中输入结果。请帮我在这里做什么是的代码
主活动.java

public class MainActivity extends Activity 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView1 = (ListView) findViewById(R.id.listView1);

         String[] items = { "List 1", "List 2", "List 3","List 4" };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, items);

        listView1.setAdapter(adapter);

        listView1.setOnItemClickListener(new OnItemClickListener() 
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,long id)
            {

                String item = ((TextView)view).getText().toString();

                //Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();

                Intent it1 =new Intent(getApplicationContext(),editbox.class); 

                startActivity(it1);

            }
        });

    }

    public boolean onCreateOptionsMenu(Menu menu) 
    {

        super.onCreateOptionsMenu(menu);
        menu.add("List").setIntent(new Intent(this,MainActivity.class));
        return true;
    }

}

编辑框.java

public class editbox extends Activity 
{

    private EditText nameText,nameText1;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.editbox);

        nameText=(EditText)findViewById(R.id.editText1);
        SharedPreferences setting = getSharedPreferences("List 1",0);
        nameText.setText(setting.getString("List 1", ""));

        nameText=(EditText)findViewById(R.id.editText1);
        SharedPreferences setting1 = getSharedPreferences("List 2",0);
        nameText.setText(setting1.getString("List 2", "")); 

    }

    protected void onStop() 
    {

        super.onStop();

        SharedPreferences setting = getSharedPreferences("List 1",0);
        SharedPreferences.Editor editor = setting.edit();
        editor.putString("List 1", nameText.getText().toString());
        editor.commit();

        SharedPreferences setting1 = getSharedPreferences("List 2",0);
        SharedPreferences.Editor editor1 = setting1.edit();
        editor1.putString("List 2", nameText1.getText().toString());
        editor1.commit();
    }

    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.main, menu);
        super.onCreateOptionsMenu(menu);
        menu.add("List 1").setIntent(new Intent(this,MainActivity.class));

        return true;
    }
}
vsaztqbk

vsaztqbk1#

nameText=(EditText)findViewById(R.id.editText1);
    SharedPreferences setting = getSharedPreferences("List 1",0);
    nameText.setText(setting.getString("List 1", ""));

// The problem is here
    nameText1=(EditText)findViewById(R.id.editText1); // get your second EditText ID here
    SharedPreferences setting1 = getSharedPreferences("List 2",0);
    nameText1.setText(setting1.getString("List 2", ""));
r55awzrz

r55awzrz2#

在editbox.java中,为什么只定义了一个编辑文本?是故意的还是在复制和粘贴时忘记了更改。尝试更改edittext名称和r.id.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

相关问题