String value ie accountname
is not passed to fragment.
In Adapter Class
Dashboard fragobj = new Dashboard();
bundle = new Bundle();
bundle.putString("accountname", accountName);
// set Fragment class Arguments
fragobj.setArguments(bundle);
In Fragment
lvDashboard = (ListView) view.findViewById(R.id.lvDashboard);
if (getArguments()!= null) {
accountname = getArguments().getString("accountname");
}
tasks = new ArrayList<String>();
tasks.add(tasks.size(),accountname);
lvDashboard.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,tasks));
It looks fine, but string value is not stored in accountname
variable in fragment.
You can use Listener/Callback in your custom Adapter something like this:
public class NameAdapter extends ArrayAdapter<String> {
...
private AdapterListener mListener;
// define listener
public interface AdapterListener {
void onClick(String name);
}
// set the listener. Must be called from the fragment
public void setListener(AdapterListener listener) {
this.mListener = listener;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// view initialization
...
// here sample for button
btButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// get the name based on the position and tell the fragment via listener
mListener.onClick(getItem(position));
}
});
return convertView;
}
}
Then set the listener in your fragment:
lvDashboard = (ListView) view.findViewById(R.id.lvDashboard);
lvDashboard.setAdapter(yourCustomAdapter);
yourCustomAdapter.setListener(new YourCustomAdapter.AdapterListener() {
public void onClick(String name) {
// do something with the string here.
}
});
Or, you can use setOnItemClickListener from the ListView:
lvDashboard.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = parent.getItemAtPosition(position);
// do something with the string here.
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.