android listview,edittext同一个ID

时间:2016-01-09 20:12:33

标签: android android-listview android-edittext

我在片段中有一个listView。此listView使用BaseExpandableListAdapter。我得到了一个问题清单,对于每一个问题,我都有一份答案清单。我在每个最后一个答案==最后一个孩子后添加一个Edittext(添加一个新答案)。

我的问题是每个edittext都有相同的ID,因此当我有多个问题时,我无法在edittext中写入,因为两个edittext具有相同的ID。 我不知道如何管理它:'(

- >片段代码:

public class HomeFragment extends Fragment implements AbsListView.OnItemClickListener {

private OnFragmentInteractionListener mListener;

/**
 * The fragment's ListView/GridView.
 */
private AbsListView mListView;

/**
 * The Adapter which will be used to populate the ListView/GridView with
 * Views.
 */
private AdapterHomeFragment adapter;

private LinkedList<Question> listQuestion;

private ExpandableListView listView;

public static HomeFragment newInstance() {
    HomeFragment fragment = new HomeFragment();

    return fragment;
}



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    listQuestion = QuestionManager.getQuestionManager().getQuestionWithAnswerNotFromMe();

    View rootView = inflater.inflate(R.layout.home_fragment, container, false);
    listView = (ExpandableListView) rootView.findViewById(R.id.listView);
    adapter = new AdapterHomeFragment(getActivity(), listQuestion);
    listView.setAdapter(adapter);
    listView.setGroupIndicator(null);

    return rootView;
}


/*
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }
}
*/



@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    if (null != mListener) {
        // Notify the active callbacks interface (the activity, if the
        // fragment is attached to one) that an item has been selected.

    }
}

/**
 * The default content for this Fragment has a TextView that is shown when
 * the list is empty. If you would like to change the text, call this method
 * to supply the text it should use.
 */
public void setEmptyText(CharSequence emptyText) {
    View emptyView = mListView.getEmptyView();

    if (emptyView instanceof TextView) {
        ((TextView) emptyView).setText(emptyText);
    }
}


public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    public void onFragmentInteraction(String id);
}

public void refreshData()
{

    listQuestion = QuestionManager.getQuestionManager().getQuestionWithAnswerNotFromMe();
    adapter = new AdapterHomeFragment(getActivity(), listQuestion);
    listView.setAdapter(adapter);
    listView.setGroupIndicator(null);

}

- &GT;适配器代码:

public class AdapterHomeFragment extends BaseExpandableListAdapter {

private LinkedList<Question> groups;
public LayoutInflater inflater;
public Activity activity;

public AdapterHomeFragment(Activity act, LinkedList<Question> groups) {
    activity = act;
    this.groups = groups;
    inflater = act.getLayoutInflater();
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return groups.get(groupPosition).getListAnswer().get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return 0;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {




    if (!isLastChild) {
        Answer answer = (Answer) getChild(groupPosition, childPosition);

        TextView text = null;

        convertView = inflater.inflate(R.layout.rowanswer_home, null);

        text = (TextView) convertView.findViewById(R.id.rowanswer_home_answer);
        text.setText(answer.getAnswer());

        text = (TextView) convertView.findViewById(R.id.rowanswer_home_author);
        text.setText("Anonymous"+answer.getUserId().toString());

        text = (TextView) convertView.findViewById(R.id.rowanswer_home_time);
        text.setText(answer.getDifferenceTime()+" ago");


    }
    else
    {
        TextView text = null;

        convertView = inflater.inflate(R.layout.footer_answer, null);
        final Question question = groups.get(groupPosition);

        final EditText editText = (EditText)convertView.findViewById(R.id.footer_answer_edit_text);

        final Button button = (Button) convertView.findViewById(R.id.footer_answer_button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                String text_answer = editText.getText().toString();
                QuestionManager.getQuestionManager().addAnswer(text_answer, question.getId());
                groups = QuestionManager.getQuestionManager().getQuestionWithAnswerNotFromMe();
                notifyDataSetChanged();


            }
        });
        button.setEnabled(false);

        editText.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {

                // you can call or do what you want with your EditText here
                String text = editText.getText().toString();
                if (verifyFormatString(text)) {
                    button.setEnabled(true);
                } else
                    button.setEnabled(false);

            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
        });

        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

        Log.d("test5780", String.valueOf(editText.getId()));


    }

    return convertView;


}

@Override
public int getChildrenCount(int groupPosition) {
    return groups.get(groupPosition).getListAnswer().size() + 1;
}

@Override
public Object getGroup(int groupPosition) {
    return groups.get(groupPosition);
}

@Override
public int getGroupCount() {
    return groups.size();
}

