Android列表视图显示空数据,直到显示键盘?

时间:2017-02-19 04:11:11

标签: android listview listadapter

我正在使用自定义列表适配器的列表视图。

当我点击“+”按钮时,会动态添加一个列表项。每个列表项包含4个EditTexts。

第一个列表项显示正确的数据,但其他项目的最右边的EditTexts为空。

这是问题

issue image

您可以看到显示除第一个列表项以外的空数据的最后编辑文本。显示键盘后,此问题已解决。

打开软键盘后

Solved image

我尝试使用adapter.notifyDataSetChanged()list.invalidateViews(),但现在已经工作了。

我该如何解决这个问题?

的onCreate()

Vector v;
CreateExerciseAdapter adapter;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_workout);

        v = new 
        adapter = new CreateExerciseAdapter(v,this);

        txtName = (EditText)findViewById(R.id.editTextName);
        list = (ListView)findViewById(R.id.listView2);

        //listview footer button (Add new button)
        LayoutInflater inflater = LayoutInflater.from(this);
        View inflate = inflater.inflate(R.layout.create_exercise_item, null);
        ImageButton btnAddNew = (ImageButton) inflate.findViewById(R.id.imageButtonAddNew);
        list.addFooterView(inflate);

        btnAddNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View item = LayoutInflater.from(view.getContext())
                        .inflate(R.layout.create_new_exercise_item, null);
                v.add(item);
                adapter.notifyDataSetChanged();
            }
        });
        list.setAdapter(adapter);
    }

谢谢:-)

更新

适配器

public class CreateWorkoutListAdapter extends BaseAdapter {
    Context context;
    Vector vector;
    LayoutInflater inflater = null;
    FontManager fm;

    public CreateWorkoutListAdapter(Context context, Vector vector){
        this.context = context;
        this.vector = vector;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return vector.size();
    }

    @Override
    public Object getItem(int position) {
        return vector.elementAt(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {

        view = inflater.inflate(R.layout.create_new_exercise_item, null);

        final EditText txtName = (EditText)view.findViewById(R.id.editTextExerciseName);
        final EditText txtReps = (EditText)view.findViewById(R.id.editTextReps);
        final EditText txtMin = (EditText)view.findViewById(R.id.editTextMin);
        final EditText txtSec = (EditText)view.findViewById(R.id.editTextSecond);


        return view;
    }
}

2 个答案:

答案 0 :(得分:0)

请使用以下内容替换您的getView:

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {

        view = inflater.inflate(R.layout.create_new_exercise_item, viewGroup, false);

        final EditText txtName = (EditText)view.findViewById(R.id.editTextExerciseName);
        final EditText txtReps = (EditText)view.findViewById(R.id.editTextReps);
        final EditText txtMin = (EditText)view.findViewById(R.id.editTextMin);
        final EditText txtSec = (EditText)view.findViewById(R.id.editTextSecond);


        return view;
    }

答案 1 :(得分:-1)

变更:

list = (ListView)findViewById(R.id.yourlistviewid);

Insted of:

list = (ListView)findViewById(R.id.