android arrayadapter 適配器

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">       今天講解的知識點是Arrayadapter這個類,這個是一個適配器,它實現的是Adapter接口,和它類似的有simpleadapter和baseadapter,其中simpleadapter這個類,大家千萬不要爲它的名字忽悠了,其實這個類的功能非常強大,我先結合api把arrayadapter講好,大家學習另外類的時候就很簡單了,接下來,接下來就進入課程的講解:</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>

完成如下功能:

視頻播放地址:http://v.youku.com/v_show/id_XNzMzNTc2ODg0.html

如果有疑問,請發送到我的郵箱:[email protected]

用戶輸入,點擊確定,再次輸入,任一次數,點擊顯示,在按鈕地下,顯示輸入的數據;


主要代碼如下:

package com.example.text111;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {
	
	private ListView list;
	private Button btn_write, btn_show;
	private EditText ed_content;
	private ArrayAdapter<String> adpter1;
	private List<String> ll;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		list = (ListView)findViewById(R.id.list);
		ed_content = (EditText)findViewById(R.id.ed_content);
		btn_write = (Button)findViewById(R.id.btn_write);
		btn_show = (Button)findViewById(R.id.btn_show);
		
		ll = new ArrayList<String>();
		
		btn_write.setOnClickListener(new OnClickListener() {	
			@Override
			public void onClick(View arg0) {
				
				ll.add(ed_content.getText().toString());
				ed_content.setText("");
				ed_content.setHint("請再次輸入");
				
			}
		});
		
		 adpter1 = new ArrayAdapter<String>
		(this, R.layout.show, R.id.content, ll);
		
		btn_show.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
						
				list.setAdapter(adpter1);
			}
		});	
	}

	@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;
	}

}


mainactivity.xml:

<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="match_parent"
        android:layout_height="wrap_content"
        android:text="一個簡單的arrayadapter的例子" />
    
    <EditText 
        android:id="@+id/ed_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入您想輸入的內容"
        />
    
    <Button 
        android:id="@+id/btn_write"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="確定"
        />
    
    <Button 
        android:id="@+id/btn_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="顯示"
        />
    <ListView 
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ></ListView>

</LinearLayout>

show.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/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章