自定义ListView适配器无法在我的android studio项目中工作
在此项目中,应用程序正在ListView中运行空值内容 活动代码自定义ListView适配器无法在我的android studio项目中工作,android,listview,Android,Listview,在此项目中,应用程序正在ListView中运行空值内容 活动代码 public class AddMember extends AppCompatActivity { ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.
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();
}
});
}
}
public类AddMember扩展appcompative活动{
列表视图列表视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u add\u成员);
listView=(listView)findViewById(R.id.list\u成员);
字符串[]数字={“1234567890”};
字符串[]名称={“Ashiq”};
CustomMembers adapter2=新的CustomMembers(此、数字、名称);
setAdapter(适配器);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
int itemPosition=位置;
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;
}
}
公共类CustomMembers扩展了ArrayAdapter{
私人活动语境;
私有字符串[]个数字;
私有字符串[]名称;
公共自定义成员(活动上下文、字符串[]编号、字符串[]名称){
超级(上下文,右布局,列出成员);
this.context=上下文;
这个。数字=数字;
this.names=名称;
}
@非空
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气器=上下文。getLayoutInflater();
视图行视图=充气机。充气(R.layout.list_成员,null,true);
TextView txtName=(TextView)rowView.findViewById(R.id.name);
TextView txtNumber=(TextView)rowView.findViewById(R.id.number);
setText(名称[位置]);
setText(数字[位置]);
返回行视图;
}
}
活动添加成员.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>
<?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>
列出成员.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>
<?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>
上面的代码运行不正常。它为MainActivity(AddMember)显示空页。您的
适配器
不知道它在列表视图中显示了多少行。为此,您必须覆盖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;
}
}
公共类CustomMembers扩展了ArrayAdapter{
私人活动语境;
私有字符串[]个数字;
私有字符串[]名称;
公共自定义成员(活动上下文、字符串[]编号、字符串[]名称){
超级(上下文,右布局,列出成员);
this.context=上下文;
这个。数字=数字;
this.names=名称;
}
@凌驾
public int getCount(){
返回name.length;
}
@凌驾
公共对象getItem(int i){
返回数字[i];//或名称[i]
}
@凌驾
公共长getItemId(int i){
返回i;
}
@非空
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气器=上下文。getLayoutInflater();
视图行视图=充气机。充气(R.layout.list_成员,null,true);
TextView txtName=(TextView)rowView.findViewById(R.id.name);
TextView txtNumber=(TextView)rowView.findViewById(R.id.number);
setText(名称[位置]);
setText(数字[位置]);
返回行视图;
}
}
Object getItem(int i)返回无效的空值no。错误显示尝试使用不兼容的返回类型。在哪里?你能把这行写下来吗?getCount()方法可以。但是getItem()方法在检索none对象返回类型时显示错误。请将方法放在此处