2017-06-06 1 views
2

Rating ActivityEnvoi valeur des lignes à WebService au format JSON (Android)

Tâche: application reçoit des valeurs au format JSON et Nom de l'élève et note actuelle sont dynamiquement à la Liste. Maintenant, l'application doit envoyer une note et des commentaires pour chaque étudiant au format JSON. Problème: Les valeurs indiquées dans Notation et commentaire peuvent être récupérées dans la classe Adapter (où une seule ligne est remplie) mais ne sont pas accessibles en dehors de cette classe. Je veux accéder à ces valeurs dans la méthode ListView.onClickListener.

Cette classe est Adaptateur:

class StudentAdapter extends BaseAdapter { 

    ArrayList<Student> studentList; 
    ArrayList<Student> studentList1; 
    LayoutInflater inflater; 

    public StudentAdapter(ArrayList<Student> studentList) { 
     this.studentList = studentList; 
     inflater = LayoutInflater.from(RatingActivity.this); 
    } 

    @Override 
    public int getCount() { 
     return studentList.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return studentList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) 
    { 
     final View row = inflater.inflate(R.layout.rowactivity3, parent, false); 
     // TextView id = (TextView) row.findViewById(R.id.tvId); 
     TextView name = (TextView) row.findViewById(R.id.tvName); 
     final RatingBar rv=(RatingBar)row.findViewById(R.id.ratingBar); 
     final RatingBar rv1=(RatingBar)row.findViewById(R.id.ratingBar1); 
     final Button b1=(Button)row.findViewById(R.id.btnDialog); 
     final String[] values = {null,null}; 
     name.setText(studentList.get(position).getSname()); 
     rv1.setRating(studentList.get(position).getRate()); 
     b1.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) { 
       AlertDialog.Builder builder = new AlertDialog.Builder(RatingActivity.this); 
       builder.setTitle("Comment here:"); 

       final EditText input = new EditText(RatingActivity.this); 
       input.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_CLASS_TEXT); 
       builder.setView(input); 

       builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         m_Text = input.getText().toString(); 
         Log.d("comment",m_Text); 
         values[0]=m_Text; 
         b1.setBackgroundColor(Color.BLUE); 
        } 
       }); 
       builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 
        } 
       }); 
       builder.show(); 
      } 
     }); 
     return row; 
    } 
} 

Ceci est la mise en page de la ligne unique:

<TextView 
    android:layout_margin="8dp" 
    android:id="@+id/tvName" 
    android:text="enter name.." 
    android:padding="12dp" 
    android:layout_width="220dp" 
    android:layout_height="wrap_content" 
    android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/> 
<RatingBar 
    android:id="@+id/ratingBar" 
    style="@style/customRatingBar" 
    android:layout_marginLeft="15dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:numStars="5" 
    android:stepSize="0.1" /> 
<Button 
    android:id="@+id/btnDialog" 
    android:layout_width="100dp" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="70dp" 
    android:text="Click"/> 
<RatingBar 
    android:id="@+id/ratingBar1" 
    android:layout_marginLeft="80dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:numStars="5" 
    android:isIndicator="true" 
    android:stepSize="0.1" /> 

+0

Vous pouvez essayer ListView.setOnItemClickListener –

+0

J'ai essayé cela, il ne fonctionne pas lorsque la barre d'évaluation et les boutons sont présents dans la rangée. – user5166776

Répondre

0

Essayez ceci:

  1. Créer une interface

    public interface AdapterItemClickListener { 
        void AdapterItemClicked(int position, Bundle data); 
    } 
    
  2. Ensuite, ajoutez le code dans votre classe d'adaptateur:

    class StudentAdapter extends BaseAdapter { 
    
    ArrayList<Student> studentList; 
    ArrayList<Student> studentList1; 
    LayoutInflater inflater; 
    AdapterItemClickListener adapterItemClickListener; 
    
    public StudentAdapter(ArrayList<Student> studentList) { 
    this.studentList = studentList; 
    inflater = LayoutInflater.from(RatingActivity.this); 
    } 
    
    @Override 
    public int getCount() { 
    return studentList.size(); 
    } 
    
    @Override 
    public Object getItem(int position) { 
    return studentList.get(position); 
    } 
    
    @Override 
    public long getItemId(int position) { 
    return position; 
    } 
    
    @Override 
    public View getView(final int position, View convertView,  ViewGroup parent) 
    { 
    final View row = inflater.inflate(R.layout.rowactivity3, parent, false); 
    // TextView id = (TextView) row.findViewById(R.id.tvId); 
    TextView name = (TextView) row.findViewById(R.id.tvName); 
    final RatingBar rv=(RatingBar)row.findViewById(R.id.ratingBar); 
    final RatingBar rv1=(RatingBar)row.findViewById(R.id.ratingBar1); 
    final Button b1=(Button)row.findViewById(R.id.btnDialog); 
    final String[] values = {null,null}; 
    name.setText(studentList.get(position).getSname()); 
    rv1.setRating(studentList.get(position).getRate()); 
    b1.setOnClickListener(new View.OnClickListener() 
    { 
        @Override 
        public void onClick(View v) { 
         AlertDialog.Builder builder = new AlertDialog.Builder(RatingActivity.this); 
         builder.setTitle("Comment here:"); 
    
         final EditText input = new EditText(RatingActivity.this); 
         input.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_CLASS_TEXT); 
         builder.setView(input); 
    
         builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           m_Text = input.getText().toString(); 
           Log.d("comment",m_Text); 
           values[0]=m_Text; 
           b1.setBackgroundColor(Color.BLUE); 
         //pass your data in bundle and pass adapter position 
        adapterItemClickListener.AdapterItemClicked(position,bundle); 
        dialog.cancel(); 
          } 
         }); 
         builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           dialog.cancel(); 
          } 
         }); 
         builder.show(); 
        } 
    }); 
    return row; 
    } 
    
    // create adapter item click listener and call from fragment or activity class 
    public void setAdapterItemClickListener(AdapterItemClickListener adapterItemClickListener){ 
        this.adapterItemClickListener=adapterItemClickListener; 
    } 
    
    } 
    
  3. Ensuite, appelez studentAdapter.setAdapterItemClickListener (ce) du fragment ou activité et passer outre vide AdapterItemClicked (position int, données Bundle); . Vous obtenez des données en bundle dans cette méthode.

Remarque: Ce code n'a pas été testé. Donc, si vous rencontrez un problème, puis déboguer et vérifier. J'espère que cela fonctionnera.

+0

Veuillez indiquer comment implémenter studentAdapter.setAdapterItemClickListener (this) dans activity. – user5166776

+0

Appelez 'studentAdapter.setAdapterItemClickListener (this)' à partir de l'activité où vous avez défini 'StudentAdapter'. Ensuite, 'studentAdapter.setAdapterItemClickListener (this)' affiche une erreur pour remplacer la méthode AdapterItemClicked (int position, Bundle data). Vous devez donc remplacer la méthode 'AdapterItemClicked (int position, Bundle data)'. –

+0

Fait maintenant. Merci. mais une autre demande est autorisée monsieur? – user5166776