如何在ListView中获取EditText的文本?

时间:2017-10-30 14:09:04

标签: android listview android-edittext

我想通过按button来获取ListView的文字。两者都在 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; }

这是我的QuestionAdapter

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;        }

}

这是我的Activity,我正在显示listView。

当在EditText中键入文本然后单击相关按钮时,不会显示任何内容。

{{1}}

我尝试了很多方法但没有任何作用。有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

简而言之 - 你必须把

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

在onClickListener

answer.setOnClickListener(new View.OnClickListener() {

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

                 ...
            }
}

PS。你的代码很难阅读