gpt4 book ai didi

android - 以编程方式创建 ListView

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:20:50 25 4
gpt4 key购买 nike

大家好,我是 Android 新手。谁能告诉我以下代码有什么问题:

public class ListApp extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView lText = new TextView(this);
lText.setId(0);

ListView lView = new ListView(this);
String[] lStr = new String[]{"AA","BB", "CC"};
ArrayAdapter lAdap = new ArrayAdapter(this,lText.getId(),lStr);
lView.setAdapter(lAdap);
lView.setFocusableInTouchMode(true);

setContentView(lView);
}
}

最佳答案

这是一个不需要您编写任何 xml 布局的解决方案。它尽可能使用标准的 android 布局,并且不需要通货膨胀:

Dialog dialog = new Dialog(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Color Mode");

ListView modeList = new ListView(this);
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);

builder.setView(modeList);
dialog = builder.create();

关于android - 以编程方式创建 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3718523/

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