Android------------BaseAdapter示例介绍

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
	<ListView android:id="@+id/listview"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"></ListView>
</LinearLayout>


adapters.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" >
    
    <ImageView android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:src="@drawable/ic_launcher"/>
	<TextView android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:text="aaaa"/>
	
</LinearLayout>

 

 

示例代码:

package com.tanghy.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class BaseAdapterTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView listview = (ListView)findViewById(R.id.listview);
        listview.setAdapter(new myBaseAdapter());
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Toast.makeText(BaseAdapterTestActivity.this, "adsfadsf", Toast.LENGTH_SHORT).show();


			}
		});
    }
    
    class myBaseAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 10;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			System.out.println("asdfasdfa");
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView textview = new TextView(getApplicationContext());
			convertView = 	LayoutInflater.from(getApplicationContext()).inflate(R.layout.adapters, null);
			
			textview.setText("什么");
			
			return convertView;
		}
    	
    }
}



 

你可能感兴趣的:(android,ListView,layout,null,Class,encoding)