Android ListView无法滚动更多,错误的自定义适配器?

时间:2017-01-04 07:02:00

标签: java android listview

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;

    }

谢谢你们。

4 个答案:

答案 0 :(得分:1)

ListView高度设置为match_parent。因此,它将占据父View的完整区域。

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

答案 1 :(得分:1)

尝试在一个RelativeLayout中添加可滚动的所有内容,并将此内容添加到ScrollView中。

<ScrollView>
    <RelativeLayout>
        // other elements

<ScrollView>
    <ListView>

第一个示例中的RelativeLayout是必需的,因为ScrollView元素只接受一个子节点。

答案 2 :(得分:1)

<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"/>

</LinearLayout>

答案 3 :(得分:0)

我解决了我的问题!在我的内部类中扩展SimpleCursorAdapter,在我的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();
                }
            });

        }