android-ListView 프리젠테이션 데이터

6013 단어
main.xml
<ListView 
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:id="@+id/listView"
     />

1.item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
         android:layout_width="120dp"
         android:layout_height="wrap_content"
         android:id="@+id/name"
         android:text="xxxxx"
        />
    <TextView
         android:layout_width="100dp"
         android:layout_height="wrap_content"
         android:id="@+id/phone"
         android:text="13547907127"
        />
    <TextView
         android:layout_width="100dp"
         android:layout_height="wrap_content"
         android:id="@+id/amount"
         android:text="100"
        />
</LinearLayout>

2.MainActivity.java
 onCreate()   show()
//listView SimpleAdapter    
    private void show(){
        PersonDao dao=new PersonDao(this.getApplicationContext());
        List<Person> persons=dao.getPage(0,20);
        List<HashMap<String,Object>> data=new ArrayList<HashMap<String,Object>>();
        for(Person person:persons){
            HashMap<String,Object> item=new HashMap<String,Object>();
            item.put("name",person.getName());
            item.put("phone",person.getPhone());
            item.put("amount",person.getAmount());
            item.put("id",person.getId());
            data.add(item);
        }
        SimpleAdapter adapter=new SimpleAdapter(getApplicationContext(),
                data,R.layout.item,
                new String[]{"name","phone","amount"},
                new int[]{R.id.name,R.id.phone,R.id.amount});
        ListView listView=(ListView)findViewById(R.id.listView);
        listView.setAdapter(adapter);
    }

//////////////////////
PersonDao.java     
//    cursor
1.    public Cursor getPageCursor(int offset,int maxResult){
        List<Person> persons=new ArrayList<Person>();    
        SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
        Cursor cursor=db.rawQuery("select id as _id,name,phone,amount from person limit ?,?",
                new String[]{String.valueOf(offset),String.valueOf(maxResult)});
        return cursor;
    }

2.MainActivity.java  show2()
//listView SimpleCursorAdapter    
    private void show2(){
        PersonDao dao=new PersonDao(this.getApplicationContext());
        Cursor cursor=dao.getPageCursor(0,20);
        SimpleCursorAdapter adapter=new SimpleCursorAdapter(getApplicationContext(),
                R.layout.item,cursor,
                new String[]{"name","phone","amount"},
                new int[]{R.id.name,R.id.phone,R.id.amount});
        ListView listView=(ListView)findViewById(R.id.listView);
        listView.setAdapter(adapter);
    }

///////////////     ///////
//   Adapter    
public class PersonAdapter extends BaseAdapter {

    private List<Person> persons;//     
    private int resource;//       
    private LayoutInflater inflater;//xml      
    
    public PersonAdapter(Context context,List<Person> persons,int resource) {
        this.persons = persons;
        this.resource=resource;
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return persons.size();
    }

    public Object getItem(int arg0) {
        return persons.get(arg0);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            //      
            convertView=inflater.inflate(resource,null);
        }
        TextView nameView=(TextView) convertView.findViewById(R.id.name);
        TextView phoneView=(TextView) convertView.findViewById(R.id.phone);
        TextView amountView=(TextView) convertView.findViewById(R.id.amount);
        Person person=persons.get(position);
        //          
        nameView.setText(person.getName());
        phoneView.setText(person.getPhone());
        amountView.setText(person.getId().toString());
        return convertView;
    }
}

///////////////////
//listView    Adapter    
    private void show3(){
        PersonDao dao=new PersonDao(this.getApplicationContext());
        List<Person> persons=dao.getPage(0,20);
        PersonAdapter adapter=new PersonAdapter(getApplicationContext(),
                persons,R.layout.item);
        listView.setAdapter(adapter);
    }

//////////////////////////////////      
private final class ItemClickListener implements OnItemClickListener{
        //parent-ListView  ,view-       ,
        //position          ,
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            ListView iview=(ListView)parent;
            /*      
             * Person p=(Person) iview.getItemAtPosition(position);
            String str=p.getId()+","+p.getName()+","+p.getPhone()+","+p.getAmount();
            Toast.makeText(getApplicationContext(),p.getId().toString(),2);*/
            
            Cursor cursor=(Cursor)iview.getItemAtPosition(position);
            int personid=cursor.getInt(cursor.getColumnIndex("id"));
            Toast.makeText(getApplicationContext(),personid+"",2);
        }
        
    }

/////////////////
istView=(ListView)findViewById(R.id.listView);
        //         
        listView.setOnItemClickListener(new ItemClickListener());

좋은 웹페이지 즐겨찾기