当我单击TextView" tvlist"时,如何从适配器发送参数? to Activity" SelectAddressActivity",在SelectAddressActivity中,我有两个EditText,我希望用适配器中的参数填充
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Typeface quicksandRegular = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf");
final View row = inflater.inflate(R.layout.list_item, parent, false);
AddressList al= objects.get(position);
final TextView tvlist = (TextView)row.findViewById(R.id.id_TextView);
tvlist.setTypeface(quicksandRegular);
tvlist.setText(al.get_addresse());
tvlist.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String content = tvlist.getText().toString();
}
});
活动
public class SelectAddressActivity extends Activity {
ArrayList<AddressList> addressList ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_address);
final ListView addressListView = (ListView) findViewById(R.id.addressListView);
final SelectAddressAdapter adapter = new SelectAddressAdapter(this, android.R.layout.simple_list_item_1, addressList);
addressListView.setAdapter(adapter);
}
答案 0 :(得分:3)
您必须在行上发送onclick
,如下所示
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AddressList al= objects.get(position);
Intent intent = new Intent(activity, SelectAddressActivity.class);
intent.putExtra("id", objects.getyourvariable());
activity.startActivity(intent);
}
});
答案 1 :(得分:3)
你可以使用一个接口,在这里我创建了OnClickInAdapter接口,它在适配器类中定义。 将以下代码放在适配器中,
OnClickInAdapter onClickInAdapter;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Typeface quicksandRegular = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf");
final View row = inflater.inflate(R.layout.list_item, parent, false);
AddressList al = objects.get(position);
final TextView tvlist = (TextView) row.findViewById(R.id.id_TextView);
tvlist.setTypeface(quicksandRegular);
tvlist.setText(al.get_addresse());
tvlist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content = tvlist.getText().toString();
try {
onClickInAdapter = (OnClickInAdapter) context;
} catch (ClassCastException e) {
throw new ClassCastException(contxt.toString()
+ " must implement OnClickInAdapter");
}
onClickInAdapter.onClickInAdapter(content);
}
});
}
public interface OnClickInAdapter {
public void onClickInAdapter(String content);
}
现在活动应该实现这个接口,这样当调用适配器中的OnClick方法时,最终调用onCLickInAdapter()的activity的方法,将下面的代码放在你的活动中,
public class SelectAddressActivity extends AppCompatActivity implements MyAdapter.OnClickInAdapter{
@Override
public void onClickInAdapter(String content) {
// you can fill the editText here
}
}
让我知道它是否有效并标记为答案,以便对其他人有用......
答案 2 :(得分:2)
make interface;
public interface AsyncResponse {
void processFinish(Object output);
}
并添加活动
实施AsyncResponse
在Activity中你必须实现processFinish(对象输出)......
,添加成员变量
public AsyncResponse delegate = null;
,添加以下代码:
delegate.processFinish(您的通行证数据);