I got key for every item in the listView and its saved to Firebase I want to send that key to another activity but every time I got the same key

huangapple 未分类评论41阅读模式
英文:

I got key for every item in the listView and its saved to Firebase I want to send that key to another activity but every time I got the same key

问题

mChildEventListner = new ChildEventListener() {
    @Override
    public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
        AdClass adClass = dataSnapshot.getValue(AdClass.class);
        list.add(0, adClass);
        listView.setAdapter(adapter);

        // here I save the key in a global variable named listId.
        listId = dataSnapshot.getKey();
    }
};

// - here I send that id via intent to another activity
AdsList.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getContext(), AdsDescrption.class);
        intent.putExtra("resId", listId);
        startActivity(intent);
    }
});

// - and the problem is I always get the same key
Intent intent = getIntent();
resId = intent.getStringExtra("resId");
英文:
      mChildEventListner = new ChildEventListener() {
          @Override
           public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                AdClass adClass = dataSnapshot.getValue(AdClass.class);
              list.add(0,adClass);
               listView.setAdapter(adapter);
  • here I save the key in global variable named listId.

           listId = dataSnapshot.getKey();
           }
    
         -here I send that id via intent to another activity 
         AdsList.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent intent = new Intent(getContext(),AdsDescrption.class);
                  intent.putExtra("resId",listId);
                  startActivity(intent);
              }
            });
    

-and the problem I always get the same key

        Intent intent = getIntent();
        resId =intent.getStringExtra("resId");

答案1

得分: 0

使用

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        //Your code goes here

    }
});
英文:

Use

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {

                //Your code goes here

            }
        });

huangapple
  • 本文由 发表于 2020年4月5日 18:47:32
  • 转载请务必保留本文链接:https://java.coder-hub.com/61041374.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定