Home > database >  How to pass multiple information from a listView to another activity using the OnclickListener metho
How to pass multiple information from a listView to another activity using the OnclickListener metho

Time:05-29

I have a small problem with my application. I want to pass it the data stored in my listView and when I do setOnItemClickListener it doesn't send the stored data. Attached is part of my code.

public class MainActivity extends AppCompatActivity {

ListView listView;
ArrayList<String> list;
Button btAdd;
EditText etName, etAge, etDescription;
ArrayAdapter<String> arrayAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    etName = (EditText) findViewById(R.id.etName);
    etAge = (EditText) findViewById(R.id.etAge);
    etDescription = (EditText) findViewById(R.id.etDescription);
    btAdd = (Button) findViewById(R.id.btGuardar);
    listView = (ListView) findViewById(R.id.lvList);
    list = new ArrayList<>();
    arrayAdapter = new ArrayAdapter<>(getApplicationContext(),
            android.R.layout.simple_list_item_1, list);
    listView.setOnItemClickListener((adapterView, view, i, l) -> {
        Intent intent = new Intent(MainActivity.this, DataActivity.class);
        intent.putExtra("Name",list);
        intent.putExtra("Age", list);
        intent.putExtra("Description", list);
        startActivity(intent);
    });
    btAdd.setOnClickListener(view -> {
        String names=etName.getText().toString();
        String ages=etAge.getText().toString();
        String description= etDescription.getText().toString();
        String stringFormatted = String.format(names,ages,description);
        list.add(stringFormatted);
        listView.setAdapter(arrayAdapter);
        arrayAdapter.notifyDataSetChanged();
    });
}

}

public class DataActivity extends AppCompatActivity { TextView tvName,tvAge,tvDescription;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_data);
    tvName = findViewById(R.id.tvName);
    tvAge = findViewById(R.id.tvAge);
    tvDescription= findViewById(R.id.tvDescription);
    Bundle extras = getIntent().getExtras();
    extras.getString("Name");
    extras.getString("Age");
    extras.getString("Description");
}

}

CodePudding user response:

You're adding the entire array three times in the extras as a Serializable here:

intent.putExtra("Name",list);
intent.putExtra("Age", list);
intent.putExtra("Description", list);

But here, you're trying to get a String:

extras.getString("Name");
extras.getString("Age");
extras.getString("Description");

Either send a String and get a String, or send a Serializable and get a Serializable.

CodePudding user response:

You can do something like this

     arrayAdapter = new ArrayAdapter<>(getApplicationContext(),
            android.R.layout.simple_list_item_1, list);
    listView.setOnItemClickListener((adapterView, view, i, l) -> {
        Intent intent = new Intent(MainActivity.this, DataActivity.class);
        intent.putExtra("Name",list.get(i).split("#123#")[0]);
        intent.putExtra("Age", list.get(i).split("#123#")[1]);
        intent.putExtra("Description", list.get(i).split("#123#")[2]);
        startActivity(intent);
    });
    btAdd.setOnClickListener(view -> {
        String names=etName.getText().toString();
        String ages=etAge.getText().toString();
        String description= etDescription.getText().toString();
        String stringFormatted = names "#123#" ages "#123#" description;
        list.add(stringFormatted);
        listView.setAdapter(arrayAdapter);
        arrayAdapter.notifyDataSetChanged();
    });

But this not perfect way i made it just to show you how can work, because in this case List should have one item inside, i made it 3 items merged by #123#..

So in your case you need to use custom GridView and pass 3 lists to it..

I mean

List for Names
List for Ages
List for Description 
  • Related