我有三项活动。在一个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);
}
});
}
答案 0 :(得分:0)
修改您的适配器,如下面的代码:
定义新的ArrayList
:
Private 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将删除该项目。
我的参考文字看起来像你需要的,但HERE使用复选框。但一般来说,逻辑仍然相同。
让我知道是否有遗漏