2017-03-28 31 views
0

リストビューの各行をカスタム表示するには、その中にテキストビューを持つボタンが必要です。ListViewがリストボタンに表示されませんか?

しかし、それは動作しません!

これは、最初のレイアウトで作成したボタンのみを表示しています。

修正のために何ができますか?多くのおかげでここ

は私のコードです:

public class MainActivity extends AppCompatActivity { 
Button button; 
    ListView lv ; 
    ListViewAdapter listViewAdapter; 

    ArrayList<String> al ; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.activity_main); 
     lv = (ListView)findViewById(R.id.lstView); 
     button = (Button)findViewById(R.id.button); 
     al = new ArrayList(); 
     al.add("cho"); 
     al.add("phuong"); 
     al.add("hello"); 
     al.add("bye"); 
     al.add("love"); 
     listViewAdapter= new ListViewAdapter(this , R.layout.button , al); 
     lv.setAdapter(listViewAdapter); 

これは、アダプタ:

public class ListViewAdapter extends ArrayAdapter { 
    /** 
    * Constructor 
    * 
    * @param context The current context. 
    * @param resource The resource ID for a layout file containing a TextView to use when 
    */ 
    Activity context; 
    int layResID; 
    ArrayList<String> object; 
    public ListViewAdapter(Activity context, int resource , ArrayList<String> object) { 
     super(context, resource); 
     this.context=context; 
     this.layResID = resource; 
     this.object = object; 

    } 

    @NonNull 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if(convertView==null){ 
      LayoutInflater inflater = context.getLayoutInflater(); 

      convertView = inflater.inflate(layResID, null); 
     } 
     Button btn = (Button)convertView.findViewById(R.id.view); 
     String string = object.get(position); 
     btn.setText(string); 
     return convertView; 
    } 
} 

これがメインのxmlです:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <Button 
     android:id="@+id/view" 
     android:layout_width="300dp" 
     android:layout_height="100dp" 
     android:background="@drawable/btn_list" 
     android:textSize="10dp"/> 

</LinearLayout> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.macbook.note.MainActivity" 
    android:background="#1A237E" 
    android:orientation="vertical" 
    android:weightSum="10" 
    > 
    <RelativeLayout 
     android:layout_weight="2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

    <Button 
     android:layout_marginTop="30dp" 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:textSize="10dp" 
     android:background="@drawable/button" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:id="@+id/button" /> 
    </RelativeLayout> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_weight="9" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 
    <ListView 
     android:id="@+id/lstView" 
     android:divider="#1A237E" 
     android:dividerHeight="20dp" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    </ListView> 
    </ScrollView> 
    </LinearLayout> 
</LinearLayout> 

これは、カスタムレイアウトであります

答えて

0

super(context, object); 
+0

ありがとう、それは仕事です! –

+0

@HảiTôHoàngご満足いただける場合は、回答を受け入れるようにしてください。 –

0

にごArrayAdapterのスーパー呼び出しを変更し、私はこの問題は、リストビューの内部で作成する必要があり、あなたの項目の現在の数を指定していないということですね。お使いのアダプタクラスの内部

public ListViewAdapter(Activity context, int resource , ArrayList<String> object) { 
    super(context, resource, object); 
} 


2.オーバーライドArrayAdapater.getCount():
1.コール他ArrayAdapterのコンストラクタのでArrayAdapterクラスはあなたのためにそれを処理します:

@Override 
public int getCount() { 
    return this.object.size(); 
} 
2つのオプションがあります
+0

ありがとうございます! –

+0

あなたは大歓迎です! –

関連する問題