android listview不能滚动更多,坏的自定义适配器?

7kqas0il  于 2021-07-09  发布在  Java
关注(0)|答案(4)|浏览(473)

edit2:我现在有一个合适的布局设计,我可以滚动,但我的自定义适配器有一个新问题。当我滚动时,我的edittext会丢失它们的内容,并被来自其他元素的其他内容替换。但是当我使用simplecursoradapter时,我没有任何问题(但是我不能使用我的按钮)。
这是我的自定义适配器:

private class MySimpleCursorAdapter extends SimpleCursorAdapter
    {
        ViewHolder vh;
        public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
            super(context, layout, c, from, to, flags);
            cursor = c;
        }

        public View newView(Context _context, Cursor _cursor, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) _context.getSystemService(_context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.card_stock, parent, false);
            vh = new ViewHolder();
            vh.idList = (TextView) view.findViewById(R.id.idList);
            vh.themeList = (TextView) view.findViewById(R.id.themeList);
            vh.questionList = (TextView) view.findViewById(R.id.questionList);
            vh.reponseList = (TextView) view.findViewById(R.id.reponseList);
            vh.difficulteList = (TextView) view.findViewById(R.id.difficultList);
            vh.Supprimer = (Button) view.findViewById(R.id.buttonDelete);
            vh.Modifier = (Button) view.findViewById(R.id.buttonModifier);

            view.setTag(vh);
            return view;
        }

        public void bindView(View view, Context Context, Cursor cursor) {
                vh.idList.setText(cursor.getString(cursor.getColumnIndex(stock._ID)));
                vh.themeList.setText(cursor.getString(cursor.getColumnIndex(stock.THEME)));
                vh.questionList.setText(cursor.getString(cursor.getColumnIndex(stock.QUESTION)));
                vh.reponseList.setText(cursor.getString(cursor.getColumnIndex(stock.REPONSE)));
                vh.difficulteList.setText(cursor.getString(cursor.getColumnIndex(stock.DIFFICULTE)));

            vh.Supprimer.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v) {
                    View parentView = (View)v.getParent();
                    TextView idList = (TextView) parentView.findViewById(R.id.idList);

                    int id = Integer.parseInt(idList.getText().toString());
                    deleteOneCard(id);
                    Toast.makeText(container.getContext(), "Suppression de "+id, Toast.LENGTH_SHORT).show();
                }
            });

            vh.Modifier.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v) {
                    View parentView = (View)v.getParent();
                    TextView idList = (TextView) parentView.findViewById(R.id.idList);
                    TextView themeList = (TextView) parentView.findViewById(R.id.themeList);
                    themeList.setFocusable(true);
                    themeList.requestFocus();
                    /*TextView questionList = (TextView) parentView.findViewById(R.id.questionList);
                    TextView reponseList = (TextView) parentView.findViewById(R.id.reponseList);
                    TextView difficulteList = (TextView) parentView.findViewById(R.id.difficultList);*/

                    int id = Integer.parseInt(idList.getText().toString());
                    Toast.makeText(container.getContext(), "bouton Modifier pour "+id, Toast.LENGTH_SHORT).show();
                }
            });

        }
    }

    public class ViewHolder
    {
        Button Supprimer, Modifier;
        TextView idList, themeList, questionList, reponseList, difficulteList;

    }

谢谢你们。

ryhaxcpt

ryhaxcpt1#

我的问题解决了!在扩展SimpleCorsorAdapter的内部类中,在bindview函数中,我必须再次初始化edittext!!这样地:

public void bindView(View view, Context Context, Cursor cursor) {
            //EDIT
            vh.idList = (TextView) view.findViewById(R.id.idList);
            vh.themeList = (TextView) view.findViewById(R.id.themeList);
            vh.questionList = (TextView) view.findViewById(R.id.questionList);
            vh.reponseList = (TextView) view.findViewById(R.id.reponseList);
            vh.difficulteList = (TextView) view.findViewById(R.id.difficultList);
            vh.Supprimer = (Button) view.findViewById(R.id.buttonDelete);
            vh.Modifier = (Button) view.findViewById(R.id.buttonModifier);
            //END EDIT

            vh.idList.setText(cursor.getString(cursor.getColumnIndex(stock._ID)));
            vh.themeList.setText(cursor.getString(cursor.getColumnIndex(stock.THEME)));
            vh.questionList.setText(cursor.getString(cursor.getColumnIndex(stock.QUESTION)));
            vh.reponseList.setText(cursor.getString(cursor.getColumnIndex(stock.REPONSE)));
            vh.difficulteList.setText(cursor.getString(cursor.getColumnIndex(stock.DIFFICULTE)));

            vh.Supprimer.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v) {
                    View parentView = (View)v.getParent();
                    TextView idList = (TextView) parentView.findViewById(R.id.idList);

                    int id = Integer.parseInt(idList.getText().toString());
                    deleteOneCard(id);
                    Toast.makeText(container.getContext(), "Suppression de "+id, Toast.LENGTH_SHORT).show();
                }
            });

            vh.Modifier.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v) {
                    View parentView = (View)v.getParent();
                    TextView idList = (TextView) parentView.findViewById(R.id.idList);
                    TextView themeList = (TextView) parentView.findViewById(R.id.themeList);
                    themeList.setFocusable(true);
                    themeList.requestFocus();
                    /*TextView questionList = (TextView) parentView.findViewById(R.id.questionList);
                    TextView reponseList = (TextView) parentView.findViewById(R.id.reponseList);
                    TextView difficulteList = (TextView) parentView.findViewById(R.id.difficultList);*/

                    int id = Integer.parseInt(idList.getText().toString());
                    Toast.makeText(container.getContext(), "bouton Modifier pour "+id, Toast.LENGTH_SHORT).show();
                }
            });

        }
zzzyeukh

zzzyeukh2#

<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="match_parent"
             android:background="@android:color/darker_gray">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollingCache="true"/>

</RelativeLayout>

或者
使用linearlayout并更改 ListView 高度至 0dp 并添加 weight=1 ```
<LinearLayout
........
android:orientation="vertical">

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:scrollingCache="true"/>
yb3bgrhw

yb3bgrhw3#

尝试将所有可滚动的内容添加到单个relativelayout中,并将此内容添加到scrollview中。

<ScrollView>
    <RelativeLayout>
        // other elements

<ScrollView>
    <ListView>

第一个示例中的relativelayout是必需的,因为scrollview元素只接受一个子元素。

hgncfbus

hgncfbus4#

设置 ListView 高度至 match_parent . 因此,它将占据父对象的整个区域 View .

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView"
    android:scrollingCache="true"/>

相关问题