Constantin N.
Constantin N.

Reputation: 2839

How to get the text of an EditText inside a ListView?

I am trying to get the text of an EditText by pressing a button. Both are in a ListView

Here is my QuestionAdapter

 public View getView(final int position, View contentView, ViewGroup parent) {

            if (contentView==null){
                contentView= LayoutInflater.from(c).inflate(R.layout.actu_list_item,parent,false);
            }
            TextView asker = (TextView) contentView.findViewById(R.id.asker_name);
            TextView date = (TextView) contentView.findViewById(R.id.date);
            TextView title = (TextView) contentView.findViewById(R.id.question_title);
            TextView content = (TextView) contentView.findViewById(R.id.question_content);
            Button answer = (Button) contentView.findViewById(R.id.answer);
             EditText answerET= (EditText)  contentView.findViewById(R.id.answer_edittext);
             answer_text = answerET.getText().toString();
            posit = this.getItem(position);
            answer.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {

                        Toast.makeText(c,"Post en cours...",Toast.LENGTH_LONG).show();
                        Answer answerClass = (Answer) posit;
                        FirebaseDatabase database = FirebaseDatabase.getInstance();
                        FirebaseAuth firebaseAuth1 = FirebaseAuth.getInstance();
                        FirebaseUser firebaseUser= firebaseAuth1.getCurrentUser();
                        qe= (Questions) posit;
                        SimpleDateFormat dateFormat=new SimpleDateFormat("HH:mm dd-MM-yyyy", Locale.FRANCE);
                        String date= dateFormat.format(new Date(System.currentTimeMillis()));
                        assert  firebaseUser !=null;
                        DatabaseReference Asker = database.getReference(firebaseUser.getUid()+"/name");
                        Asker.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                name = dataSnapshot.getValue(String.class);

                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });
                        answerClass.setAnswer(answer_text);
                        answerClass.setUsername(name);
                        answerClass.setDate(date);
                        DatabaseReference AnswerRef =database.getReference("Questions/"+qe.getTag_id());
                        AnswerRef.push().setValue(answerClass).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                Toast.makeText(c,"Reponse postée",Toast.LENGTH_LONG).show();
                            }
                        });
                }
            });

            qe= (Questions) this.getItem(position);
            asker.setText(qe.getQuestion_username());
            date.setText(qe.getQuestion_date());
            title.setText(String.format("Q: %s", qe.getQuestion_title()));
            content.setText(qe.getQuestion_content());

            contentView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(c,qe.getQuestion_username(),Toast.LENGTH_LONG).show();
                }
            });

            return contentView;
        }

Here is my Activity where i'm displaying the listView.

when type text in the EditText then click on the related button, nothing is shown.

public class Actu extends Fragment {
    private DatabaseReference db;
    FirebaseHelper helper;
    QuestionAdapter adapter;
    ListView lv;
    View v;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
                container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.activity_actu, container, false);
        lv = (ListView) v.findViewById(R.id.actu_rv);
        progressBar = (ProgressBar) v.findViewById(R.id.progress_bar);
        no_connection =(TextView) v.findViewById(R.id.no_connection);
        db = FirebaseDatabase.getInstance().getReference("Questions");
        db.limitToLast(100).orderByKey();
        helper = new FirebaseHelper(db);
        adapter = new QuestionAdapter(getContext(),helper.retreive());
        lv.setAdapter(adapter);


        return v;        }

}

I tried many ways but nothing works. Can someone help me?

Upvotes: 1

Views: 78

Answers (1)

Leśniakiewicz
Leśniakiewicz

Reputation: 904

In short words - You have to put

answer_text = answerET.getText().toString();

inside onClickListener

answer.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                 answer_text = answerET.getText().toString();

                 ...
            }
}

PS. Your code is very hard to read

Upvotes: 1

Related Questions