Android——ListView機能の実現
4245 ワード
1、main.xml
2、item.xml
3、MainActivity
結果は次のとおりです.
<?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/personLV"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
2、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="horizontal" >
<TextView android:id="@+id/idTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp"
android:textSize="20sp"
/>
<TextView android:id="@+id/nameTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:padding="10dp"
android:textSize="20sp"
/>
<TextView android:id="@+id/balanceTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:padding="10dp"
android:textSize="20sp"
/>
</LinearLayout>
3、MainActivity
package com.njupt.listview;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ListView personLV;
private List<Person> persons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
personLV = (ListView) findViewById(R.id.personLV);
PersonDao dao = new PersonDao(this);
persons = dao.queryAll();
personLV.setAdapter(new MyBaseAdapter());// ListView adapter, adapter ListView
}
private class MyBaseAdapter extends BaseAdapter{// Adapter, Person , ListView
@Override
public int getCount() {//
return persons.size();
}
@Override
public Object getItem(int position) {//
return persons.get(position);
}
@Override
public long getItemId(int position) {// id
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {// , ListView
View item = View.inflate(getApplicationContext(), R.layout.item, null);// View(LinearLayout)
TextView idTV = (TextView) item.findViewById(R.id.idTV);// View TextView
TextView nameTV = (TextView) item.findViewById(R.id.nameTV);
TextView balanceTV = (TextView) item.findViewById(R.id.balanceTV);
Person p = persons.get(position);// Person
idTV.setText(p.getId() + "");// TextView
nameTV.setText(p.getName());
balanceTV.setText(p.getBalance()+"");
return item;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
------------------------------------------------------- 結果は次のとおりです.