Array elements based on position

2

I'm developing a small project in android studio where I have a listview where data is entered through an array. But what I need is when the user selects some element from the list, based on the position of the element loading all the data in another activity

class that sends the data (position)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_noticias);
    mAdapter = new NoticiasAdapter(Noticias.this, mList);
    setListAdapter(mAdapter);
}

public void onResume() {
    super.onResume();
    preencherlista();
    mAdapter.notifyDataSetChanged();
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    mList.get(position);
    Intent intent = new Intent(getBaseContext(), NoticiasValor.class);
    intent.putExtra("valor","position");
    startActivity(intent);
}

public void preencherlista() {
    mList.add(new NoticiasDados("Jose", "Maria"));
    mList.add(new NoticiasDados("Maria","Alfredo"));
    mList.add(new NoticiasDados("Luis","Sonia"));
}

}

class that receives and displays

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.noticas_valor_layout);

    /*Intent mIntent = getIntent();
    int intValue = mIntent.getIntExtra("valor",0);*/
    int intValue= getIntent().getIntExtra("valor",0);
    TextView a = (TextView) findViewById(R.id.txttitulo2);
    a.setText(mList.get(intValue).getTitulo());
    TextView b = (TextView) findViewById(R.id.txttexto2);
    b.setText(mList.get(intValue).getTexto());
}
    
asked by anonymous 07.09.2017 / 00:30

1 answer

1
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    NoticiasDados noticiasDados = mList.get(position);
    Intent intent = new Intent(getBaseContext(), NoticiasValor.class);
    intent.putExtra("valor",noticiasDados);
    startActivity(intent);
}

Receiving the data

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.noticas_valor_layout);

    NoticiasDados noticiasDados = (NoticiasDados) getIntent().getSerializableExtra("valor");
    TextView a = (TextView) findViewById(R.id.txttitulo2);
    a.setText(noticiasDados.getTitulo());
    TextView b = (TextView) findViewById(R.id.txttexto2);
    b.setText(noticiasDados.getTexto());
}

Remembering that your ClassDates class should implement Serializable .

    
08.09.2017 / 15:26