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

如果有疑问,请发送到我的邮箱:whsgzcy@foxmail.com

用户输入,点击确定,再次输入,任一次数,点击显示,在按钮地下,显示输入的数据;


主要代码如下:

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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值