2017-04-27 43 views
0

在這個項目中應用的ListView 活動代碼用完的空值內容自定義的ListView適配器不工作我的Android Studio項目

public class AddMember extends AppCompatActivity { 


    ListView listView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_add_member); 
     listView = (ListView) findViewById(R.id.list_members); 
     String[] numbers = {"1234567890"}; 
     String[] names = {"Ashiq"}; 

     CustomMembers adapter2 = new CustomMembers(this,numbers,names); 
     listView.setAdapter(adapter); 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
            int position, long id) { 
       int itemPosition  = position; 
       String itemValue = (String) listView.getItemAtPosition(position); 
       Toast.makeText(getApplicationContext(),"Position : "+itemValue,Toast.LENGTH_SHORT).show(); 
      } 
     }); 
} 
} 

CustomMembers.java

public class CustomMembers extends ArrayAdapter<String> { 

    private Activity context; 
    private String[] numbers; 
    private String[] names; 
    public CustomMembers(Activity context, String[] numbers,String[] names) { 
     super(context, R.layout.list_members); 
     this.context = context; 
     this.numbers = numbers; 
     this.names = names; 
    } 

    @NonNull 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     View rowView = inflater.inflate(R.layout.list_members, null, true); 
     TextView txtName = (TextView) rowView.findViewById(R.id.name); 
     TextView txtNumber = (TextView) rowView.findViewById(R.id.number); 
     txtName.setText(names[position]); 
     txtNumber.setText(numbers[position]); 
     return rowView; 
    } 
} 

activity_add_member .xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/activity_add_member" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="alaman.dailybook.AddMember"> 
    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 
     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      android:background="@color/colorPrimary"/> 
    </android.support.design.widget.AppBarLayout> 
    <android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true" 
     android:id="@+id/scrollPager" 
     android:layout_below="@+id/toolbar" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <ListView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/list_members"/> 
     </RelativeLayout> 
    </android.support.v4.widget.NestedScrollView> 
</android.support.design.widget.CoordinatorLayout> 

list_members.xml

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 
    <TableRow> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="5dp" 
      android:background="#fff" 
      android:clickable="true" 
      android:elevation="4dp" 
      android:padding="20dp" 
      android:textColor="#000" 
      android:layout_gravity="center" 
      android:id="@+id/name" 
      android:text="Name" /> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="5dp" 
      android:background="#fff" 
      android:clickable="true" 
      android:elevation="4dp" 
      android:padding="20dp" 
      android:textColor="#000" 
      android:id="@+id/number" 
      android:layout_gravity="center" 
      android:text="1234567890" /> 
    </TableRow> 
</TableLayout> 

上面的代碼沒有運行properly.Its顯示在MainActivity(使用addMember)空白頁。

回答

0

你的Adapter不知道你的ListView顯示了多少行。要做到這一點,你必須覆蓋所有的方法,由ArrayAdapter

public class CustomMembers extends ArrayAdapter<String> { 

     private Activity context; 
     private String[] numbers; 
     private String[] names; 
     public CustomMembers(Activity context, String[] numbers,String[] names) { 
      super(context, R.layout.list_members); 
      this.context = context; 
      this.numbers = numbers; 
      this.names = names; 
     } 
    @Override 
public int getCount() { 
return names.length; 
} 

@Override 
public Object getItem(int i) { 
return numbers[i]; // or names[i] 
} 

@Override 
public long getItemId(int i) { 
return i; 
} 


     @NonNull 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      LayoutInflater inflater = context.getLayoutInflater(); 
      View rowView = inflater.inflate(R.layout.list_members, null, true); 
      TextView txtName = (TextView) rowView.findViewById(R.id.name); 
      TextView txtNumber = (TextView) rowView.findViewById(R.id.number); 
      txtName.setText(names[position]); 
      txtNumber.setText(numbers[position]); 
      return rowView; 
     } 
    } 
+0

對象的getItem(int i)以返回空值 – Ashiq

+0

更改getCount將()方法,我編輯我的答案 –

+0

沒有,這不是正常工作。錯誤顯示嘗試使用不兼容的返回類型。 – Ashiq

相關問題