@Override
public void onGroupCollapsed(int groupPosition) {
    super.onGroupCollapsed(groupPosition);
}

@Override
public void onGroupExpanded(int groupPosition) {
    super.onGroupExpanded(groupPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return 0;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    int nbAnswer;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.rowquestion_home, null);
    }

    Question question = (Question) getGroup(groupPosition);
    ((CheckedTextView) convertView.findViewById(R.id.rowquestion_home_question)).setText(question.getQuestion());
    ((CheckedTextView) convertView.findViewById(R.id.rowquestion_home_question)).setChecked(isExpanded);
    ((TextView) convertView.findViewById(R.id.rowquestion_home_author)).setText("Anonymous" + question.getUserId().toString());
    ((TextView) convertView.findViewById(R.id.rowquestion_home_time)).setText(question.getDifferenceTime()+ " ago");

    nbAnswer = question.getListAnswer().size();
    if (nbAnswer == 1)
        ((TextView) convertView.findViewById(R.id.rowquestion_home_nbAnswer)).setText(String.valueOf(question.getListAnswer().size()) + " answer");
    else
        ((TextView) convertView.findViewById(R.id.rowquestion_home_nbAnswer)).setText(String.valueOf(question.getListAnswer().size()) + " answers");



    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

public boolean verifyFormatString (String question)
{
    boolean valid = false;
    int i = 0;

    while (!valid && i < question.length())
    {
        if (question.charAt(i) != ' ')
            valid = true;

        i += 1;
    }

    return valid;
}

- &GT;页脚的Xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="40dp"
android:background="@color/layout_button"
tools:context=".MainActivity" >

<View
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/footer_answer_block"
    />

<EditText
    android:id="@+id/footer_answer_edit_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawablePadding="5dp"
    android:gravity="center_vertical"
    android:hint="Add answer"
    android:textSize="14sp"
    android:textStyle="italic"
    android:layout_toLeftOf="@+id/footer_answer_button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">
</EditText>

<Button
    android:id="@+id/footer_answer_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:lines="1"
    android:textSize="12sp"
    android:text="add"


    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/footer_answer_block">
</Button>

如果你有任何想法如何解决它,它将帮助我很多! 谢谢。

When I click on the edittext the keyboard open but instant lose the focus on the edittext and it's impossible to write inside

1 个答案:

答案 0 :(得分:1)

您可以使用setTag()方法查看。所以每当你的getChildView()执行时,只需设置editText.setTag(position)。用户提交答案后,您只需找到编辑测试用户通过edittext.getTag()键入的标记。它将返回您在执行getChildView()时标记的位置。通过这种方式,一旦您有一个以上的问题,您就可以了解不同的答案。

getChildView()代码

@Override

public View getChildView(int groupPosition,final int childPosition,                          boolean isLastChild,View convertView,ViewGroup parent){

if (!isLastChild) {
    Answer answer = (Answer) getChild(groupPosition, childPosition);

    TextView text = null;

    convertView = inflater.inflate(R.layout.rowanswer_home, null);

    text = (TextView) convertView.findViewById(R.id.rowanswer_home_answer);
    text.setText(answer.getAnswer());

    text = (TextView) convertView.findViewById(R.id.rowanswer_home_author);
    text.setText("Anonymous"+answer.getUserId().toString());

    text = (TextView) convertView.findViewById(R.id.rowanswer_home_time);
    text.setText(answer.getDifferenceTime()+" ago");


}
else
{
    TextView text = null;

    convertView = inflater.inflate(R.layout.footer_answer, null);
    final Question question = groups.get(groupPosition);

    final EditText editText = (EditText)convertView.findViewById(R.id.footer_answer_edit_text);
editText.setTag(childPosition);
    final Button button = (Button) convertView.findViewById(R.id.footer_answer_button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            String text_answer = editText.getText().toString();
            QuestionManager.getQuestionManager().addAnswer(text_answer, question.getId());
            groups = QuestionManager.getQuestionManager().getQuestionWithAnswerNotFromMe();
            notifyDataSetChanged();


        }
    });
    button.setEnabled(false);

    editText.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {

            // you can call or do what you want with your EditText here
            String text = editText.getText().toString();
int answeredPosition = (Integer)editText.getTag();
Log.d("Answered Position",""+answeredPosition);// This is the position of question in listview for which user has typed the answer.
            if (verifyFormatString(text)) {
                button.setEnabled(true);
            } else
                button.setEnabled(false);

        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    });

    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

    Log.d("test5780", String.valueOf(editText.getId()));


}

return convertView;

}

让我知道它是否有效或您需要更多描述性答案。