安卓ListView获取信息并显示

本文介绍了一个简单的应用程序,用于从数据库中获取并展示动物类成语列表。通过定义布局、创建适配器及设置列表视图,实现了成语数据的有效展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获取成语列表
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);
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;
	}

}
就这些。
运行结果




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值