【问题标题】:android listview, edittext same IDandroid listview,edittext同ID
【发布时间】:2016-04-14 09:29:57
【问题描述】:

我在片段中有一个 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);

}

-> 适配器代码:

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

-> 页脚的 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

【问题讨论】:

  • 尝试自己调用 setId() 并为每次视图膨胀使用不同的 id。
  • 我在 getChildView 中尝试过,但它不起作用 :(.final EditText editText = (EditText)convertView.findViewById(R.id.footer_answer_edit_text); editText.setId(editText.getId() + groupPosition);
  • 不,请注意,使用 editText.setId(someNumberOfYourOwn) 并在放下下一个 editText 时执行相同操作,但除了最后一个之外的其他数字,请跟踪这些 id,因为您需要稍后通过它们获取editTexts的值。
  • 但是我不知道 parent 的数量(数据库中的问题),所以在适配器的构造器中,我应该创建一个 ID 选项卡?我还没有把 setID 放在哪里? (我在哪里做的?)
  • 我做了这个:editText.setId((int) id.get(groupPosition)) 我做了。在构造函数中我这样做了: for (int i = 0 ; i

标签: android android-listview android-edittext


【解决方案1】:

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

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;

}

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

【讨论】:

  • 谢谢,但我什至不能在一个编辑文本中写的问题(我在帖子底部更新了一张照片)。我真的不知道为什么我不能在里面写...
  • 能否请您通过使活动窗口软输入模式调整调整大小来检查
  • 谢谢!只是这样:'(。
猜你喜欢
  • 2011-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-05
  • 2014-08-10
  • 2013-10-13
  • 1970-01-01
相关资源
最近更新 更多