Android ListView实现简单列表功能

时间:2021-05-21

ListView如何实现简单列表,供大家参考,具体内容如下

效果图:

啥也没干的ListView张这样:

fry.Activity01

package fry;import com.example.ListView.R;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;public class Activity01 extends Activity implements OnItemClickListener{ private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity01); listView=(ListView) findViewById(R.id.listView); ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, getData()); listView.setAdapter(adapter); listView.setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub String string=(String) parent.getItemAtPosition(position); Toast.makeText(this, string, Toast.LENGTH_SHORT).show(); } private String[] getData(){ return new String[]{"韩寒","蔡静","卢本伟","五五开","战狼2","朝阳兄","虹桥一姐","犀利哥","王宝庆", "系大大","搞踹踹","张三","李四","王五"}; }}

/ListView/res/layout/activity01.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" ></ListView></LinearLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章