[ how can I send to parameter from adapter to Activity ]
how can I send to parameter from adapter when I Click TextView "tvlist" to Activity "SelectAddressActivity", in SelectAddressActivity i have two EditText which i want be filled with parameters from adapters
@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();
}
});
Activity `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);
}`
Answer 1
You have to send onclick on row...like below
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);
}
});
Answer 2
You can do that using an interface, here I have created OnClickInAdapter interface and it defined in the adapter class. Put the below code in your adapter,
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);
}
And now the activity should implement this interface so that when OnClick method in adapter is called, ultimately the activity's method which is onCLickInAdapter() is called, put the below code in your activity,
public class SelectAddressActivity extends AppCompatActivity implements MyAdapter.OnClickInAdapter{
@Override
public void onClickInAdapter(String content) {
// you can fill the editText here
}
}
Let me know if it works and mark as an answer so that it would be useful to others...
Answer 3
make interface;
public interface AsyncResponse { void processFinish(Object output); }
and add activity
implements AsyncResponse
in Activity you have to implement processFinish(Object output)...
in Adapter, add member variable
public AsyncResponse delegate = null;
in setOnClickListener, add this code:
delegate.processFinish(YOUR PASS DATA);