2016-12-15 15 views
-2

私はFirebaseを使ってチャットアプリを開発しています。onClickイベントのリストビューアイテムから特定のアイテムを取得するにはどうすればよいですか?

私の問題は、Firebaseデータベースからリストビュー内のすべてのユーザーを表示していることです。リストから任意のユーザーをクリックすると、そのユーザーの電子メールがトーストに表示されます。

リストビューでは、別のテキストビューでユーザーの名前とメールを表示します。では、クリックしたアイテムの電子メールのテキストビューのデータを取得するにはどうすればよいですか?

代わりの解決策を提案してください。

enter image description here

これは私のコードです:

users = (ListView) findViewById(R.id.userlist1); 

    users.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 


      Intent intent = new Intent(UserList.this, OneChat.class); 
      String p = "p"; 
      //Toast.makeText(getApplicationContext(), i+""+users.getItemAtPosition(i).toString(), Toast.LENGTH_LONG).show(); 
      intent.putExtra(p,i); 
      startActivity(intent); 

     }}); 



    List<user> allu = new ArrayList<>(); 
    uad = new useradapter(this, R.layout.item_user, allu); 
    users.setAdapter(uad); 

    firebaseAuth0 = FirebaseAuth.getInstance(); 


    FirebaseUser user0 = firebaseAuth0.getCurrentUser(); 
    if (user0 != null) { 
     // User is signed in 
     attachDatabaseReadListener(); 
    } else { 
     // User is signed out 
     detachDatabaseReadListener(); 
    } 


    attachDatabaseReadListener(); 

} 

private void attachDatabaseReadListener() { 
    if (mChildEventListener == null) { 

     mChildEventListener = new ChildEventListener() { 
      @Override 
      public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
       user userf = dataSnapshot.getValue(user.class); 
       uad.add(userf); 
       progressDialog.hide(); 
       //mProgressBar.setVisibility(ProgressBar.INVISIBLE); 
       // tvw=(TextView) findViewById(R.id.textView3); 
       //tvw.setVisibility(View.GONE); 
      } 

      public void onChildChanged(DataSnapshot dataSnapshot, String s) { 
      } 

      public void onChildRemoved(DataSnapshot dataSnapshot) { 

      } 

      public void onChildMoved(DataSnapshot dataSnapshot, String s) { 
      } 

      public void onCancelled(DatabaseError databaseError) { 
      } 
     }; 
     udbr.addChildEventListener(mChildEventListener); 


    }} 

private void detachDatabaseReadListener() { 
    if (mChildEventListener != null) { 
     udbr.removeEventListener(mChildEventListener); 
     mChildEventListener = null; 
    } 
} 
+0

[ここから参照を取得することがあります何かを得る](http://stackoverflow.com/questions/38744080/after-filter-listview-i-am-not-able-to-get-actual-position-アイテムイン-アンドロイド) –

答えて

1

私は、すべてのユーザーが含まれているあなたのALLUリストを想定...このリストにグローバル変数を作成してからonItemSelectListenerにこの

ような何かを行います
users.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      Toast.makeText(getApplicationContext(), allu.get(i).getEmail(), Toast.LENGTH_LONG).show(); 
      Intent intent = new Intent(UserList.this, OneChat.class); 
      intent.putExtra("p",i); 
      startActivity(intent); 
     }}); 
関連する問題