2017-01-04 60 views
1

編輯2:我現在有一個適合我的佈局的設計,我可以滾動,但我的自定義適配器有一個新問題。當我滾動時,我的editText丟失了他們的內容,並被來自其他元素的其他內容所取代。但是當我使用SimpleCursorAdapter時,我沒有任何問題(但我不能使用我的按鈕)。Android ListView無法滾動更多,壞的自定義適配器?

這裏是我的自定義適配器:

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; 

    } 

謝謝你們。

+0

你爲什麼使用FrameLayout,然後ListView? –

+0

使用您的列表視圖上方的另一個父佈局 – Anjali

+0

我試圖添加父佈局,更改我的FrameLayout,但它不起作用 –

回答

1

ListView高度設置爲match_parent。所以,它將佔據父母的全部區域View

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

我試過了,看看我的帖子結尾處的圖片。但它奇怪的 –

1

嘗試在單個RelativeLayout中添加所有可滾動的內容,並在ScrollView中添加這個內容。

<ScrollView> 
    <RelativeLayout> 
     // other elements 

<ScrollView> 
    <ListView> 

的RelativeLayout的在第一實施例是必要的,因爲滾動型元件只接受一個孩子。

+0

我已經在我的listView佈局內有一個relativeLayout,但我仍然嘗試使用ScrollView,並且(並且沒有)另一個相對佈局,它不會改變任何東西 –

+0

嘗試使用ScrollView來包裝ListView。通常我會這樣做。 – TheMechanic

+0

當我這樣做時,我的結果與我的最後一張圖片相同,我的編輯視圖變成了bug –

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

它仍然有同樣的問題,我的最後一張照片,我認爲我的問題來自我的Fragment類中的自定義光標適配器 –

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

     } 
相關問題