将EditText值从ListView传递给另一个活动

问题描述:

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

代码:

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

你传递一个列表项的位置和你的ArrayList的使用意图,并​​在接下来的活动中,你得到的是ArrayList和位置,并使用接收的位置从ArrayList中获得价值。 – Shailesh

+0

谢谢...可以给你简要的想法如何通过项目的位置 –

+0

请检查此http://stackoverflow.com/questions/5374546/passing-arraylist-through-intent – Shailesh

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

定义新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参数,首先点击它将添加项目,然后第二次点击相同的文本将删除项目。

我有一个参考看你需要什么,但HERE使用复选框。但通常情况下,逻辑仍然相同。

让我知道如果有什么遗漏

+0

谢谢...它的工作.. –

+0

不客气。乐意效劳.. –