这是您需要的:
- 一个类包含这些数据:
Date1
, Num1
, Num2
,CalculatedNum
- 一个自定义适配器,从
BaseAdapter
- 内项目的布局
ListView
- 主布局,其中包含
ListView
- 最后,主要活动由二
TextView
、二EditText
和一组成Button
。
这是设置 的最基本的框架ListView
,当然您可以在代码中实现所有视图,但请尝试遵循此模式。它将使您的代码更清晰,更容易适应布局的变化。
#1
package com.example.demo;
public class Data {
private int mX;
private int mY;
private int mSum;
private String mDate;
public Data(int x, int y, int sum, String date) {
mX = x;
mY = y;
mSum = sum;
mDate = date;
}
public int getX() {
return mX;
}
public int getY() {
return mX;
}
public int getSum() {
return mSum;
}
public String getDate() {
return mDate;
}
}
#2
package com.example.demo;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class DemoAdapter extends BaseAdapter {
private Context mContext;
private List<Data> mItems;
private ViewHolder mHolder;
private static LayoutInflater mInflater = null;
public DemoAdapter(Context context, List<Data> items) {
mContext = context;
mItems = items;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return mItems.size();
}
public Object getItem(int position) {
return mItems.get(position);
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
TextView x;
TextView y;
TextView sum;
TextView date;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
mHolder = new ViewHolder();
mHolder.x = (TextView) convertView.findViewById(R.id.text_x);
mHolder.y = (TextView) convertView.findViewById(R.id.text_y);
mHolder.sum = (TextView) convertView.findViewById(R.id.text_sum);
mHolder.date = (TextView) convertView.findViewById(R.id.text_date);
convertView.setTag(mHolder);
}
else {
mHolder = (ViewHolder) convertView.getTag();
}
mHolder.x.setText(Integer.toString(mItems.get(position).getX()));
mHolder.y.setText(Integer.toString(mItems.get(position).getY()));
mHolder.sum.setText(Integer.toString(mItems.get(position).getSum()));
mHolder.date.setText(mItems.get(position).getDate());
return convertView;
}
}
#3
<?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="horizontal" >
<TextView
android:id="@+id/text_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="date">
</TextView>
<TextView
android:id="@+id/text_x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="x">
</TextView>
<TextView
android:id="@+id/text_y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="y">
</TextView>
<TextView
android:id="@+id/text_sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="sum">
</TextView>
</LinearLayout>
#4
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Num 1" />
<EditText
android:id="@+id/edit_x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:ems="10"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Num 2" />
<EditText
android:id="@+id/edit_y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
</LinearLayout>
<Button
android:id="@+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
#5
package com.example.demo;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
private List<Data> mData;
private ListView mListView;
private DemoAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupListView();
Button add = (Button) findViewById(R.id.button_add);
add.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText editX = (EditText) findViewById(R.id.edit_x);
EditText editY = (EditText) findViewById(R.id.edit_y);
int x = Integer.parseInt(editX.getText().toString());
int y = Integer.parseInt(editY.getText().toString());
mData.add(new Data(x, y, x + y, "02/33/2033"));
// add new data to list and refresh
mAdapter.notifyDataSetChanged();
}
});
}
private void prepareDummyData() {
mData = new ArrayList<Data>();
mData.add(new Data(1, 2, 3, "07/22/2012"));
mData.add(new Data(2, 3, 5, "07/23/2012"));
}
private void setupListView() {
// add some data
prepareDummyData();
mListView = (ListView) findViewById(R.id.listview);
mAdapter = new DemoAdapter(this, mData);
mListView.setAdapter(mAdapter);
}
}