世事洞明皆学问,人情练达即文章。这篇文章主要讲述ListView的简单使用--Android相关的知识,希望能为你提供帮助。
1、本例实现效果图
文章图片
2、主要是activity_main.xml(布局文件)和Activity类文件,实现过程比较简单,直接附源码了哈! activity_main.xml: < ?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:background="@color/color_main_bg">
< ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
< /RelativeLayout> MainActivity.java: package livetelecast.thonlon.example.cn.thonlonlivetelecast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView) findViewById(R.id.list_view);
final ArrayAdapter< String> adapter=new ArrayAdapter< String> (this,android.R.layout.simple_expandable_list_item_1,getData());
listView.setAdapter(adapter); // 不关心数据是从哪里来的只需要设置 adapter,adapter会给listview数据
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override //items的点击事件
public void onItemClick(AdapterView< ?> adapterView, View view, int i, long l) {
String txt=adapter.getItem(i);
//Toast.makeText(MainActivity.this,txt,0).show();
//setContentView(R.layout.activity_play);
}
});
}
private String[] getData(){
return new String[]{"CCTV1","CCTV2","CCTV3","CCTV4","CCTV5","CCTV6","CCTV7","CCTV8","CCTV9","湖南卫视"};
}
}
3、源码下载:ListView.zip【ListView的简单使用--Android】
推荐阅读
- Install Kali in Android
- Pedometer_forAndroid
- Spring boot 学习 四(spring boot 配置文件 application.yml)
- Android中简单活动窗口的切换--Android
- android--------自定义控件
- Android Sdk
- java语言中application异常退出和线程异常崩溃的捕获方法,并且在捕获的钩子方法中进行异常处理
- MindMapper16中文破解版
- JVisualVM出现: “Local Applications Cannot Be Detected”