您好,登錄后才能下訂單哦!
我先貼出代碼,后面詳細介紹
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。