继续上一篇,这次先写数据显示页面,首先是每个菜谱显示的界面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relative"
android:orientation="vertical"
android:background="@color/background"
>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dp"
android:dividerPadding="3dp"
android:divider="@color/yellow"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cookname"
android:textSize="18dp"
android:textColor="@color/color_three"
/>
<TextView
android:id="@+id/tv_cookview_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textColor="@color/color_one"
android:shadowRadius="3.0"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="3dp"
android:dividerPadding="3dp"
android:divider="@color/yellow">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cooktype"
android:textSize="18dp"
android:textColor="@color/color_three"
/>
<TextView
android:id="@+id/tv_cookview_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textColor="@color/color_one"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="3dp"
android:dividerPadding="3dp"
android:divider="@color/yellow">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cookfeature"
android:textSize="18dp"
android:textColor="@color/color_three"
/>
<TextView
android:id="@+id/tv_cookview_feature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textColor="@color/color_one"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dp"
android:dividerPadding="3dp"
android:divider="@color/color_one">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cookmaterial"
android:textSize="18dp"
android:textColor="@color/color_three"
/>
<TextView
android:id="@+id/tv_cookview_material"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textColor="@color/color_one"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dp"
android:dividerPadding="3dp"
android:divider="@color/color_one">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cookproduction"
android:textSize="18dp"
android:textColor="@color/color_three"
/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
>
<TextView
android:id="@+id/tv_cookview_production"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:scrollbars="vertical"
android:singleLine="false"
android:maxLines="25"
android:textColor="@color/color_one"
/>
</ScrollView>
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/cookview_collect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="@string/collect"
android:textColor="@color/purple"
android:background="@drawable/buttonstyle"
android:textSize="18dp" />
<Button
android:id="@+id/cookview_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:textColor="@color/purple"
android:background="@drawable/buttonstyle"
android:text="@string/back" />
<Button
android:id="@+id/cookview_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textColor="@color/purple"
android:background="@drawable/buttonstyle"
android:text="@string/edit" />
</RelativeLayout>
接下来我们先写好adapter 由于我们每个listview基本上数据显示结构都是一样的 所有可以基本上使用同一个adapter,新建一个类
package com.example.cookbook;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.cookbook.data.CBInfo;
public class MyAdapters extends BaseAdapter
{
private List<CBInfo> infos=new ArrayList<CBInfo>();
private Context context;
public MyAdapters(Context context,List<CBInfo> infos)
{
this.context=context;
this.infos=infos;
}
public int getCount()
{
return infos.size();
}
public Object getItem(int position)
{
return infos.get(position);
}
public long getItemId(int position)
{
return position;
}
public View getView(int position,View convertview,ViewGroup parent)
{
CBInfo info=infos.get(position);
if(convertview==null)
{
ContactViews views=new ContactViews();
View view=View.inflate(context, R.layout.getall_listview_item, null);
views.tv_cookname=(TextView)view.findViewById(R.id.tv_getall_listview_item_name);
views.tv_cooktype=(TextView)view.findViewById(R.id.tv_getall_listview_item_type);
views.tv_cookfeature=(TextView)view.findViewById(R.id.tv_getall_listview_itme_feature);
views.tv_cookname.setText(info.getName());
views.tv_cooktype.setText(info.getType());
views.tv_cookfeature.setText(info.getFeature());
view.setTag(views);
return view;
}
else
{
ContactViews views=(ContactViews)convertview.getTag();
views.tv_cookname.setText(info.getName());
views.tv_cooktype.setText(info.getType());
views.tv_cookfeature.setText(info.getFeature());
return convertview;
}
}
private class ContactViews
{
TextView tv_cookname;
TextView tv_cooktype;
TextView tv_cookfeature;
}
}
写好adapter之后就可以调用写我们的getall界面了,并同时在getall中直接实现记录上次浏览位置功能
package com.example.cookbook;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.example.cookbook.data.CBInfo;
import com.example.cookbook.data.CBInfoRead;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class GetAll extends Activity{
private MyAdapters adapter;
private List<CBInfo> infos;
private ListView lv;
private static final int complete=0;
private AssetManager am;
private Handler handler=new Handler()
{
public void handleMessage(Message msg)
{
if(msg.what==complete)
{
lv.setAdapter(adapter);
SharedPreferences sp=getSharedPreferences("itemids",0);
int index=sp.getInt("itemid",0);
lv.setSelection(index);
}
}
};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.getall_listview);
lv=(ListView)findViewById(R.id.getall_listview);
infos=new ArrayList<CBInfo>();
new Thread()
{
public void run()
{
CBInfoRead read=new CBInfoRead(GetAll.this);
try
{
infos=read.getAllCooks();
adapter=new MyAdapters(GetAll.this,infos);
Message msg=new Message();
msg.what=complete;
handler.sendMessage(msg);
}
catch (Throwable e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,View view,int position,long id)
{
CBInfo info=new CBInfo();
info=infos.get(position);
Intent intent=new Intent(GetAll.this,CookView.class);
Bundle bundle=new Bundle();
bundle.putSerializable("info", info);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
public void onDestroy()
{
int id=lv.getFirstVisiblePosition();
SharedPreferences sp=getSharedPreferences("itemids",0);
Editor editor=sp.edit();
editor.putInt("itemid", id);
editor.commit();
super.onDestroy();
}
}
getall界面很简单 就是一个简单的listview