Master Detail Flow While clicking on recyclerview I get null exception
Well, I have recyclerView and a fragment as the examples of of Master Detail Flows
now i'm getting my data from json and i'm parsing it in the recyclerView perfectly
the problem is whenever I click on it I get null exception..
public class ItemDetailFragment extends Fragment {
/**
* The fragment argument representing the item ID that this fragment
* represents.
*/
public static final String ARG_ITEM_ID = "item_id";
/**
* The dummy content this fragment is presenting.
*/
private DummyContent.DummyItem mItem;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ItemDetailFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID)) {
// Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader
// to load content from a content provider.
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
Activity activity = this.getActivity();
CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
if (appBarLayout != null) {
appBarLayout.setTitle(mItem.id);
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.item_detail, container, false);
// Show the dummy content as text in a TextView.
if (mItem != null) {
((TextView) rootView.findViewById(R.id.item_detail)).setText("hello");
}
return rootView;
}
and this is the Activity
public class ItemDetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own detail action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
// Show the Up button in the action bar.
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
// savedInstanceState is non-null when there is fragment state
// saved from previous configurations of this activity
// (e.g. when rotating the screen from portrait to landscape).
// In this case, the fragment will automatically be re-added
// to its container so we don't need to manually add it.
// For more information, see the Fragments API guide at:
//
// http://developer.android.com/guide/components/fragments.html
//
if (savedInstanceState == null) {
// Create the detail fragment and add it to the activity
// using a fragment transaction.
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID,
getIntent().getStringExtra(ItemDetailFragment.ARG_ITEM_ID));
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.add(R.id.item_detail_container, fragment)
.commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
navigateUpTo(new Intent(this, ItemListActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
this is where i get the json and ItemlistActivity
public class ItemListActivity extends AppCompatActivity {
View recyclerView;
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
private boolean mTwoPane;
RequestQueue queue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
queue = Volley.newRequestQueue(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle(getTitle());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
recyclerView = findViewById(R.id.item_list);
assert recyclerView != null;
if (findViewById(R.id.item_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-w900dp).
// If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
}
connection();
}
private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(DummyContent.ITEMS));
}
public class SimpleItemRecyclerViewAdapter
extends RecyclerView.Adapter<ViewHolder> {
private final List<DummyContent.DummyItem> mValues;
public SimpleItemRecyclerViewAdapter(List<DummyContent.DummyItem> items) {
mValues = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_list_content, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
String profilePicUrl = mValues.get(position).profile_pic;
String full_name = mValues.get(position).full_name;
String state = mValues.get(position).state;
String city = mValues.get(position).city;
String phoneNo = mValues.get(position).phone_no;
holder.full_name.setText("Name : " +full_name);
holder.state.setText("State : " +state);
holder.city.setText("City : " +city);
holder.phone_number.setText("Phone Number : " +phoneNo);
Glide.with(getApplicationContext())
.load(profilePicUrl)
.fitCenter()
.override(500,500)
.into(holder.profile_pic);
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, holder.mItem.id);
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment)
.commit();
} else {
Context context = v.getContext();
Intent intent = new Intent(context, ItemDetailActivity.class);
Log.e("hello",holder.mItem.full_name);
intent.putExtra(ItemDetailFragment.ARG_ITEM_ID, holder.mItem.id);
context.startActivity(intent);
}
}
});
}
@Override
public int getItemCount() {
return mValues.size();
}
}
public void connection(){
final String url = "aaaaa";
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
// display response
try {
JSONArray jsonArray = response.getJSONArray("results");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject test = jsonArray.getJSONObject(i);
String gender = test.getString("gender");
String full_name = test.getJSONObject("name").getString("title") + " " +
test.getJSONObject("name").getString("first") + " " +
test.getJSONObject("name").getString("last");
String state = test.getJSONObject("location").getString("state");
String city = test.getJSONObject("location").getString("city");
String phone_no = test.getString("phone");
String profile_pic = test.getJSONObject("picture").getString("large");
String id = test.getJSONObject("id").getString("name");
Log.d("test2222", gender + " n " + full_name + " n " + state + " n " + city + " n " + phone_no + "n" + profile_pic
+ "n" + id);
addItem(new DummyContent.DummyItem(gender,profile_pic, full_name, state, city, phone_no,"hello"));
}
setupRecyclerView((RecyclerView) recyclerView);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", error.toString());
Toast.makeText(getApplicationContext(),"check internet connection",Toast.LENGTH_SHORT).show();
}
}
);
// add it to the RequestQueue
queue.add(getRequest);
}
and at last dummycontent
public class DummyContent {
/**
* An array of sample (dummy) items.
*/
public static final List<DummyItem> ITEMS = new ArrayList<DummyItem>();
/**
* A map of sample (dummy) items, by ID.
*/
public static final Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();
private static final int COUNT = 25;
static {
// Add some sample items.
}
public static void addItem(DummyItem item) {
ITEMS.add(item);
ITEM_MAP.put(item.full_name, item);
}
/**
* A dummy item representing a piece of content.
*/
public static class DummyItem {
public final String id;
public final String profile_pic;
public final String full_name;
public final String state;
public final String city;
public final String phone_no;
public final String data;
public DummyItem(String id ,String profile_pic, String full_name, String state ,String city,String phone_no,String data) {
this.id = id;
this.profile_pic = profile_pic;
this.full_name = full_name;
this.state = state;
this.city = city;
this.phone_no = phone_no;
this.data = data;
}
@Override
public String toString() {
return full_name;
}
}
}
now the thing is that I want to get same data (text or image) to the fragment
my problem is that I can't even show the fragment with custom "hello world" textview i get exception whenever I click on the recyclerview so explain to me where is the wrong in my code thanks
and this is the ViewHolder
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final ImageView profile_pic;
public final TextView full_name;
public final TextView state;
public final TextView city;
public final TextView phone_number;
public DummyContent.DummyItem mItem;
public ViewHolder(View view) {
super(view);
mView = view;
profile_pic = (ImageView) view.findViewById(R.id.profile_pic);
full_name = (TextView) view.findViewById(R.id.full_name_txt);
state = (TextView) view.findViewById(R.id.state_txt);
city = (TextView) view.findViewById(R.id.city_txt);
phone_number = (TextView) view.findViewById(R.id.phone_no_txt);
}
@Override
public String toString() {
return super.toString() + " '" + full_name.getText() + "'";
}
}
android android-recyclerview master-detail
add a comment |
Well, I have recyclerView and a fragment as the examples of of Master Detail Flows
now i'm getting my data from json and i'm parsing it in the recyclerView perfectly
the problem is whenever I click on it I get null exception..
public class ItemDetailFragment extends Fragment {
/**
* The fragment argument representing the item ID that this fragment
* represents.
*/
public static final String ARG_ITEM_ID = "item_id";
/**
* The dummy content this fragment is presenting.
*/
private DummyContent.DummyItem mItem;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ItemDetailFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID)) {
// Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader
// to load content from a content provider.
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
Activity activity = this.getActivity();
CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
if (appBarLayout != null) {
appBarLayout.setTitle(mItem.id);
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.item_detail, container, false);
// Show the dummy content as text in a TextView.
if (mItem != null) {
((TextView) rootView.findViewById(R.id.item_detail)).setText("hello");
}
return rootView;
}
and this is the Activity
public class ItemDetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own detail action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
// Show the Up button in the action bar.
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
// savedInstanceState is non-null when there is fragment state
// saved from previous configurations of this activity
// (e.g. when rotating the screen from portrait to landscape).
// In this case, the fragment will automatically be re-added
// to its container so we don't need to manually add it.
// For more information, see the Fragments API guide at:
//
// http://developer.android.com/guide/components/fragments.html
//
if (savedInstanceState == null) {
// Create the detail fragment and add it to the activity
// using a fragment transaction.
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID,
getIntent().getStringExtra(ItemDetailFragment.ARG_ITEM_ID));
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.add(R.id.item_detail_container, fragment)
.commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
navigateUpTo(new Intent(this, ItemListActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
this is where i get the json and ItemlistActivity
public class ItemListActivity extends AppCompatActivity {
View recyclerView;
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
private boolean mTwoPane;
RequestQueue queue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
queue = Volley.newRequestQueue(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle(getTitle());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
recyclerView = findViewById(R.id.item_list);
assert recyclerView != null;
if (findViewById(R.id.item_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-w900dp).
// If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
}
connection();
}
private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(DummyContent.ITEMS));
}
public class SimpleItemRecyclerViewAdapter
extends RecyclerView.Adapter<ViewHolder> {
private final List<DummyContent.DummyItem> mValues;
public SimpleItemRecyclerViewAdapter(List<DummyContent.DummyItem> items) {
mValues = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_list_content, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
String profilePicUrl = mValues.get(position).profile_pic;
String full_name = mValues.get(position).full_name;
String state = mValues.get(position).state;
String city = mValues.get(position).city;
String phoneNo = mValues.get(position).phone_no;
holder.full_name.setText("Name : " +full_name);
holder.state.setText("State : " +state);
holder.city.setText("City : " +city);
holder.phone_number.setText("Phone Number : " +phoneNo);
Glide.with(getApplicationContext())
.load(profilePicUrl)
.fitCenter()
.override(500,500)
.into(holder.profile_pic);
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, holder.mItem.id);
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment)
.commit();
} else {
Context context = v.getContext();
Intent intent = new Intent(context, ItemDetailActivity.class);
Log.e("hello",holder.mItem.full_name);
intent.putExtra(ItemDetailFragment.ARG_ITEM_ID, holder.mItem.id);
context.startActivity(intent);
}
}
});
}
@Override
public int getItemCount() {
return mValues.size();
}
}
public void connection(){
final String url = "aaaaa";
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
// display response
try {
JSONArray jsonArray = response.getJSONArray("results");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject test = jsonArray.getJSONObject(i);
String gender = test.getString("gender");
String full_name = test.getJSONObject("name").getString("title") + " " +
test.getJSONObject("name").getString("first") + " " +
test.getJSONObject("name").getString("last");
String state = test.getJSONObject("location").getString("state");
String city = test.getJSONObject("location").getString("city");
String phone_no = test.getString("phone");
String profile_pic = test.getJSONObject("picture").getString("large");
String id = test.getJSONObject("id").getString("name");
Log.d("test2222", gender + " n " + full_name + " n " + state + " n " + city + " n " + phone_no + "n" + profile_pic
+ "n" + id);
addItem(new DummyContent.DummyItem(gender,profile_pic, full_name, state, city, phone_no,"hello"));
}
setupRecyclerView((RecyclerView) recyclerView);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", error.toString());
Toast.makeText(getApplicationContext(),"check internet connection",Toast.LENGTH_SHORT).show();
}
}
);
// add it to the RequestQueue
queue.add(getRequest);
}
and at last dummycontent
public class DummyContent {
/**
* An array of sample (dummy) items.
*/
public static final List<DummyItem> ITEMS = new ArrayList<DummyItem>();
/**
* A map of sample (dummy) items, by ID.
*/
public static final Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();
private static final int COUNT = 25;
static {
// Add some sample items.
}
public static void addItem(DummyItem item) {
ITEMS.add(item);
ITEM_MAP.put(item.full_name, item);
}
/**
* A dummy item representing a piece of content.
*/
public static class DummyItem {
public final String id;
public final String profile_pic;
public final String full_name;
public final String state;
public final String city;
public final String phone_no;
public final String data;
public DummyItem(String id ,String profile_pic, String full_name, String state ,String city,String phone_no,String data) {
this.id = id;
this.profile_pic = profile_pic;
this.full_name = full_name;
this.state = state;
this.city = city;
this.phone_no = phone_no;
this.data = data;
}
@Override
public String toString() {
return full_name;
}
}
}
now the thing is that I want to get same data (text or image) to the fragment
my problem is that I can't even show the fragment with custom "hello world" textview i get exception whenever I click on the recyclerview so explain to me where is the wrong in my code thanks
and this is the ViewHolder
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final ImageView profile_pic;
public final TextView full_name;
public final TextView state;
public final TextView city;
public final TextView phone_number;
public DummyContent.DummyItem mItem;
public ViewHolder(View view) {
super(view);
mView = view;
profile_pic = (ImageView) view.findViewById(R.id.profile_pic);
full_name = (TextView) view.findViewById(R.id.full_name_txt);
state = (TextView) view.findViewById(R.id.state_txt);
city = (TextView) view.findViewById(R.id.city_txt);
phone_number = (TextView) view.findViewById(R.id.phone_no_txt);
}
@Override
public String toString() {
return super.toString() + " '" + full_name.getText() + "'";
}
}
android android-recyclerview master-detail
I'm not sure how SimpleItemRecyclerViewAdapter works, but Isn't the ViewHolder class required in recyclerview adapter where we initialize all the layout items?
– ManishPrajapati
Nov 14 '18 at 11:35
I added the ViewHolder for you
– user7596908
Nov 14 '18 at 12:18
little help please people
– user7596908
Nov 15 '18 at 5:40
Are you able to see the list of data in recyclerView? Only clicking leads to Crash? I see that you are using View recyclerView instead of RecyclerView recyclerView. Also, have you assigned the Layout Manager to recyclerView like recyclerView.setLayoutManager(new LinearLayoutManager(this)); ??
– ManishPrajapati
Nov 15 '18 at 6:13
yes i can see the data but whenever i click on it i'm sending ARG_ITEM_ID with bundle the problem is with this it's always null although i'm giving data for it
– user7596908
Nov 15 '18 at 6:28
add a comment |
Well, I have recyclerView and a fragment as the examples of of Master Detail Flows
now i'm getting my data from json and i'm parsing it in the recyclerView perfectly
the problem is whenever I click on it I get null exception..
public class ItemDetailFragment extends Fragment {
/**
* The fragment argument representing the item ID that this fragment
* represents.
*/
public static final String ARG_ITEM_ID = "item_id";
/**
* The dummy content this fragment is presenting.
*/
private DummyContent.DummyItem mItem;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ItemDetailFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID)) {
// Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader
// to load content from a content provider.
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
Activity activity = this.getActivity();
CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
if (appBarLayout != null) {
appBarLayout.setTitle(mItem.id);
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.item_detail, container, false);
// Show the dummy content as text in a TextView.
if (mItem != null) {
((TextView) rootView.findViewById(R.id.item_detail)).setText("hello");
}
return rootView;
}
and this is the Activity
public class ItemDetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own detail action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
// Show the Up button in the action bar.
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
// savedInstanceState is non-null when there is fragment state
// saved from previous configurations of this activity
// (e.g. when rotating the screen from portrait to landscape).
// In this case, the fragment will automatically be re-added
// to its container so we don't need to manually add it.
// For more information, see the Fragments API guide at:
//
// http://developer.android.com/guide/components/fragments.html
//
if (savedInstanceState == null) {
// Create the detail fragment and add it to the activity
// using a fragment transaction.
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID,
getIntent().getStringExtra(ItemDetailFragment.ARG_ITEM_ID));
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.add(R.id.item_detail_container, fragment)
.commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
navigateUpTo(new Intent(this, ItemListActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
this is where i get the json and ItemlistActivity
public class ItemListActivity extends AppCompatActivity {
View recyclerView;
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
private boolean mTwoPane;
RequestQueue queue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
queue = Volley.newRequestQueue(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle(getTitle());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
recyclerView = findViewById(R.id.item_list);
assert recyclerView != null;
if (findViewById(R.id.item_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-w900dp).
// If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
}
connection();
}
private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(DummyContent.ITEMS));
}
public class SimpleItemRecyclerViewAdapter
extends RecyclerView.Adapter<ViewHolder> {
private final List<DummyContent.DummyItem> mValues;
public SimpleItemRecyclerViewAdapter(List<DummyContent.DummyItem> items) {
mValues = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_list_content, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
String profilePicUrl = mValues.get(position).profile_pic;
String full_name = mValues.get(position).full_name;
String state = mValues.get(position).state;
String city = mValues.get(position).city;
String phoneNo = mValues.get(position).phone_no;
holder.full_name.setText("Name : " +full_name);
holder.state.setText("State : " +state);
holder.city.setText("City : " +city);
holder.phone_number.setText("Phone Number : " +phoneNo);
Glide.with(getApplicationContext())
.load(profilePicUrl)
.fitCenter()
.override(500,500)
.into(holder.profile_pic);
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, holder.mItem.id);
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment)
.commit();
} else {
Context context = v.getContext();
Intent intent = new Intent(context, ItemDetailActivity.class);
Log.e("hello",holder.mItem.full_name);
intent.putExtra(ItemDetailFragment.ARG_ITEM_ID, holder.mItem.id);
context.startActivity(intent);
}
}
});
}
@Override
public int getItemCount() {
return mValues.size();
}
}
public void connection(){
final String url = "aaaaa";
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
// display response
try {
JSONArray jsonArray = response.getJSONArray("results");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject test = jsonArray.getJSONObject(i);
String gender = test.getString("gender");
String full_name = test.getJSONObject("name").getString("title") + " " +
test.getJSONObject("name").getString("first") + " " +
test.getJSONObject("name").getString("last");
String state = test.getJSONObject("location").getString("state");
String city = test.getJSONObject("location").getString("city");
String phone_no = test.getString("phone");
String profile_pic = test.getJSONObject("picture").getString("large");
String id = test.getJSONObject("id").getString("name");
Log.d("test2222", gender + " n " + full_name + " n " + state + " n " + city + " n " + phone_no + "n" + profile_pic
+ "n" + id);
addItem(new DummyContent.DummyItem(gender,profile_pic, full_name, state, city, phone_no,"hello"));
}
setupRecyclerView((RecyclerView) recyclerView);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", error.toString());
Toast.makeText(getApplicationContext(),"check internet connection",Toast.LENGTH_SHORT).show();
}
}
);
// add it to the RequestQueue
queue.add(getRequest);
}
and at last dummycontent
public class DummyContent {
/**
* An array of sample (dummy) items.
*/
public static final List<DummyItem> ITEMS = new ArrayList<DummyItem>();
/**
* A map of sample (dummy) items, by ID.
*/
public static final Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();
private static final int COUNT = 25;
static {
// Add some sample items.
}
public static void addItem(DummyItem item) {
ITEMS.add(item);
ITEM_MAP.put(item.full_name, item);
}
/**
* A dummy item representing a piece of content.
*/
public static class DummyItem {
public final String id;
public final String profile_pic;
public final String full_name;
public final String state;
public final String city;
public final String phone_no;
public final String data;
public DummyItem(String id ,String profile_pic, String full_name, String state ,String city,String phone_no,String data) {
this.id = id;
this.profile_pic = profile_pic;
this.full_name = full_name;
this.state = state;
this.city = city;
this.phone_no = phone_no;
this.data = data;
}
@Override
public String toString() {
return full_name;
}
}
}
now the thing is that I want to get same data (text or image) to the fragment
my problem is that I can't even show the fragment with custom "hello world" textview i get exception whenever I click on the recyclerview so explain to me where is the wrong in my code thanks
and this is the ViewHolder
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final ImageView profile_pic;
public final TextView full_name;
public final TextView state;
public final TextView city;
public final TextView phone_number;
public DummyContent.DummyItem mItem;
public ViewHolder(View view) {
super(view);
mView = view;
profile_pic = (ImageView) view.findViewById(R.id.profile_pic);
full_name = (TextView) view.findViewById(R.id.full_name_txt);
state = (TextView) view.findViewById(R.id.state_txt);
city = (TextView) view.findViewById(R.id.city_txt);
phone_number = (TextView) view.findViewById(R.id.phone_no_txt);
}
@Override
public String toString() {
return super.toString() + " '" + full_name.getText() + "'";
}
}
android android-recyclerview master-detail
Well, I have recyclerView and a fragment as the examples of of Master Detail Flows
now i'm getting my data from json and i'm parsing it in the recyclerView perfectly
the problem is whenever I click on it I get null exception..
public class ItemDetailFragment extends Fragment {
/**
* The fragment argument representing the item ID that this fragment
* represents.
*/
public static final String ARG_ITEM_ID = "item_id";
/**
* The dummy content this fragment is presenting.
*/
private DummyContent.DummyItem mItem;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ItemDetailFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID)) {
// Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader
// to load content from a content provider.
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
Activity activity = this.getActivity();
CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
if (appBarLayout != null) {
appBarLayout.setTitle(mItem.id);
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.item_detail, container, false);
// Show the dummy content as text in a TextView.
if (mItem != null) {
((TextView) rootView.findViewById(R.id.item_detail)).setText("hello");
}
return rootView;
}
and this is the Activity
public class ItemDetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own detail action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
// Show the Up button in the action bar.
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
// savedInstanceState is non-null when there is fragment state
// saved from previous configurations of this activity
// (e.g. when rotating the screen from portrait to landscape).
// In this case, the fragment will automatically be re-added
// to its container so we don't need to manually add it.
// For more information, see the Fragments API guide at:
//
// http://developer.android.com/guide/components/fragments.html
//
if (savedInstanceState == null) {
// Create the detail fragment and add it to the activity
// using a fragment transaction.
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID,
getIntent().getStringExtra(ItemDetailFragment.ARG_ITEM_ID));
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.add(R.id.item_detail_container, fragment)
.commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
navigateUpTo(new Intent(this, ItemListActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
this is where i get the json and ItemlistActivity
public class ItemListActivity extends AppCompatActivity {
View recyclerView;
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
private boolean mTwoPane;
RequestQueue queue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
queue = Volley.newRequestQueue(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle(getTitle());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
recyclerView = findViewById(R.id.item_list);
assert recyclerView != null;
if (findViewById(R.id.item_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-w900dp).
// If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
}
connection();
}
private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(DummyContent.ITEMS));
}
public class SimpleItemRecyclerViewAdapter
extends RecyclerView.Adapter<ViewHolder> {
private final List<DummyContent.DummyItem> mValues;
public SimpleItemRecyclerViewAdapter(List<DummyContent.DummyItem> items) {
mValues = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_list_content, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
String profilePicUrl = mValues.get(position).profile_pic;
String full_name = mValues.get(position).full_name;
String state = mValues.get(position).state;
String city = mValues.get(position).city;
String phoneNo = mValues.get(position).phone_no;
holder.full_name.setText("Name : " +full_name);
holder.state.setText("State : " +state);
holder.city.setText("City : " +city);
holder.phone_number.setText("Phone Number : " +phoneNo);
Glide.with(getApplicationContext())
.load(profilePicUrl)
.fitCenter()
.override(500,500)
.into(holder.profile_pic);
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, holder.mItem.id);
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment)
.commit();
} else {
Context context = v.getContext();
Intent intent = new Intent(context, ItemDetailActivity.class);
Log.e("hello",holder.mItem.full_name);
intent.putExtra(ItemDetailFragment.ARG_ITEM_ID, holder.mItem.id);
context.startActivity(intent);
}
}
});
}
@Override
public int getItemCount() {
return mValues.size();
}
}
public void connection(){
final String url = "aaaaa";
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
// display response
try {
JSONArray jsonArray = response.getJSONArray("results");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject test = jsonArray.getJSONObject(i);
String gender = test.getString("gender");
String full_name = test.getJSONObject("name").getString("title") + " " +
test.getJSONObject("name").getString("first") + " " +
test.getJSONObject("name").getString("last");
String state = test.getJSONObject("location").getString("state");
String city = test.getJSONObject("location").getString("city");
String phone_no = test.getString("phone");
String profile_pic = test.getJSONObject("picture").getString("large");
String id = test.getJSONObject("id").getString("name");
Log.d("test2222", gender + " n " + full_name + " n " + state + " n " + city + " n " + phone_no + "n" + profile_pic
+ "n" + id);
addItem(new DummyContent.DummyItem(gender,profile_pic, full_name, state, city, phone_no,"hello"));
}
setupRecyclerView((RecyclerView) recyclerView);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", error.toString());
Toast.makeText(getApplicationContext(),"check internet connection",Toast.LENGTH_SHORT).show();
}
}
);
// add it to the RequestQueue
queue.add(getRequest);
}
and at last dummycontent
public class DummyContent {
/**
* An array of sample (dummy) items.
*/
public static final List<DummyItem> ITEMS = new ArrayList<DummyItem>();
/**
* A map of sample (dummy) items, by ID.
*/
public static final Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();
private static final int COUNT = 25;
static {
// Add some sample items.
}
public static void addItem(DummyItem item) {
ITEMS.add(item);
ITEM_MAP.put(item.full_name, item);
}
/**
* A dummy item representing a piece of content.
*/
public static class DummyItem {
public final String id;
public final String profile_pic;
public final String full_name;
public final String state;
public final String city;
public final String phone_no;
public final String data;
public DummyItem(String id ,String profile_pic, String full_name, String state ,String city,String phone_no,String data) {
this.id = id;
this.profile_pic = profile_pic;
this.full_name = full_name;
this.state = state;
this.city = city;
this.phone_no = phone_no;
this.data = data;
}
@Override
public String toString() {
return full_name;
}
}
}
now the thing is that I want to get same data (text or image) to the fragment
my problem is that I can't even show the fragment with custom "hello world" textview i get exception whenever I click on the recyclerview so explain to me where is the wrong in my code thanks
and this is the ViewHolder
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final ImageView profile_pic;
public final TextView full_name;
public final TextView state;
public final TextView city;
public final TextView phone_number;
public DummyContent.DummyItem mItem;
public ViewHolder(View view) {
super(view);
mView = view;
profile_pic = (ImageView) view.findViewById(R.id.profile_pic);
full_name = (TextView) view.findViewById(R.id.full_name_txt);
state = (TextView) view.findViewById(R.id.state_txt);
city = (TextView) view.findViewById(R.id.city_txt);
phone_number = (TextView) view.findViewById(R.id.phone_no_txt);
}
@Override
public String toString() {
return super.toString() + " '" + full_name.getText() + "'";
}
}
android android-recyclerview master-detail
android android-recyclerview master-detail
edited Nov 14 '18 at 12:18
asked Nov 14 '18 at 11:18
user7596908
I'm not sure how SimpleItemRecyclerViewAdapter works, but Isn't the ViewHolder class required in recyclerview adapter where we initialize all the layout items?
– ManishPrajapati
Nov 14 '18 at 11:35
I added the ViewHolder for you
– user7596908
Nov 14 '18 at 12:18
little help please people
– user7596908
Nov 15 '18 at 5:40
Are you able to see the list of data in recyclerView? Only clicking leads to Crash? I see that you are using View recyclerView instead of RecyclerView recyclerView. Also, have you assigned the Layout Manager to recyclerView like recyclerView.setLayoutManager(new LinearLayoutManager(this)); ??
– ManishPrajapati
Nov 15 '18 at 6:13
yes i can see the data but whenever i click on it i'm sending ARG_ITEM_ID with bundle the problem is with this it's always null although i'm giving data for it
– user7596908
Nov 15 '18 at 6:28
add a comment |
I'm not sure how SimpleItemRecyclerViewAdapter works, but Isn't the ViewHolder class required in recyclerview adapter where we initialize all the layout items?
– ManishPrajapati
Nov 14 '18 at 11:35
I added the ViewHolder for you
– user7596908
Nov 14 '18 at 12:18
little help please people
– user7596908
Nov 15 '18 at 5:40
Are you able to see the list of data in recyclerView? Only clicking leads to Crash? I see that you are using View recyclerView instead of RecyclerView recyclerView. Also, have you assigned the Layout Manager to recyclerView like recyclerView.setLayoutManager(new LinearLayoutManager(this)); ??
– ManishPrajapati
Nov 15 '18 at 6:13
yes i can see the data but whenever i click on it i'm sending ARG_ITEM_ID with bundle the problem is with this it's always null although i'm giving data for it
– user7596908
Nov 15 '18 at 6:28
I'm not sure how SimpleItemRecyclerViewAdapter works, but Isn't the ViewHolder class required in recyclerview adapter where we initialize all the layout items?
– ManishPrajapati
Nov 14 '18 at 11:35
I'm not sure how SimpleItemRecyclerViewAdapter works, but Isn't the ViewHolder class required in recyclerview adapter where we initialize all the layout items?
– ManishPrajapati
Nov 14 '18 at 11:35
I added the ViewHolder for you
– user7596908
Nov 14 '18 at 12:18
I added the ViewHolder for you
– user7596908
Nov 14 '18 at 12:18
little help please people
– user7596908
Nov 15 '18 at 5:40
little help please people
– user7596908
Nov 15 '18 at 5:40
Are you able to see the list of data in recyclerView? Only clicking leads to Crash? I see that you are using View recyclerView instead of RecyclerView recyclerView. Also, have you assigned the Layout Manager to recyclerView like recyclerView.setLayoutManager(new LinearLayoutManager(this)); ??
– ManishPrajapati
Nov 15 '18 at 6:13
Are you able to see the list of data in recyclerView? Only clicking leads to Crash? I see that you are using View recyclerView instead of RecyclerView recyclerView. Also, have you assigned the Layout Manager to recyclerView like recyclerView.setLayoutManager(new LinearLayoutManager(this)); ??
– ManishPrajapati
Nov 15 '18 at 6:13
yes i can see the data but whenever i click on it i'm sending ARG_ITEM_ID with bundle the problem is with this it's always null although i'm giving data for it
– user7596908
Nov 15 '18 at 6:28
yes i can see the data but whenever i click on it i'm sending ARG_ITEM_ID with bundle the problem is with this it's always null although i'm giving data for it
– user7596908
Nov 15 '18 at 6:28
add a comment |
1 Answer
1
active
oldest
votes
You are getting null value because you are using mItem from ViewHolder class without initializing, which is null.
Try this:
DummyContent.DummyItem mItem = mValues.get(position); //initialize here, not in ViewHolder class
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, mItem.id); //use without holder
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment)
.commit();
} else {
Context context = v.getContext();
Intent intent = new Intent(context, ItemDetailActivity.class);
Log.e("hello",holder.mItem.full_name);
intent.putExtra(ItemDetailFragment.ARG_ITEM_ID, mItem.id); //same here
context.startActivity(intent);
}
}
});
thank you my friend
– user7596908
Nov 15 '18 at 7:38
and 1 question is the id important in master detail flow or i can do it and add data without id and parse it how ever i want
– user7596908
Nov 15 '18 at 7:58
1
It depends on how you are populating fields in Detail activity. It you are fetching data again from server in Detail, you just have to pass the id to get the particular record. Or you can implement Parcelable in your Model class and pass the whole Model class object to detail activity, where you can fetch the data from the received object without fetching the data from server again.
– ManishPrajapati
Nov 15 '18 at 9:27
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298979%2fmaster-detail-flow-while-clicking-on-recyclerview-i-get-null-exception%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are getting null value because you are using mItem from ViewHolder class without initializing, which is null.
Try this:
DummyContent.DummyItem mItem = mValues.get(position); //initialize here, not in ViewHolder class
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, mItem.id); //use without holder
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment)
.commit();
} else {
Context context = v.getContext();
Intent intent = new Intent(context, ItemDetailActivity.class);
Log.e("hello",holder.mItem.full_name);
intent.putExtra(ItemDetailFragment.ARG_ITEM_ID, mItem.id); //same here
context.startActivity(intent);
}
}
});
thank you my friend
– user7596908
Nov 15 '18 at 7:38
and 1 question is the id important in master detail flow or i can do it and add data without id and parse it how ever i want
– user7596908
Nov 15 '18 at 7:58
1
It depends on how you are populating fields in Detail activity. It you are fetching data again from server in Detail, you just have to pass the id to get the particular record. Or you can implement Parcelable in your Model class and pass the whole Model class object to detail activity, where you can fetch the data from the received object without fetching the data from server again.
– ManishPrajapati
Nov 15 '18 at 9:27
add a comment |
You are getting null value because you are using mItem from ViewHolder class without initializing, which is null.
Try this:
DummyContent.DummyItem mItem = mValues.get(position); //initialize here, not in ViewHolder class
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, mItem.id); //use without holder
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment)
.commit();
} else {
Context context = v.getContext();
Intent intent = new Intent(context, ItemDetailActivity.class);
Log.e("hello",holder.mItem.full_name);
intent.putExtra(ItemDetailFragment.ARG_ITEM_ID, mItem.id); //same here
context.startActivity(intent);
}
}
});
thank you my friend
– user7596908
Nov 15 '18 at 7:38
and 1 question is the id important in master detail flow or i can do it and add data without id and parse it how ever i want
– user7596908
Nov 15 '18 at 7:58
1
It depends on how you are populating fields in Detail activity. It you are fetching data again from server in Detail, you just have to pass the id to get the particular record. Or you can implement Parcelable in your Model class and pass the whole Model class object to detail activity, where you can fetch the data from the received object without fetching the data from server again.
– ManishPrajapati
Nov 15 '18 at 9:27
add a comment |
You are getting null value because you are using mItem from ViewHolder class without initializing, which is null.
Try this:
DummyContent.DummyItem mItem = mValues.get(position); //initialize here, not in ViewHolder class
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, mItem.id); //use without holder
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment)
.commit();
} else {
Context context = v.getContext();
Intent intent = new Intent(context, ItemDetailActivity.class);
Log.e("hello",holder.mItem.full_name);
intent.putExtra(ItemDetailFragment.ARG_ITEM_ID, mItem.id); //same here
context.startActivity(intent);
}
}
});
You are getting null value because you are using mItem from ViewHolder class without initializing, which is null.
Try this:
DummyContent.DummyItem mItem = mValues.get(position); //initialize here, not in ViewHolder class
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, mItem.id); //use without holder
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment)
.commit();
} else {
Context context = v.getContext();
Intent intent = new Intent(context, ItemDetailActivity.class);
Log.e("hello",holder.mItem.full_name);
intent.putExtra(ItemDetailFragment.ARG_ITEM_ID, mItem.id); //same here
context.startActivity(intent);
}
}
});
answered Nov 15 '18 at 7:06
ManishPrajapatiManishPrajapati
221314
221314
thank you my friend
– user7596908
Nov 15 '18 at 7:38
and 1 question is the id important in master detail flow or i can do it and add data without id and parse it how ever i want
– user7596908
Nov 15 '18 at 7:58
1
It depends on how you are populating fields in Detail activity. It you are fetching data again from server in Detail, you just have to pass the id to get the particular record. Or you can implement Parcelable in your Model class and pass the whole Model class object to detail activity, where you can fetch the data from the received object without fetching the data from server again.
– ManishPrajapati
Nov 15 '18 at 9:27
add a comment |
thank you my friend
– user7596908
Nov 15 '18 at 7:38
and 1 question is the id important in master detail flow or i can do it and add data without id and parse it how ever i want
– user7596908
Nov 15 '18 at 7:58
1
It depends on how you are populating fields in Detail activity. It you are fetching data again from server in Detail, you just have to pass the id to get the particular record. Or you can implement Parcelable in your Model class and pass the whole Model class object to detail activity, where you can fetch the data from the received object without fetching the data from server again.
– ManishPrajapati
Nov 15 '18 at 9:27
thank you my friend
– user7596908
Nov 15 '18 at 7:38
thank you my friend
– user7596908
Nov 15 '18 at 7:38
and 1 question is the id important in master detail flow or i can do it and add data without id and parse it how ever i want
– user7596908
Nov 15 '18 at 7:58
and 1 question is the id important in master detail flow or i can do it and add data without id and parse it how ever i want
– user7596908
Nov 15 '18 at 7:58
1
1
It depends on how you are populating fields in Detail activity. It you are fetching data again from server in Detail, you just have to pass the id to get the particular record. Or you can implement Parcelable in your Model class and pass the whole Model class object to detail activity, where you can fetch the data from the received object without fetching the data from server again.
– ManishPrajapati
Nov 15 '18 at 9:27
It depends on how you are populating fields in Detail activity. It you are fetching data again from server in Detail, you just have to pass the id to get the particular record. Or you can implement Parcelable in your Model class and pass the whole Model class object to detail activity, where you can fetch the data from the received object without fetching the data from server again.
– ManishPrajapati
Nov 15 '18 at 9:27
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298979%2fmaster-detail-flow-while-clicking-on-recyclerview-i-get-null-exception%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm not sure how SimpleItemRecyclerViewAdapter works, but Isn't the ViewHolder class required in recyclerview adapter where we initialize all the layout items?
– ManishPrajapati
Nov 14 '18 at 11:35
I added the ViewHolder for you
– user7596908
Nov 14 '18 at 12:18
little help please people
– user7596908
Nov 15 '18 at 5:40
Are you able to see the list of data in recyclerView? Only clicking leads to Crash? I see that you are using View recyclerView instead of RecyclerView recyclerView. Also, have you assigned the Layout Manager to recyclerView like recyclerView.setLayoutManager(new LinearLayoutManager(this)); ??
– ManishPrajapati
Nov 15 '18 at 6:13
yes i can see the data but whenever i click on it i'm sending ARG_ITEM_ID with bundle the problem is with this it's always null although i'm giving data for it
– user7596908
Nov 15 '18 at 6:28