CustomListAdapter.class
如何从列表视图中获取TextView eng
的值,并分别在两个不同的edittext中设置该值。
public class CustomListAdapter extends ArrayAdapter<DataModel> implements View.OnClickListener{
Context mContext;
ArrayList<DataModel> dataSet;
public CustomListAdapter(ArrayList<DataModel> dataModels, Context context) {
super(context, R.layout.route, dataModels);
this.dataSet = dataModels;
this.mContext = context;
}
private static class ViewHolder {
TextView eng;
TextView hindi;
}
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();
Object object = getItem(position);
DataModel dataModel = (DataModel) object;
switch (v.getId()) {
case R.id.eng:
String s = dataModel.getStationEng();
//mStart.(FareCalculatorActivity.class).getClasses().setText(s.trim().toUpperCase().toString());
break;
case R.id.hindi:
String s1 = dataModel.getStationEng();
break;
}
}
private int lastPosition = -1;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
DataModel dataModel = getItem(position);
ViewHolder viewHolder;
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.route, parent, false);
viewHolder.eng = convertView.findViewById(R.id.eng);
viewHolder.hindi = (TextView) convertView.findViewById(R.id.hindi);
result = convertView;
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
result = convertView;
}
Animation animation = AnimationUtils.loadAnimation(mContext, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
result.startAnimation(animation);
lastPosition = position;
viewHolder.eng.setText(dataModel.getStationEng());
viewHolder.hindi.setText(dataModel.getStationhindi());
viewHolder.eng.setOnClickListener(this);
viewHolder.eng.setTag(position);
viewHolder.hindi.setOnClickListener(this);
viewHolder.hindi.setTag(position);
return convertView;
}
}
从自定义列表视图适配器获取值,并将值设置为一个一个地编辑文本。
这个列表视图是过滤器列表视图,我想要列表视图的值并将其设置为edittext
答案 0 :(得分:1)
您可以创建单独的回调接口,也可以使用View.OnClickListener
。从而修改了您现有的适配器类。
public class CustomListAdapter extends ArrayAdapter<DataModel> {
Context mContext;
ArrayList<DataModel> dataSet;
View.OnClickListener listener;
public CustomListAdapter(ArrayList<DataModel> dataModels, Context context, View.OnClickListener listener) {
super(context, R.layout.route, dataModels);
this.dataSet = dataModels;
this.mContext = context;
this.listener = listener;
}
private static class ViewHolder {
TextView eng;
TextView hindi;
}
private int lastPosition = -1;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
DataModel dataModel = getItem(position);
ViewHolder viewHolder;
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.route, parent, false);
viewHolder.eng = convertView.findViewById(R.id.eng);
viewHolder.hindi = (TextView) convertView.findViewById(R.id.hindi);
result = convertView;
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
result = convertView;
}
Animation animation = AnimationUtils.loadAnimation(mContext, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
result.startAnimation(animation);
lastPosition = position;
viewHolder.eng.setText(dataModel.getStationEng());
viewHolder.hindi.setText(dataModel.getStationhindi());
viewHolder.eng.setOnClickListener(listener);
viewHolder.eng.setTag(position);
viewHolder.hindi.setOnClickListener(listener);
viewHolder.hindi.setTag(position);
return convertView;
}
}
现在更改您在活动/片段中初始化适配器的呼叫,如下所示
CustomListAdapter adapter = new CustomListAdapter(data,context,new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();
Object object = getItem(position);
DataModel dataModel = (DataModel) object;
switch (v.getId()) {
case R.id.eng:
String s = dataModel.getStationEng();
//mStart.(FareCalculatorActivity.class).getClasses().setText(s.trim().toUpperCase().toString());
// set to editText
break;
case R.id.hindi:
String s1 = dataModel.getStationEng();
// set to editText
break;
}
}
});
答案 1 :(得分:0)
解决问题的一些方法是:-
1。使用接口将数据从适配器类传递到活动。
This link will help to find the above mentioned approaches in more detail
希望这会有所帮助。