思路是:因为ViewHolder我们可以拿到每个Item的根布局,所以如果我们为根布局设置单独的OnClick监听并将其开放给Adapter,那不就可以在组装RecyclerView时就能够设置ItemClickListener,只不过这个Listener不是设置到RecyclerView上而是设置到Adapter。
我们首先看ViewHolder的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class MyViewHolder extends ViewHolder implements OnClickListener,OnLongClickListener{
public ImageView iv;
public TextView tv;
private MyItemClickListener mListener;
private MyItemLongClickListener mLongClickListener;
public MyViewHolder(View rootView,MyItemClickListener listener,MyItemLongClickListener longClickListener) {
super(rootView);
iv = (ImageView)rootViewfindViewById(Riditem_iv);
tv = (TextView)rootViewfindViewById(Riditem_tv);
thismListener = listener;
thismLongClickListener = longClickListener;
rootViewsetOnClickListener(this);
rootViewsetOnLongClickListener(this);
}
/
点击监听
/
@Override
public void onClick(View v) {
if(mListener != null){
mListeneronItemClick(v,getPosition());
}
}
/
长按监听
/
@Override
public boolean onLongClick(View arg0) {
if(mLongClickListener != null){
mLongClickListeneronItemLongClick(arg0, getPosition());
}
return true;
}
}</span>
因为在构造ViewHolder时,rootView将作为一个必传参数传递进来,所以我们只需要拿到rootView并给其绑定点击监听事件即可。
下面要考虑的就是怎样把listener传递进来。Demo中设定了监听点击事件的Interface:MyItemClickListener:
1
2
3
public interface MyItemClickListener {
public void onItemClick(View view,int postion);
}
MyItemClickListener模仿ListView的OnItemClickListener,开放了view和position两个参数,这对习惯使用ListView的开发者们使用起来更得心应手。从ViewHolder的代码中可以看到,执行onClick方法时会调用getPosition()将当前Item的位置回调给listener。getPosition()是ViewHolder的内置方法,可直接使用。
上面提到过,listener是设定到Adapter上的,所以Adapter就需要对外开放相关方法:
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflaterfrom(parentgetContext())inflate(Rlayoutitem, parent,false);
MyViewHolder vh = new MyViewHolder(itemView,mItemClickListener,mItemLongClickListener);
return vh;
}
/
设置Item点击监听
@param listener
/
public void setOnItemClickListener(MyItemClickListener listener){
thismItemClickListener = listener;
}
public void setOnItemLongClickListener(MyItemLongClickListener listener){
thismItemLongClickListener = listener;
}
<!--#include file="connasp"-->
<%
id=clng(request("id"))
set rs=connexecute("select from 表名 where id="&id)
responsewrite "<table cellspacing=1 width=600 align='center' border=1 bgcolor=#333333>"
do while not rseof
responsewrite "<tr bgcolor=#ffffff>"
for i=0 to rsfieldscount-1
responsewrite "<td>"&rsfieldsitem(i)value&"</td>"
next
responsewrite "</tr>"
rsmovenext
loop
responsewrite"</table>"
%>
以上是d出页面的代码,遍历字段值,然后通过表格输出
第一步,先建立与数据库的连接
第二步,在programcs里面写程序
using System;
using SystemCollectionsGeneric;
using SystemConfiguration;
using SystemData;
using SystemDataSqlClient;
using SystemLinq;
using SystemText;
using SystemThreadingTasks;
namespace CaterDal
{
public static class SqliteHelper
{
//从配置文本中读取连接字符串
private static string connStr = "Data Source=数据库的名称;Initial Catalog=SWALL;User ID=SA;Password=666";//是这个数据库没错吧?
public static DataTable GetDataTable(string sql,params SqlParameter[] ps)
{
using (SqlConnection conn=new SqlConnection(connStr))
{
//构造适配器对象
SqlDataAdapter pter=new SqlDataAdapter(sql,conn);
//构造数据表,用于接收查询结果
DataTable dt=new DataTable();
//添加参数
pterSelectCommandParametersAddRange(ps);
//执行结果
pterFill(dt);
//返回结果集
return dt;
}
}
}
}
第三步,在项目里面写程序
private void button4_Click(object sender, EventArgs e)
{
DataTable Dt = SqliteHelperGetDataTable("select from jjjj");
dataGridView1DataSource = Dt;
}
}
}
private void button4_Click(object sender, EventArgs e)
{
DataTable Dt = SqliteHelperGetDataTable("select from jjjj");
dataGridView1DataSource = Dt;
}
}
}
private void button4_Click(object sender, EventArgs e)
{
DataTable Dt = SqliteHelperGetDataTable("select from lishiji");
dataGridView1DataSource = Dt;
}
}
}
Form1cs项目名
private void button4_Click(object sender, EventArgs e)
{
DataTable Dt = SqliteHelperGetDataTable("select from jjjj");
dataGridView1DataSource = Dt;
}
}
}
FROM2CS项目名
using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemLinq;
using SystemText;
using SystemThreadingTasks;
using SystemWindowsForms;
using SystemDataOleDb;
using CaterDal;
namespace sqlssss
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataTable Dt = new DataTable();
Dt = SqliteHelperGetDataTable("select from lishiji");
DgvQueueDataSource = Dt;
}
}
}
以上就是关于如何使用RecyclerView显示数据库中的数据全部的内容,包括:如何使用RecyclerView显示数据库中的数据、如何显示数据库全部内容、C#如何把数据库里面的东西读出来,并且显示在文本框里面呢等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)