在这个项目应用程序中运行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
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>
以上代码未正常运行。它显示MainActivity(AddMember)的空白页。
答案 0 :(得分: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;
}
}