ArrayAdapter中的getView()表示行1位于位置0,而不是正确的位置1

时间:2018-08-18 00:51:15

标签: java android android-studio listview android-arrayadapter

问题

我有一个ListView和一个TextViewEditText。我在EditText上有一个addTextChangeListener(),所以我可以立即将文本保存到ArrayList中,以便可以在父活动中检索它。 *由于某种原因,当我在第1行时,它认为位置是第0行,并将EditText信息从第1行保存到数组的位置0。然后继续将行1的信息保存到数组的位置1。所以我有第1行信息的两倍。之后的所有其他事情都是完全正常的。

有人对如何解决这个问题有任何想法吗?


ArrayAdapter

class CustomListViewEntryAdapter extends ArrayAdapter<ThoughtClass> {

private ArrayList<ThoughtClass> mListOfThoughtClass;
private ThoughtClass mThoughtClass;
private int mNumberOfThought;
private String mThoughtEntry;

static class ViewHolder {
    CardView cardView;
    EditText editText;
    TextView textView;
}

public CustomListViewEntryAdapter(@NonNull Context context, ArrayList<ThoughtClass> thoughtClass) {
    super(context, R.layout.listview_entry_edittext_row ,thoughtClass);

    mListOfThoughtClass = thoughtClass;
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    ViewHolder viewHolder;
    View currentView = convertView;

    if(convertView == null) {
        LayoutInflater davidsInflater = LayoutInflater.from(getContext());
        currentView = davidsInflater.inflate(R.layout.listview_entry_edittext_row, parent, false);

        viewHolder = new ViewHolder();
        viewHolder.cardView = currentView.findViewById(R.id.thoughtCard);
        viewHolder.textView = currentView.findViewById(R.id.thoughtNumber);
        viewHolder.editText = currentView.findViewById(R.id.thoughtEntry);

        // associate the holder with the view for later lookup
        currentView.setTag(viewHolder);
    }
    else {
        // view already exists, get the holder instance from the view
        viewHolder = (ViewHolder) currentView.getTag();
    }

    viewHolder.editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            mListOfThoughtClass.get(position).setThoughtEntry(s.toString());
        }
    });

    mThoughtClass = mListOfThoughtClass.get(position);
    mNumberOfThought = mThoughtClass.getNumberOfThought();
    if(mThoughtClass.getThoughtEntry() == null) {
        mThoughtEntry = "";
    }
    else {
        mThoughtEntry = mThoughtClass.getThoughtEntry();
    }

    // Set objects
    viewHolder.textView.setText( Integer.toString(mNumberOfThought) );
    if(!mThoughtEntry.equals("") || mThoughtEntry != null) {
        viewHolder.editText.setText(mThoughtEntry);
    }

    return currentView;
}
}

ThoughtClass

public class ThoughtClass {

private int mNumberOfThought;
private String mThoughtEntry;
private StorageDistortions mStorageDistortions;

public ThoughtClass(int numberOfThought) {
    this.mNumberOfThought = numberOfThought;
    this.mThoughtEntry = "";
    this.mStorageDistortions = new StorageDistortions();
}

public ThoughtClass(int numberOfThought, String thoughtEntry){
    this.mNumberOfThought = numberOfThought;
    this.mThoughtEntry = thoughtEntry;
}

public ThoughtClass(int numberOfThought, StorageDistortions storageDistortions){
    this.mNumberOfThought = numberOfThought;
    this.mStorageDistortions = storageDistortions;
}

public int getNumberOfThought() {
    return mNumberOfThought;
}

public void setNumberOfThought(int numberOfThought) {
    mNumberOfThought = numberOfThought;
}

public String getThoughtEntry() {
    return mThoughtEntry;
}

public void setThoughtEntry(String thoughtEntry) {
    mThoughtEntry = thoughtEntry;
}

public StorageDistortions getStorageDistortions() {
    return mStorageDistortions;
}

public void setStorageDistortions(StorageDistortions storageDistortions) {
    mStorageDistortions = storageDistortions;
}
}

修改 我忘了提到这一部分。在父视图中,有一个添加按钮,用于向适配器引入新成员。我认为这不会影响定位。

public void addNewThought() {
    ThoughtClass thoughtClass = new ThoughtClass(++mNumberOfThoughts);
    mThoughtClassList.add( thoughtClass );

    setListViewHeightBasedOnItems(mNegativeThoughtsListView);
    mNegativeThoughtsAdapter.notifyDataSetChanged();
}

public boolean setListViewHeightBasedOnItems(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();
    mThoughtClassList = mNegativeThoughtsAdapter.getItems();
    if (listAdapter != null) {

        int numberOfItems = listAdapter.getCount();

        // Get total height of all mItems.
        int totalItemsHeight = 0;
        for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
            View item = listAdapter.getView(itemPos, null, listView);
            item.measure(0, 0);
            totalItemsHeight += item.getMeasuredHeight();
        }

        // Get total height of all item dividers.
        int totalDividersHeight = listView.getDividerHeight() *
                (numberOfItems - 1);

        // Set list height.
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalItemsHeight + totalDividersHeight;
        listView.setLayoutParams(params);
        listView.requestLayout();

        return true;

    } else {
        return false;
    }

}

0 个答案:

没有答案