Array elements based on position

Asked

Viewed 235 times

2

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

class sending 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());
}
  • 1

    And what have you tried to do? What was the difficulty?

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 News class should implement Serializable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.