1

I have three activities. In one Activity, I read data from CSV file and using ListView, I am showing it in EditText. Now, I want to pass value of EditText to another Activity. What can be done to solve this problem?

Code:

    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);
        }
    });
}
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
vivek pagar
  • 45
  • 1
  • 8

1 Answers1

0

modify your adapter like the following code :

Define new ArrayList: Private List<String> listItemChosen = new ArrayList<>();

Create method that return list :

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

in your onClick do like below :

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();
                }
            });

That code above just in adding condition. Every click in textview, it always save the selected item in your list.

if you need remove item that has been added, you should add parameter again like int parameter, First click it will add item, and the second click on same textview it will delete the item.

I have a reference look like what you need, but HERE using checkbox. But generally, the logic still same.

let me know if there something missed

R Besar
  • 574
  • 1
  • 5
  • 12