获取成语列表
1.1首先
你得有显示信息的界面:activity_animal.xml。
这里很简单,只需要加入ListView控件,加上id就可以了
<?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" >
<ListView
android:id="@+id/lvAnimalList"
android:layout_width="match_parent"
android:layoutAnimation="@anim/anim_layout_listview"
<strong>android:listSelector="#00000000"</strong>
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
上面加粗的代码可以去掉每次点击列表时黄色的背景。
1.2
下面的文件是用来设置每行数据的
<?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:descendantFocusability="blocksDescendants"
>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="我们爱学习"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<ImageButton
android:focusable="false"
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:layout_alignTop="@+id/tvName"
android:layout_alignParentRight="true"
android:src="@drawable/btnsave" />
</RelativeLayout>
2.然后 你得有需要显示的数据内容(可以是string.xml中的,也可以是从数据库或直接从网络中抓取的数据),这里我们使用数据库。AnimalDao.java
package com.example.istudy.dao;
import java.util.ArrayList;
import java.util.List;
import com.example.istudy.db.DBOpenHelper;
import com.example.istudy.entity.Animal;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class AnimalDao {
private static AnimalDao animalDao;
private SQLiteDatabase db;
/**
* 构造方法私有化
*
* @param context
*/
private AnimalDao(Context context) {
DBOpenHelper dbOpenHelper = new DBOpenHelper(context);
db = dbOpenHelper.openDatabase();
}
/**
* 获取Animal实例
*/
public synchronized static AnimalDao getInstance(Context context) {
if (animalDao == null) {
animalDao = new AnimalDao(context);
}
return animalDao;
}
/**
* 获取所有动物类成语
*/
public List<Animal> getAllAnimals() {
List<Animal> list=new ArrayList<Animal>();
Cursor cursor = db.query("animal", null, null, null, null, null, null);
if(cursor.moveToFirst()){
do{
Animal animal=new Animal();
animal.setId(cursor.getInt(cursor.getColumnIndex("_id")));
animal.setName(cursor.getString(cursor.getColumnIndex("name")));
animal.setAntonym(cursor.getString(cursor.getColumnIndex("antonym")));
animal.setDerivation(cursor.getString(cursor.getColumnIndex("derivation")));
animal.setPronounce(cursor.getString(cursor.getColumnIndex("pronounce")));
animal.setExamples(cursor.getString(cursor.getColumnIndex("examples")));
animal.setExplain(cursor.getString(cursor.getColumnIndex("explain")));
animal.setHomoionym(cursor.getString(cursor.getColumnIndex("homoionym")));
list.add(animal);
}while(cursor.moveToNext());
}
System.out.println("dao.test");
return list;
}
}
3.构造
适配器AnimalAdapter.java
package com.example.istudy.adapter;
import java.util.List;
import com.example.istudy.R;
import com.example.istudy.entity.Animal;
import com.example.istudy.util.DialogUtil;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class AnimalAdapter extends ArrayAdapter<Animal> {
private int resourceId;
private Context context;
public AnimalAdapter(Context context, int resource, List<Animal> objects) {
super(context, resource, objects);
this.context = context;
resourceId = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Animal animal = getItem(position);
View view;
ViewHolder viewHolder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
viewHolder = new ViewHolder();
viewHolder.tvName = (TextView) view.findViewById(R.id.tvName);
viewHolder.btnSave = (ImageView) view.findViewById(R.id.btnSave);
viewHolder.btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, animal.getName(), Toast.LENGTH_LONG).show();
DialogUtil.save_dialog("是否收藏:【" + animal.getName() + "】?",
context);
}
});
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.tvName.setText(animal.getName());
return view;
}
class ViewHolder {
TextView tvName;
ImageView btnSave;
}
}
4.然后呢
(StudyAnimalActivity.java)活动中加入下面的代码就可以了:
initAnimals();
AnimalAdapter animalAdapter=new AnimalAdapter(this, R.layout.animal_item, animalList);
lvAnimalList=(ListView) findViewById(R.id.lvAnimalList);
lvAnimalList.setAdapter(animalAdapter);
运行结果
AnimalAdapter animalAdapter=new AnimalAdapter(this, R.layout.animal_item, animalList);
lvAnimalList=(ListView) findViewById(R.id.lvAnimalList);
lvAnimalList.setAdapter(animalAdapter);
package com.example.istudy.activity;
import java.util.List;
import com.example.istudy.R;
import com.example.istudy.adapter.AnimalAdapter;
import com.example.istudy.dao.AnimalDao;
import com.example.istudy.entity.Animal;
import com.example.istudy.util.DialogUtil;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class StudyAnimalActivity extends Activity {
private List<Animal> animalList;
private AnimalDao animalDao;
private ListView lvAnimalList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animal);
<strong>initAnimals();
AnimalAdapter animalAdapter=new AnimalAdapter(this, R.layout.animal_item, animalList);
lvAnimalList=(ListView) findViewById(R.id.lvAnimalList);
lvAnimalList.setAdapter(animalAdapter);</strong>
lvAnimalList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adaterView, View view, int position,
long id) {
Animal animal=animalList.get(position);
String message=animal.getName()+"\n"+animal.getPronounce()
+"\n【解释】"+animal.getExplain()
+"\n【近义词】"+animal.getHomoionym()
+"\n【反义词】"+animal.getAntonym()
+"\n【来源】"+animal.getDerivation()
+"\n【示例】"+animal.getExamples();
DialogUtil.showDialog(message, StudyAnimalActivity.this);
}
});
}
private void initAnimals(){
animalDao=AnimalDao.getInstance(this);
animalList=animalDao.getAllAnimals();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.study_animal, menu);
return true;
}
}
就这些。运行结果