简体   繁体   English

自定义ListView适配器无法正常工作我的Android Studio项目

[英]Custom ListView adapter is not working my android studio project

In this project app running out null value contents in ListView Activity Code 在此项目应用程序中,用尽ListView 活动代码中的空值内容

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

Above code is not running properly.Its show empty page for the MainActivity (AddMember). 上面的代码无法正常运行,它显示MainActivity(AddMember)的空白页面。

Your Adapter dont know how many lines it's shown in your ListView . 您的Adapter不知道它在ListView显示了多少行。 To do that, you have to override all the methods from ArrayAdapter 为此,您必须重写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;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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