Android 以编程方式创建 ListView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3718523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:30:44  来源:igfitidea点击:

Create ListView programmatically

androidlistview

提问by ashraf

Hi I am new in Android. Could anyone tell me pls whats the wrong with the following code:

嗨,我是 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);
    }
}

回答by moonlightcheese

here's a solution that does not require you to write any xml layouts. it uses standard android layouts where possible and no inflation is necessary:

这是一个不需要您编写任何 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();

回答by Krishna

Try this..

尝试这个..

Paste the following code in list_item.xml.

将以下代码粘贴到 list_item.xml 中。

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"
    android:padding="10dp" 
    android:textSize="16sp" android:textColor="#ffffff" android:textStyle="bold" android:background="@drawable/border_cell">
</TextView>

Here is the activity class....

这里是活动课......

    public class UsersListActivity extends ListActivity{    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);         
            String[] statesList = {"listItem 1", "listItem 2", "listItem 3"};
            setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
                    statesList)); 
            ListView lv = getListView(); 

            lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {


                     Toast.makeText(getApplicationContext(),
                     "You selected : "+((TextView) view).getText(), Toast.LENGTH_SHORT).show();
     }
            });

        }

}

回答by kzotin

Best practice is to separate view (xml layout) and controller (Activity).

最佳做法是将视图(xml 布局)和控制器(活动)分开。

If you dont want this, try to put

如果你不想要这个,试着把

 setContentView(lView);

at the beginning of onCreate

onCreate的开头

回答by Kevin Bradshaw

public class ListApp extends ListActivity

回答by Nathan Schwermann

Make a layout XML file with your listview and use findViewById to set it's adapter after first setting the content view to your layout

使用您的列表视图制作一个布局 XML 文件,并在首先将内容视图设置为您的布局后使用 findViewById 设置它的适配器