我有一个ListView,一个CustomAdapter和一个xml文件" row_layout"为ListView定义一行。 " row_layout"有一个TextView和一个按钮。我的问题是单击按钮,如何从相应的TextView获取访问信息?目前,我从listview的最后一行中的textview获取信息。
public class CustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
View view;
public CustomAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row_layout, null);
}
//Handle buttons and add onClickListeners
Button myButton = (Button)view.findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//DOES NOT WORK HERE AS PLANNED
TextView tv = (TextView) view.findViewById(R.id.myTextView);
notifyDataSetChanged();
}
});
return view;
}
}
}
以下是xml布局:
<RelativeLayout android:id="@+id/root">
<TextView android:id="@+id/myTextView"/>
<Button android:id="@+id/myButton"/>
</RelativeLayout>
答案 0 :(得分:1)
你必须替换
TextView tv = (TextView) layout.findViewById(R.id.myTextView);
带
TextView tv = (TextView) view.findViewById(R.id.myTextView);
您会发现Textview属于您的实际行(在您的代码中为变量view
),而不是您的基本布局。
编辑:您的通胀应该是
view=inflater.inflate(R.layout.row_layout, parent, false);
答案 1 :(得分:1)
替换此
RunAction(new CCFollow(yourNode, CCRect.Zero));
使用:
view = inflater.inflate(R.layout.row_layout, null);
答案 2 :(得分:1)
view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row_layout, null);
//Handle buttons and add onClickListeners
Button myButton = (Button)view.findViewById(R.id.myButton);
TextView textview = (TextView)view.findViewById(R.id.myTextView);}
myButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//DOES NOT WORK HERE AS PLANNED
textview.getText().toString();
notifyDataSetChanged();
}
});
return view;
}