android listview item中的按钮点击事件怎么样添加,讨教

移动开发 码拜 7年前 (2017-04-23) 2061次浏览
如题。
本人的listview在lose_book_list这个activity文件中,在lose_book_item中设置它的显示条目item。
本人的button写在lose_book_item中,当运行之后,每个条目上都会显示有一个button如下图:
android listview item中的按钮点击事件怎么样添加,讨教
lose_book_list中的详细代码为:

package com.example.studentsystembook;
import java.util.List;
import com.example.studentsystembook.Bookdao.BorrowedBook;
import com.example.studentsystembook.dao.BorrowedDao;
import com.example.studentsystembook.lose_book_list.myadapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterViewFlipper;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class lose_book_list extends Activity implements OnItemClickListener {
	private ListView lv;
	private List<BorrowedBook> borrow_books;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.losebook_query_list);
		lv = (ListView) findViewById(R.id.lv_lose_books);
		SharedPreferences sp = getSharedPreferences("stunum",
				Context.MODE_PRIVATE);
		String stu_num = sp.getString("stu_num", "");
		BorrowedDao dao = new BorrowedDao(this);
		borrow_books = dao.findallbysnm(stu_num);
		lv.setAdapter(new myadapter());
		lv.setOnItemClickListener(this);
	}
	public class myadapter extends BaseAdapter {
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return borrow_books.size();
		}
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			BorrowedBook book = borrow_books.get(position);
			View view = View.inflate(lose_book_list.this,
					R.layout.lose_book_list_item, null);
			// 設置textview中的顯示文本
			TextView b_num = (TextView) view.findViewById(R.id.tv_bnum_lose);
			b_num.setText("书号:" + book.getB_num());
			TextView tv_ISBN = (TextView) view.findViewById(R.id.tv_ISBN_lose);
			tv_ISBN.setText("ISBN:" + book.getISBN());
			TextView bname = (TextView) view.findViewById(R.id.bname_lose);
			bname.setText("书名:" + book.getB_name());
			TextView autor = (TextView) view.findViewById(R.id.autor_lose);
			autor.setText("作者:" + book.getB_autor());
			TextView publishment = (TextView) view
					.findViewById(R.id.publishment_lose);
			publishment.setText("出版社:" + book.getB_publishment());
			TextView tv_brtime = (TextView) view
					.findViewById(R.id.tv_brtime_lose);
			tv_brtime.setText("借时:" + book.getBorrow_time());
			TextView tv_ordertime = (TextView) view
					.findViewById(R.id.tv_ordertime_lose);
			tv_ordertime.setText("还时:" + book.getReturn_time());
			return view;
		}
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (position >= 0) {
			Intent intent = new Intent(this, text_view.class);
			startActivity(intent);

		}
	}
}

求高手解析,怎么样在inflater中为这个按钮根据点击的条目位置,设置相应的事件。

解决方案

40

在适配器getview中,获取button后添加。

20

getView中不是可以获取到button控件么,然后给button添加onclicklistener

60

@Override
public View getView(int position, View convertView, ViewGroup parent) {
BorrowedBook book = borrow_books.get(position);
View view = View.inflate(lose_book_list.this,
R.layout.lose_book_list_item, null);

// 設置textview中的顯示文本
TextView b_num = (TextView) view.findViewById(R.id.tv_bnum_lose);
b_num.setText(“书号:” + book.getB_num());

TextView tv_ISBN = (TextView) view.findViewById(R.id.tv_ISBN_lose);
tv_ISBN.setText(“ISBN:” + book.getISBN());

TextView bname = (TextView) view.findViewById(R.id.bname_lose);
bname.setText(“书名:” + book.getB_name());

TextView autor = (TextView) view.findViewById(R.id.autor_lose);
autor.setText(“作者:” + book.getB_autor());

TextView publishment = (TextView) view
.findViewById(R.id.publishment_lose);
publishment.setText(“出版社:” + book.getB_publishment());

TextView tv_brtime = (TextView) view
.findViewById(R.id.tv_brtime_lose);
tv_brtime.setText(“借时:” + book.getBorrow_time());

TextView tv_ordertime = (TextView) view
.findViewById(R.id.tv_ordertime_lose);
tv_ordertime.setText(“还时:” + book.getReturn_time());
btn =  (Button) view
.findViewById(R.id.btn);
btn.setOnclickListener(new View.OnclickListener() {
public void onClick(View v) {
Intent intent = new Intent(this, text_view.class);
startActivity(intent);
});
return view;
}


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明android listview item中的按钮点击事件怎么样添加,讨教
喜欢 (0)
[1034331897@qq.com]
分享 (0)