Android 我怎么能从 baseadapter 执行 onclick 字面上无法理解
Android How can I do an onclick from a baseadapter literally can not figure it out
我有一个列表视图,它绑定到实现自定义适配器的基本适配器。我从数据库中获取数据,因此当用户单击该文本视图时,列表视图行中的每个文本视图都有不同的值我想在 Toast 中显示该值。
这是我的customAdadapter.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#FFFFFF"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:clickable="true"
android:textSize="10sp"
android:text="# votes"
android:id="@+id/votes" />
</RelativeLayout>
这是我的基础适配器
public class LocalFeed_CustomView extends BaseAdapter {
JSONObject names;
Context ctx;
LayoutInflater myiflater;
public LocalFeed_CustomView(JSONObject arr,Context c) {
ctx = c;
names = arr;
myiflater = (LayoutInflater)c.getSystemService(c.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
try {
JSONArray jaLocalstreams = names.getJSONArray("localstreams");
return jaLocalstreams.length();
} catch (Exception e) {
Toast.makeText(ctx,"Error: Please try again",Toast.LENGTH_LONG).show();
return names.length();
}
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, final View convertView, ViewGroup parent) {
View v = convertView;
final CompleteListViewHolder viewHolder;
try {
if(convertView==null) {
LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.customadapter, null);
viewHolder = new CompleteListViewHolder(v);
v.setTag(viewHolder);
}
else {
viewHolder = (CompleteListViewHolder) v.getTag();
}
JSONArray jaLocalstreams = names.getJSONArray("localstreams");
final JSONObject jsonObject = jaLocalstreams.getJSONObject(position);
viewHolder.votes.setText(jsonObject.getString("votes")+ " Votes");
viewHolder.idnumber.setText(jsonObject.getInt("id"));
viewHolder.votes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LocalFeed dd= new LocalFeed();
String vote_id= String.valueOf(viewHolder.idnumber.getText());
// this toast does not fire
Toast.makeText(ctx,"hello"+ vote_id,Toast.LENGTH_SHORT).show();
}
});
return v;
} catch (Exception e) {
e.printStackTrace();
}
return v;
}
}
class CompleteListViewHolder {
public TextView votes,idnumber;
public CompleteListViewHolder(View base) {
votes=(TextView)base.findViewById(R.id.votes);
idnumber=(TextView)base.findViewById(R.id.idnumber);
}
}
请将视图分配给查看器中的 运行 时间标识符,如下所示
改变
if(convertView==null) {
LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.customadapter, null);
viewHolder = new CompleteListViewHolder(v);
v.setTag(viewHolder);
}
至
if(convertView==null) {
LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.customadapter, null);
viewHolder = new CompleteListViewHolder(v);
viewholder.votes = v.findViewById(R.id.votes);
viewholder.idnumber = v.findViewById(R.id.idnumber);
v.setTag(viewHolder);
}
还要确保 TextView 的对齐以及填充和边距正确,以便它可以正确捕获触摸事件
我有一个列表视图,它绑定到实现自定义适配器的基本适配器。我从数据库中获取数据,因此当用户单击该文本视图时,列表视图行中的每个文本视图都有不同的值我想在 Toast 中显示该值。
这是我的customAdadapter.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#FFFFFF"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:clickable="true"
android:textSize="10sp"
android:text="# votes"
android:id="@+id/votes" />
</RelativeLayout>
这是我的基础适配器
public class LocalFeed_CustomView extends BaseAdapter {
JSONObject names;
Context ctx;
LayoutInflater myiflater;
public LocalFeed_CustomView(JSONObject arr,Context c) {
ctx = c;
names = arr;
myiflater = (LayoutInflater)c.getSystemService(c.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
try {
JSONArray jaLocalstreams = names.getJSONArray("localstreams");
return jaLocalstreams.length();
} catch (Exception e) {
Toast.makeText(ctx,"Error: Please try again",Toast.LENGTH_LONG).show();
return names.length();
}
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, final View convertView, ViewGroup parent) {
View v = convertView;
final CompleteListViewHolder viewHolder;
try {
if(convertView==null) {
LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.customadapter, null);
viewHolder = new CompleteListViewHolder(v);
v.setTag(viewHolder);
}
else {
viewHolder = (CompleteListViewHolder) v.getTag();
}
JSONArray jaLocalstreams = names.getJSONArray("localstreams");
final JSONObject jsonObject = jaLocalstreams.getJSONObject(position);
viewHolder.votes.setText(jsonObject.getString("votes")+ " Votes");
viewHolder.idnumber.setText(jsonObject.getInt("id"));
viewHolder.votes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LocalFeed dd= new LocalFeed();
String vote_id= String.valueOf(viewHolder.idnumber.getText());
// this toast does not fire
Toast.makeText(ctx,"hello"+ vote_id,Toast.LENGTH_SHORT).show();
}
});
return v;
} catch (Exception e) {
e.printStackTrace();
}
return v;
}
}
class CompleteListViewHolder {
public TextView votes,idnumber;
public CompleteListViewHolder(View base) {
votes=(TextView)base.findViewById(R.id.votes);
idnumber=(TextView)base.findViewById(R.id.idnumber);
}
}
请将视图分配给查看器中的 运行 时间标识符,如下所示
改变
if(convertView==null) {
LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.customadapter, null);
viewHolder = new CompleteListViewHolder(v);
v.setTag(viewHolder);
}
至
if(convertView==null) {
LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.customadapter, null);
viewHolder = new CompleteListViewHolder(v);
viewholder.votes = v.findViewById(R.id.votes);
viewholder.idnumber = v.findViewById(R.id.idnumber);
v.setTag(viewHolder);
}
还要确保 TextView 的对齐以及填充和边距正确,以便它可以正确捕获触摸事件