您现在的位置是:首页 >精选问答 > 2023-10-25 08:42:24 来源:

ArrayAdapter用法(arrayadapter)

导读 大家好,我是小夏,我来为大家解答以上问题。ArrayAdapter用法,arrayadapter很多人还不知道,现在让我们一起来看看吧!android中的Array ...

大家好,我是小夏,我来为大家解答以上问题。ArrayAdapter用法,arrayadapter很多人还不知道,现在让我们一起来看看吧!

android中的Array adapter类用于把一个列表中的数据绑定到一个listview, 它就像一个桥梁,把数据和视图联系起来,下面以我的编码实践讲解如何使用arrayadapter来实现自定义的显示格式。如下图所示,它的功能是在文本框输入内容,点击OK,可以把输入的数据加到下面的列表中,列表每一行显示输入的内容和时期

1.添加两个layout, 一个是activity的, 一个是每一行数据的

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/ok"

android:clickable="true"

android:id="@+id/btn3"

android:layout_below="@+id/et1"/>

<ListView

android:id = "@+id/listview"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_below="@+id/btn3"

/>

</RelativeLayout>

2.增加一个类用于保存每一行的数据:

<?xml version="1.0" encoding="UTF-8"?>

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background = "#ffffffff"

>

<TextView

android:id="@+id/date"

android:layout_width = "wrap_content"

android:layout_height = "fill_parent"

android:padding = "10dp"

android:scrollbars = "vertical"

android:fadingEdge = "vertical"

android:layout_alignParentRight="true"

android:textColor = "#ffff0000"

/>

<TextView

android:id="@+id/title"

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

android:padding = "10dp"

android:scrollbars = "vertical"

android:fadingEdge = "vertical"

android:layout_alignParentLeft="true"

android:textColor = "#ffff0000"

/>

</RelativeLayout>

3.写一个自定义的array adapter,覆盖原来的getView方法,该方法的功能即是返回一个VIEW,这个view将被用于显示一行数据

package pack.xianle.test;

import java.text.SimpleDateFormat;

import java.util.Date;

public class ToDoItem {

String task;

Date created;

public ToDoItem(String t, Date c){

task = t;

this.created = c;

}

public ToDoItem(String t){

this(t, new Date(java.lang.System.currentTimeMillis()));

}

public String getTask(){

return task;

}

public String getDate(){

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");

String dataString = sdf.format(created);

return dataString;

}

@Override

public String toString(){

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");

String dataString = sdf.format(created);

return "["+ dataString +"]" + task;

}

}

3.写activity的代码:

package pack.xianle.test;

import java.util.List;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.LinearLayout;

import android.widget.TextView;

public class MyArrayAdapter extends ArrayAdapter<ToDoItem> {

int resource;

public MyArrayAdapter(Context c, int r, List<ToDoItem> items){

super(c, r, items);

this.resource = r;

}

@Override

public View getView(int position, View v, ViewGroup parent) {

ToDoItem item=getItem(position);

LinearLayout todoView;

String taskString = item.getTask();

String dateString = item.getDate();

if (v == null) {

todoView = new LinearLayout(getContext());

LayoutInflater iv = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

iv.inflate(resource, todoView, true);

} else {

todoView = (LinearLayout)v;

}

TextView data = (TextView)todoView.findViewById(R.id.date);

TextView text = (TextView)todoView.findViewById(R.id.title);

data.setText(dateString);

text.setText(taskString);

return todoView;

}

}

ArrayAdapter也可以用于Spinner,如下代码所示:

package pack.xianle.test;

import java.util.ArrayList;

import android.app.Activity;

import android.os.Bundle;

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 ToDoList extends Activity {

private ArrayList<ToDoItem> todoItems;

private ListView mListView;

private EditText mEditText;

private MyArrayAdapter aa;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

this.setContentView(R.layout.todolist);

this.mListView = (ListView)this.findViewById(R.id.listview);

this.mEditText = (EditText)this.findViewById(R.id.et1);

todoItems = new ArrayList<ToDoItem>();

aa = new MyArrayAdapter(this, R.layout.todoitem,todoItems);

mListView.setAdapter(aa);

Button btn = (Button)this.findViewById(R.id.btn3);

btn.setOnClickListener(new OnClickListener(){

public void onClick(View v){

ToDoItem item = new ToDoItem(mEditText.getText().toString());

todoItems.add(0, item);

mEditText.setText("");

aa.notifyDataSetChanged();

}

});

}

}

本文到此讲解完毕了,希望对大家有帮助。