Reputation: 7
I got a listView in a fragment. This listView use an BaseExpandableListAdapter. I got a list of question and for each of them, I have a list of answer. I add a Edittext after each last answer == last child (to add a new answer).
My problem is that each edittext has the same ID, so when I have more than one question, I can't write inside the edittext because two edittext have the same Id. I don't know how to manage it :'(.
-> Fragment code :
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);
}
-> Adapter code :
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 of the footer
<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>
If you have any idea how to resolve it, it will help me a lot ! Thank you.
Upvotes: 0
Views: 746
Reputation: 833
You can use setTag() method of view. So whenever your getChildView() executes just set editText.setTag(position). Once user submit the answer you just need to find the tag that in which edit test user has typed by edittext.getTag(). It will return you the position which you tagged at the time of getChildView() execution. By that way you can get to know different answers once you have more than 1 question.
getChildView() Code
@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;
}
Let me know if it work or you need more descriptive answer.
Upvotes: 1