今天讲解的知识点是Arrayadapter这个类,这个是一个适配器,它实现的是Adapter接口,和它类似的有simpleadapter和baseadapter,其中simpleadapter这个类,大家千万不要为它的名字忽悠了,其实这个类的功能非常强大,我先结合api把arrayadapter讲好,大家学习另外类的时候就很简单了,接下来,接下来就进入课程的讲解
完成如下功能:
视频播放地址: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:
[html] view plaincopy
<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代码如下
[html] view plaincopy
<?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>
转载于:https://blog.51cto.com/7861921/1544286