繁体   English   中英

将ListView中的EditText值传递给另一个Activity

[英]Pass EditText value from ListView to another Activity

我有三个活动。 在一个Activity ,我从CSV文件读取数据并使用ListView ,我在EditText显示它。 现在,我想将EditText值传递给另一个Activity 可以做些什么来解决这个问题?

码:

    String dir = Environment.getExternalStorageDirectory() + "/grocery/data/" + spinner.toString();

    final File csvfile = new File(dir.toString());

    ListView listView = (ListView) findViewById(R.id.listview);

    MyList adapter = new MyList(getApplicationContext(), R.layout.activity_editfeild);

    listView.setAdapter(adapter);


    studentid= (EditText) findViewById(R.id.et_studentid);
    Schoolname= (EditText) findViewById(R.id.et_schoolname);
    schoolAdress= (EditText) findViewById(R.id.et_schooladd);
    studentname=(EditText) findViewById(R.id.et_studentname);
    fathername= (EditText) findViewById(R.id.et_fathername);
    mothername= (EditText) findViewById(R.id.et_mothername);
    studentaddress= (EditText) findViewById(R.id.et_studentadd);

 //Toast.makeText(Editinfo.this, studentid.getText().toString(), Toast.LENGTH_SHORT).show();


    FileInputStream inputStream = null;
    try {
        inputStream = new FileInputStream(csvfile);
    } catch (FileNotFoundException e){
        e.printStackTrace();
    }

    CSVReader reader = new CSVReader(inputStream);

    final List<String[]> myList=reader.read();

    for (String[] data : myList) {
        adapter.add(data);
    }

    next.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), Editinfo1.class);

            intent.putExtra("spinner", chosenOption);

            intent.putExtra("studentid", studentid.getText().toString());

            intent.putExtra("schoolname", Schoolname.getText().toString());
            intent.putExtra("schooladdress", schoolAdress.getText().toString());
            intent.putExtra("studentname", studentname.getText().toString());
            intent.putExtra("fathername", fathername.getText().toString());
            intent.putExtra("mothername", mothername.getText().toString());
            intent.putExtra("studentaddress", studentaddress.getText().toString());
            intent.putExtra("myList", (Serializable) myList);
            //intent.putExtra("mylist", myList);
            startActivity(intent);
        }
    });
}

修改您的适配器,如下面的代码:

定义新的ArrayListPrivate List<String> listItemChosen = new ArrayList<>();

创建返回列表的方法:

private List<String> getSelectedString(){
        return listItemChosen;
    }

在你的onClick上做如下:

SharedPreferences sharedPreferences = context.getSharedPreferences(Constants.MODE_SHARED, Context.MODE_PRIVATE);
final SharedPreferences.Editor edit = sharedPreferences.edit();
final Gson gson = new Gson();

holder.tvItemName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listItemChosen.add(itemName);
                    String jsonString = gson.toJson(getSelectedString());
                    edit.putString(Constants.KEY_SHARED,jsonString);
                    edit.commit();
                }
            });

上面的代码只是添加条件。 每次单击textview ,它始终会将所选项目保存在列表中。

如果你需要删除已经添加的项目,你应该再次添加参数,如int参数,首先点击它将添加项目,第二次点击相同的textview,它将删除项目。

我有一个参考看起来像你需要的,但在这里使用复选框。 但一般来说,逻辑仍然相同。

如果遗漏了什么,请告诉我

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM