在没有 xml 的情况下以编程方式创建 Listview:不显示任何单元格
Creating Listview programatically without xml : not showing any cell
我正在尝试在 android 中以编程方式创建列表视图。
我的目标是创建一个动态视图,如果我单击一个按钮,它会将布局内的内容从列表视图更改为另一个视图,反之亦然。
一切似乎都很好我成功地将列表视图放入 java 文件的线性布局中,我通过为单元格创建自定义布局来做到这一点。
问题是它显示列表视图但不添加单元格。
我知道它放置了列表视图,因为当我 运行 它时背景变成绿色,但它没有显示列表视图的单元格。
我错过了什么吗?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="@color/colorPrimary">
<TextView
android:id="@+id/listContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
LinearLayout mainView,sideView;
ListView notelist;
String[] Names = {"great","good","average","great","good","average"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainView = (LinearLayout)findViewById(R.id.mainView);
notelist = new ListView(this);
notelist.setBackgroundColor(Color.GREEN);
CustomAdapter customAdapter = new CustomAdapter();
notelist.setAdapter(customAdapter);
notelist.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
mainView.addView(notelist);
}
class CustomAdapter extends BaseAdapter{
@Override
public int getCount() {
return Names.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.list_vertical,null);
TextView contentText = (TextView) view.findViewById(R.id.listContent);
contentText.setText(Names[position]);
return null;
}
}
}
主要xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/sideView"
android:layout_weight=".20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageButton
android:id="@+id/oneBtn"
android:layout_width="@dimen/ButtonSize"
android:layout_height="@dimen/ButtonSize"
android:background="@android:color/transparent"
android:src="@drawable/noct"
/>
<ImageButton
android:id="@+id/twoBtn"
android:layout_width="@dimen/ButtonSize"
android:layout_height="@dimen/ButtonSize"
android:background="@android:color/transparent"
android:src="@drawable/note"
/>
<ImageButton
android:id="@+id/threeBtn"
android:layout_width="@dimen/ButtonSize"
android:layout_height="@dimen/ButtonSize"
android:background="@android:color/transparent"
android:src="@drawable/journal"
/>
<ImageButton
android:id="@+id/fourBtn"
android:layout_width="@dimen/ButtonSize"
android:layout_height="@dimen/ButtonSize"
android:background="@android:color/transparent"
android:src="@drawable/mic"/>
<ImageButton
android:id="@+id/fiveBtn"
android:layout_width="@dimen/ButtonSize"
android:layout_height="@dimen/ButtonSize"
android:background="@android:color/transparent"
android:src="@drawable/add"
/>
<ImageButton
android:id="@+id/sixBtn"
android:layout_width="@dimen/ButtonSize"
android:layout_height="@dimen/ButtonSize"
android:background="@android:color/transparent"
android:src="@drawable/pencil"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/mainView"
android:layout_weight=".80"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
不要在 getView
中 return null
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.list_vertical,null);
TextView contentText = (TextView) view.findViewById(R.id.listContent);
contentText.setText(Names[position]);
return view;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.list_vertical,null);
TextView contentText = (TextView) convertView.findViewById(R.id.listContent);
contentText.setText(Names[position]);
return convertView;
}
我正在尝试在 android 中以编程方式创建列表视图。
我的目标是创建一个动态视图,如果我单击一个按钮,它会将布局内的内容从列表视图更改为另一个视图,反之亦然。
一切似乎都很好我成功地将列表视图放入 java 文件的线性布局中,我通过为单元格创建自定义布局来做到这一点。
问题是它显示列表视图但不添加单元格。 我知道它放置了列表视图,因为当我 运行 它时背景变成绿色,但它没有显示列表视图的单元格。
我错过了什么吗?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="@color/colorPrimary">
<TextView
android:id="@+id/listContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
LinearLayout mainView,sideView;
ListView notelist;
String[] Names = {"great","good","average","great","good","average"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainView = (LinearLayout)findViewById(R.id.mainView);
notelist = new ListView(this);
notelist.setBackgroundColor(Color.GREEN);
CustomAdapter customAdapter = new CustomAdapter();
notelist.setAdapter(customAdapter);
notelist.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
mainView.addView(notelist);
}
class CustomAdapter extends BaseAdapter{
@Override
public int getCount() {
return Names.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.list_vertical,null);
TextView contentText = (TextView) view.findViewById(R.id.listContent);
contentText.setText(Names[position]);
return null;
}
}
}
主要xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/sideView"
android:layout_weight=".20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageButton
android:id="@+id/oneBtn"
android:layout_width="@dimen/ButtonSize"
android:layout_height="@dimen/ButtonSize"
android:background="@android:color/transparent"
android:src="@drawable/noct"
/>
<ImageButton
android:id="@+id/twoBtn"
android:layout_width="@dimen/ButtonSize"
android:layout_height="@dimen/ButtonSize"
android:background="@android:color/transparent"
android:src="@drawable/note"
/>
<ImageButton
android:id="@+id/threeBtn"
android:layout_width="@dimen/ButtonSize"
android:layout_height="@dimen/ButtonSize"
android:background="@android:color/transparent"
android:src="@drawable/journal"
/>
<ImageButton
android:id="@+id/fourBtn"
android:layout_width="@dimen/ButtonSize"
android:layout_height="@dimen/ButtonSize"
android:background="@android:color/transparent"
android:src="@drawable/mic"/>
<ImageButton
android:id="@+id/fiveBtn"
android:layout_width="@dimen/ButtonSize"
android:layout_height="@dimen/ButtonSize"
android:background="@android:color/transparent"
android:src="@drawable/add"
/>
<ImageButton
android:id="@+id/sixBtn"
android:layout_width="@dimen/ButtonSize"
android:layout_height="@dimen/ButtonSize"
android:background="@android:color/transparent"
android:src="@drawable/pencil"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/mainView"
android:layout_weight=".80"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
不要在 getView
中 return nullpublic View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.list_vertical,null);
TextView contentText = (TextView) view.findViewById(R.id.listContent);
contentText.setText(Names[position]);
return view;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.list_vertical,null);
TextView contentText = (TextView) convertView.findViewById(R.id.listContent);
contentText.setText(Names[position]);
return convertView;
}