简体   繁体   中英

Android ListView can't scroll more, bad custom Adapter?

EDIT2 : I have now a proper design for my layout and I can scroll , but I have a new problem with my Custom Adapter. When I scroll, my editText are loosing their content and are replaced by other content from other element. But When I use a SimpleCursorAdapter, then I don't have any problem (but I can't use my button).

Here is my Custom Adapter :

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;

    }

Thank you guys.

Set the ListView height to match_parent . So, it will occupy the complete area of the parent View .

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

Try adding everything scrollable inside a single RelativeLayout and this one inside a ScrollView.

<ScrollView>
    <RelativeLayout>
        // other elements

or

<ScrollView>
    <ListView>

The RelativeLayout in the first example is necessary because the ScrollView element accepts only one child.

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

or

Use LinearLayout and Change the ListView height to 0dp and add 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>

I have resolver my problem ! In my inner class that extends SimpleCursorAdapter, in my bindView function, I had to initialize again my editText !! Like this :

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();
                }
            });

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM