2016-12-06 84 views
1

我有三項活動。在一個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

回答

0

修改您的適配器如下面的代碼:

定義新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

不客氣。樂意效勞.. –

相關問題