android listview item中的按钮点击事件如何添加,请教大神

qq_19817377 2016-02-21 09:03:12
如题。
我的listview在lose_book_list这个activity文件中,在lose_book_item中设置它的显示条目item。
我的button写在lose_book_item中,当运行之后,每个条目上都会显示有一个button如下图:

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中为这个按钮根据点击的条目位置,设置相应的事件。
...全文
734 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
NNadea 2016-10-12
  • 打赏
  • 举报
回复
楼主怎么解决的?用楼上的方法吗?
柒加伊 2016-02-21
  • 打赏
  • 举报
回复
@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; }
ken_LG 2016-02-21
  • 打赏
  • 举报
回复
getView中不是可以获取到button控件么,然后给button添加onclicklistener
qq_19817377 2016-02-21
  • 打赏
  • 举报
回复
楼上能再详细些么,获取button后,点击的位置如何确定啊? 大神能贴上些代码么?急求!
李元静 2016-02-21
  • 打赏
  • 举报
回复
在适配器getview中,获取button后添加。
qq_19817377 2016-02-21
  • 打赏
  • 举报
回复
谢谢各位,问题已经解决。

80,354

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