当前位置:首页 > 开发 > 移动开发 > 正文

Android BaseAdapter如何获得每一项并添加按钮事件

发表于: 2013-05-21   作者:梦想家dream   来源:转载   浏览:
摘要: package com.hangcheng; import java.io.File; import java.util.ArrayList; import com.example.bdcustomer_demo.R; import com.hangcheng.entity.FileBin; import com.hangcheng.helper.MySQLiteHelper;
package com.hangcheng;

import java.io.File;
import java.util.ArrayList;

import com.example.bdcustomer_demo.R;
import com.hangcheng.entity.FileBin;
import com.hangcheng.helper.MySQLiteHelper;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class DbActivity extends Activity {

	private EditText editText_type;
	private Button btn_search;

	private TextView textView_id;
	private TextView textView_type;
	private TextView textView_path;

	private Button btn_delete;
	private Button btn_play;

	private String file_id = "";
	private String file_type = "";
	private String file_path = "";

	private ListView listView_db;
	ArrayList data;
	private Cursor cursor;

	private FileBin fileBin;

	private String fileid;
	private String type;
	private String path;

	BaseAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dbfile);

		data = new ArrayList();
		initView();

		cursor = new MySQLiteHelper(DbActivity.this, "file.db", null, 1)
				.querydataFromdb(file_id, file_type, file_path);
		cursor.moveToFirst();

		while (cursor.moveToNext()) {
			android.util.Log.e(
					"tag",
					cursor.getString(cursor.getColumnIndex("_id"))
							+ cursor.getString(cursor.getColumnIndex("type"))
							+ cursor.getString(cursor
									.getColumnIndex("filepath")));
		}

		data = new MySQLiteHelper(DbActivity.this, "file.db", null, 1)
				.getfileData();
		android.util.Log.e("MESSAGE", data.size() + "");

		adapter = new BaseAdapter() {

			@Override
			public View getView(final int position, View convertView,
					ViewGroup parent) {

				if (convertView == null) {
					convertView = LayoutInflater.from(DbActivity.this).inflate(
							R.layout.list_view_filedb_items, null);

				}

				FileBin filebin = (FileBin) data.get(position);
				Log.e("MESSAGE", filebin.getId());
				Log.e("MESSAGE", filebin.getType());
				Log.e("MESSAGE", filebin.getFilepath());

				textView_id = (TextView) convertView
						.findViewById(R.id.textViewdb_id);
				textView_type = (TextView) convertView
						.findViewById(R.id.textViewdb_type);
				textView_path = (TextView) convertView
						.findViewById(R.id.textViewdb_path);

				btn_delete = (Button) convertView.findViewById(R.id.btn_delete);
				btn_play = (Button) convertView.findViewById(R.id.btn_play);

				textView_id.setText(filebin.getId());
				textView_type.setText(filebin.getType());
				textView_path.setText(filebin.getFilepath());

				final int id = position;

				btn_play.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {

						Log.e("HAHAHAHHAHA", "进来了");
						Log.e("HAHAHAHHAHA", "当前点击的位置是:" + position + "");
						Log.e("HAHAHAHHAHA",
								((FileBin) data.get(position)).getType());
						Log.e("HAHAHAHHAHA",
								((FileBin) data.get(position)).getFilepath());
						type = ((FileBin) data.get(position)).getType();
						path = ((FileBin) data.get(position)).getFilepath();
						if (type.equals("jpg") || type.equals("png")
								|| type.equals("jpeg")) {
							Intent intent = new Intent(getBaseContext(),
									PictureActivity.class);
							intent.putExtra("picpath", path);
							startActivity(intent);

						}
						if (type.equals("mp4") || type.equals("wav")
								|| type.equals("avi") || type.equals("mp3")) {
							Log.e("HAHAHAHAHHA", "进入播放");
							Intent intent = new Intent(DbActivity.this,
									VideoActivity.class);
							intent.putExtra("videopath", path);
							startActivity(intent);

						}
						if (type.equals("gif")) {
							Intent intent = new Intent(getBaseContext(),
									GifViewActivity.class);
							intent.putExtra("gifpath", path);
							startActivity(intent);
						}

					}
				});

				btn_delete.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						fileid = ((FileBin) data.get(position)).getId();
						MySQLiteHelper helper = new MySQLiteHelper(
								getBaseContext(), "file.db", null, 1);

						helper.deletedataFromdb(Integer.parseInt(fileid));

						data = new MySQLiteHelper(DbActivity.this, "file.db",
								null, 1).getfileData();

						listView_db.setAdapter(adapter);
						listView_db.invalidate();
						adapter.notifyDataSetChanged();

					}
				});

				return convertView;
			}

			@Override
			public long getItemId(int arg0) {
				return arg0;
			}

			@Override
			public Object getItem(int arg0) {
				fileBin = (FileBin) data.get(arg0);
				fileid = fileBin.getId();
				type = fileBin.getType();
				path = fileBin.getFilepath();

				return data.get(arg0);
			}

			@Override
			public int getCount() {
				return data.size();
			}
		};

		listView_db.setAdapter(adapter);

		btn_search.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				file_type = editText_type.getText().toString();

				if (file_type.equals("")) {
					data = new MySQLiteHelper(DbActivity.this, "file.db", null,
							1).getfileData();
				} else {

					data = new MySQLiteHelper(DbActivity.this, "file.db", null,
							1).getfilebyTypeData(file_type);
				}

				cursor.moveToFirst();
				listView_db.setAdapter(new BaseAdapter() {

					@Override
					public View getView(final int position, View convertView,
							ViewGroup parent) {

						if (convertView == null) {
							convertView = LayoutInflater.from(DbActivity.this)
									.inflate(R.layout.list_view_filedb_items,
											null);

						}

						FileBin filebin = (FileBin) data.get(position);
						Log.e("MESSAGE", filebin.getId());
						Log.e("MESSAGE", filebin.getType());
						Log.e("MESSAGE", filebin.getFilepath());

						textView_id = (TextView) convertView
								.findViewById(R.id.textViewdb_id);
						textView_type = (TextView) convertView
								.findViewById(R.id.textViewdb_type);
						textView_path = (TextView) convertView
								.findViewById(R.id.textViewdb_path);

						btn_delete = (Button) convertView
								.findViewById(R.id.btn_delete);
						btn_play = (Button) convertView
								.findViewById(R.id.btn_play);

						textView_id.setText(filebin.getId());
						textView_type.setText(filebin.getType());
						textView_path.setText(filebin.getFilepath());

						type = filebin.getType();
						path = filebin.getFilepath();

						btn_play.setOnClickListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {

								Log.e("HAHAHAHHAHA", "进来了");
								Log.e("HAHAHAHHAHA", "当前点击的位置是:" + position
										+ "");
								Log.e("HAHAHAHHAHA", ((FileBin) data
										.get(position)).getType());
								Log.e("HAHAHAHHAHA", ((FileBin) data
										.get(position)).getFilepath());
								type = ((FileBin) data.get(position)).getType();
								path = ((FileBin) data.get(position))
										.getFilepath();
								if (type.equals("jpg") || type.equals("png")
										|| type.equals("jpeg")) {
									Intent intent = new Intent(
											getBaseContext(),
											PictureActivity.class);
									intent.putExtra("picpath", path);
									startActivity(intent);

								}
								if (type.equals("mp4") || type.equals("wav")
										|| type.equals("avi")
										|| type.equals("mp3")) {
									Log.e("HAHAHAHAHHA", "进入播放");
									Intent intent = new Intent(DbActivity.this,
											VideoActivity.class);
									intent.putExtra("videopath", path);
									startActivity(intent);

								}
								if (type.equals("gif")) {
									Intent intent = new Intent(
											getBaseContext(),
											GifViewActivity.class);
									intent.putExtra("gifpath", path);
									startActivity(intent);
								}

							}
						});

						btn_delete
								.setOnClickListener(new View.OnClickListener() {

									@Override
									public void onClick(View v) {
										fileid = ((FileBin) data.get(position))
												.getId();
										MySQLiteHelper helper = new MySQLiteHelper(
												getBaseContext(), "file.db",
												null, 1);

										helper.deletedataFromdb(Integer
												.parseInt(fileid));

										data = new MySQLiteHelper(
												DbActivity.this, "file.db",
												null, 1).getfileData();

										listView_db.setAdapter(adapter);
										listView_db.invalidate();
										adapter.notifyDataSetChanged();

									}
								});

						return convertView;
					}

					@Override
					public long getItemId(int arg0) {
						return arg0;
					}

					@Override
					public Object getItem(int arg0) {
						return data.get(arg0);
					}

					@Override
					public int getCount() {
						return data.size();
					}
				});

			}

		});

	}

	private void initView() {
		editText_type = (EditText) findViewById(R.id.file_type);
		btn_search = (Button) findViewById(R.id.btn_search);
		listView_db = (ListView) findViewById(R.id.listview_db);

	}

}

Android BaseAdapter如何获得每一项并添加按钮事件

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
第一种:匿名内部类作为事件监听器类 大部分时候,事件处理器都没有什么利用价值(可利用代码通常都
前面我们已经学习了怎样新建一个android项目。 Android实战(五)------Myeclipse新建运行Android项目
Android程序不仅可以像《【Android】利用xml文件布局修改Helloworld程序》(点击打开链接)利用xml
今天编写了一个小程序,运行的时候总是出现如下的错误 找了好久,问了eoeAndroid群里的高手们才知道
哎,今晚就一个小问题耽误了好长时间。前面两篇日志就是因为开始在List中加入Button后没法正确给这
效果图 2、xml 布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:androi
修改xml文件就可以添加按钮,文件位置/frameworks/base/core/res/res/layout/status_bar.xml 绿色部
用户在点击手机的返回按钮时,默认是推出当前的activty,但是有时用户不小心按到返回,所以需要给用
用户在点击手机的返回按钮时,默认是推出当前的activty,但是有时用户不小心按到返回,所以需要给用
今天纪录一下滑动按钮功能。。 首先效果图: 然后是分别建立三个文件,第一个是main.class,第二个
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号