2017-12-04 6 views
1

状況:私はリストビューでクラスのリストを持っています。それは正常に動作しています。さて、リスト項目をクリックすると、私が表示したいとき重複名から1つの値を取得するにはどうすればよいですか?

enter image description here

English 
Math 
French 

これは、クラスのデータベースです:私はこのようなリスト内のクラスを取得することができていますそのクラスのセクションであるので、Englishリスト項目をクリックすると、そのクラスのセクションに警告ダイアログが表示されます。私はこのようなことを行うことができる午前:

listclasses.setLongClickable(true); 

listclasses.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
    @Override 
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
     final String selectedFromList = (String) listclasses.getItemAtPosition(position); 
     final DatabaseReference retriev = FirebaseDatabase.getInstance().getReference().child("Class"); 
     retriev.orderByChild("Classname").equalTo(selectedFromList).addListenerForSingleValueEvent(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       for(DataSnapshot data: dataSnapshot.getChildren()){ 

        String key=data.getKey(); 

        DatabaseReference referes=FirebaseDatabase.getInstance().getReference().child("Class"); 
        referes.orderByKey().equalTo(key).addListenerForSingleValueEvent(new ValueEventListener() { 
         @Override 
         public void onDataChange(DataSnapshot dataSnapshot) { 
          for(DataSnapshot datas: dataSnapshot.getChildren()){ 

          String section=datas.child("section").getValue().toString(); 

          LayoutInflater li = LayoutInflater.from(StudentSearchActivity.this); //inflate converts xml to a view object to use in code 
          View promptsView = li.inflate(R.layout.info, null); //two parameters, the xml and the root(null means the layout is a child of viewgroup 

          AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            StudentSearchActivity.this); /*alertdialog.builder class,creates a builder for alert dialog, parameter is context */ 

          alertDialogBuilder.setView(promptsView); 
          final TextView sections = (TextView) promptsView.findViewById(R.id.sections); 
          sections.setText(section); 
          alertDialogBuilder.setCancelable(true); //true can be canceled with back key 
          alertDialogBuilder.setPositiveButton("Ok", 
            new DialogInterface.OnClickListener() { 
             public void onClick(DialogInterface dialog, int id) { 
              dialog.dismiss(); 
             } 
            }); 
          AlertDialog alertDialog = alertDialogBuilder.create(); //creates alert dialog from builder 
          alertDialog.show(); 
         }} 

問題:私は同じクラス名例Englishを持っていますが、それぞれのセクションにあるとき、私はアイテムの長いクリックで両方のセクションを取得します。例:

手順1:Englishの項目を長くクリックします。

手順2:セクション503の警告ダイアログが表示されます。

ステップ3:バックプレスまたはokを押すと、セクション:402の別のアラートダイアログが表示されます。

したがって、両方のセクションが同じ名前であるため、1つのクラスを長時間クリックすると表示されますが、これは間違っています。その英語クラスのセクションのみが表示され、他の英語クラスをクリックすると、この英語クラスのセクションも表示されます。これはFirebaseで解決できますか?

私はaddListenerForSingleValueEventを使用していますが、コードではClassnameというデータベースも使用しています。

+0

ですか? –

+0

私はそれが解決できるかどうか尋ねる?そしてどうやって?私はちょうど両方のセクションではなく、重複した名前を長時間クリックしたいのです –

+0

リストに1つの "英語"を表示するには??? –

答えて

0

listclassesの値をハードコードしていて、その値を使用してClassのいずれかを照会すると、複数の子を取得する可能性があります。あなたのスクリーンショットに基づいてclassnameに同じ値を含む子が2つあるためです。

あなたはClassノードにリスナーを添付して、データベースの値に基づいてlistclassesを移入した後、自分のsection値でAlertDialogを示し、それらの一つ一つにOnClickListenerまたはOnLongItemClickListenerを設定する必要があります。

はここで解決ですから、そのような場合に何をしたいのか

// to store the class's value 
List<Class> classes = new ArrayList<>(); 

// to retrieve the list of class from the database 
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Class"); 
ref.addChildEventListener(new ChildEventListener() { 
    @Override 
    public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
     Class class = dataSnapshot.getValue(Class.class); 
     classes.add(class); 

     ... add 'class.getClassName()' to the listclasses adapter 
    } 
}); 

// add the OnItemLongClickListener 
listclasses.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
    @Override 
    public boolean onItemLongClick(AdapterView <<?>parent, View view, int position, long id) { 
     Class selectedClass = classes.get(position); 

     ... show an AlertDialog with the value of 'selectedClass.getSection()' 
    } 
}); 
+0

これは何をしているのですか?データベースから 'listclasses'を読み込んでいますが、モデルクラスを使用していないので、Stringを実行しています。classnames = dataSnapshot.child(" Classname ")。getValue()。toString();次にlistofclasses.add(クラス名); –

+0

私はそれがちょうど仕事をしないと思う –

関連する問題