我先贴出代码,后面详细介绍
Mativity.java
package com.example.sss;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity extends Activity {
/**
* 适配器
*/
private baseadapterss bseeee;
private ListView lv_item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_item=(ListView)findViewById(R.id.lv_item);
lv_item.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
});
bseeee=new baseadapterss(MainActivity.this);
lv_item.setAdapter(bseeee);
}
}
适配器的baseadapterss.java
package com.example.sss;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public class baseadapterss extends BaseAdapter{
private Context context;
public baseadapterss(Context context) {
this.context=context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 45;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null) {
convertView=LayoutInflater.from(context).inflate(R.layout.item, null);
}
return convertView;
}
}
下面的activity_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lv_item"
android:divider="#b4b4b4"
android:dividerHeight="1dp"
android:listSelector="#00000000"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
item.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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="你好!"
android:gravity="center"
/>
</LinearLayout>
我之前还写错了适配器的用法,导致了程序跑错了;以上把代码贴上就可以跑出来了。如果你觉得我的仔细就给我一个赞吧。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。