gpt4 book ai didi

android - 在没有 xml 的情况下以编程方式创建 Listview : not showing any cell

转载 作者:行者123 更新时间:2023-11-29 23:29:44 24 4
gpt4 key购买 nike

我正在尝试在 android 中以编程方式创建 ListView 。

我的目标是创建一个动态 View ,如果我单击一个按钮,它会将布局内的内容从 ListView 更改为另一个 View ,反之亦然。

一切似乎都很好我成功地将 ListView 放入 java 文件的线性布局中,我通过为单元格创建自定义布局来做到这一点。

问题是它显示 ListView 但不添加单元格。我知道它放置了 ListView ,因为当我运行它时背景变成绿色但它没有显示 ListView 的单元格。

我错过了什么吗?

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

主要 Activity .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中返回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;
}

关于android - 在没有 xml 的情况下以编程方式创建 Listview : not showing any cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52845976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com