-2

I am developing a small project in android studio where I have a listview where the 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());
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
JavaTest
  • 11
  • 4
  • So what is the problem? Title of the question is too confusing. I am unable to understand what you are not getting? – Birendra Singh Sep 07 '17 at 16:10
  • For getting the position your code seems correct and for item depends on how your `mList` is stored in the memory. – Birendra Singh Sep 07 '17 at 16:16
  • @BirendraSingh in the page that receives the position value, I can not go to the array and display the data. I made debug and in the page that sends the position everything is correct but in the page that receives, I can not get the value of the position – JavaTest Sep 07 '17 at 16:31
  • Then pass the value itself. As your code shows you use two Strings from the item at clicked position, pass both the strings via intent. If you need to pass the whole object then you need to serialize your item. Either implement Parcelable or save the item properties in SharedPreferences etc. and reconstruct the item from SharedPreferences values. – Birendra Singh Sep 07 '17 at 16:40
  • If you need example code let me know. – Birendra Singh Sep 07 '17 at 16:43
  • @BirendraSingh can you give example pls – JavaTest Sep 07 '17 at 16:49

2 Answers2

0

In your onListItemClick() method, change this code:

mList.get(position);
Intent intent = new Intent(getBaseContext(), NoticiasValor.class);
intent.putExtra("valor","position");
startActivity(intent);

to this:

NoticiasDados item = mList.get(position);
Intent intent = new Intent(getBaseContext(), NoticiasValor.class);
intent.putExtra("valor",item);
startActivity(intent);

And then in your receiving activity, change this:

int intValue= getIntent().getIntExtra("valor",0);

to this:

NoticiasDados item = getIntent().getParcelableExtra("valor");

Note that this requires that your NoticiasDados class implements the Parcelable interface. If it is not already, you can check this out to learn more: How can I make my custom objects Parcelable?

Ben P.
  • 52,661
  • 6
  • 95
  • 123
0

I modified your onListItemClick method so that instead of passing the string "position" in the intent instead it passes the variable i which contains the object at the position that the user clicked.

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

I also modified the onCreate method in the class that receives the intent to utilise the get methods you created in the NoticiasDados class.

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

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

Also please make sure your NoticiasDados class implements Serializable

Just The Highlights
  • 1,555
  • 19
  • 31