![](/img/trans.png)
[英]Pass Data in ListView when clicked to EditText from another 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);
}
});
}
修改您的適配器,如下面的代碼:
定義新的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,它將刪除項目。
我有一個參考看起來像你需要的,但在這里使用復選框。 但一般來說,邏輯仍然相同。
如果遺漏了什么,請告訴我
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.