android *** Layout 13 Adapter

来源:互联网 发布:mac 打开icloud 编辑:程序博客网 时间:2024/04/20 00:43

网站如下

http://android-doc.com/reference/android/widget/Adapter.html


网站里对Adapter介绍很清楚,主要是将数据库里的一些复杂数据转化成为能够看见的数据,好比显示器将电流转化成图像信息。

贴一个http://blog.csdn.net/a_large_swan/article/details/7535337

然后就可以自己创建一个适配器了。

xml如下

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:android1="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >    <ListView        android1:id="@+id/listView1"        android1:layout_width="match_parent"        android1:layout_height="match_parent"        android1:layout_alignParentLeft="true"        android1:layout_alignParentTop="true" >    </ListView></RelativeLayout>

java
package com.example.tree;import android.support.v7.app.ActionBarActivity;import android.text.Html;import android.text.Spanned;import android.text.method.LinkMovementMethod;import android.util.Log;import java.util.ArrayList;import java.util.Calendar;import java.util.List;import android.app.ProgressDialog;import android.content.Context;import android.content.Intent;import android.graphics.drawable.Drawable;import android.os.AsyncTask;import android.os.Bundle;import android.view.*;import android.view.View.OnClickListener;import android.view.View.OnKeyListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.AnalogClock;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.CheckBox;import android.widget.DatePicker;import android.widget.EditText;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.ProgressBar;import android.widget.RatingBar;import android.widget.RatingBar.OnRatingBarChangeListener;import android.widget.ScrollView;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;import android.widget.Spinner;import android.widget.TextView;import android.widget.TimePicker;import android.widget.TimePicker.OnTimeChangedListener;import android.widget.Toast;public class MainActivity extends ActionBarActivity {private ListView listView;private MyAdapter adapter;private final String TAG="MainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.i(TAG, "-onCreate-->>");listView=(ListView)this.findViewById(R.id.listView1);adapter=new MyAdapter(getData());listView.setAdapter(adapter);}public List<String> getData(){List<String> list=new ArrayList<String>();for(int i=0;i<10;++i)list.add("jack"+i);return list;}public class MyAdapter extends BaseAdapter{private List<String> list;MyAdapter(List<String> list){this.list=list;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn list.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn list.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubView view =LayoutInflater.from(MainActivity.this).inflate(R.layout.linear, null);TextView textView=(TextView)view.findViewById(R.id.textView1);textView.setText(list.get(position));//TextView textView =new TextView(MainActivity.this);//textView.setText(list.get(position));Log.i(TAG, "-->>"+position);return view;}}protected void onStart(){super.onStart();Log.i(TAG, "-onStart-->>");}protected void onRestart(){super.onRestart();Log.i(TAG, "-onRestart-->>");}protected void onResume(){super.onResume();Log.i(TAG, "-onResume-->>");}protected void onPause(){super.onPause();Log.i(TAG, "-onPause-->>");}protected void onStop(){super.onStop();Log.i(TAG, "-onStop-->>");}protected void onDestroy(){super.onDestroy();Log.i(TAG, "-onDestroy-->>");}@Overridepublic 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;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}

其实主要是

public View getView(int position, View convertView, ViewGroup parent) 

的编写

代码里是这样写的

public View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubView view =LayoutInflater.from(MainActivity.this).inflate(R.layout.linear, null);//TextView textView=(TextView)view.findViewById(R.id.textView1);//textView.setText(list.get(position));////TextView textView =new TextView(MainActivity.this);//textView.setText(list.get(position));Log.i(TAG, "-->>"+position);return view;}

被注掉的两行同样可以,但是会影响运行效率。于是就用后面加//的那三行的那种方法,从 xml文件中加载一个布局,然后往下加东西。

该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:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20sp"        android:textColor="#FF8000" /></LinearLayout>




0 0
原创粉丝点击