EditText inside a ListView on a onCreateView - android

I have a List view with two EditText in and want to modify the problem is that I'm working it from a fregment and I only onCreateView to work
could help me?
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
rootView = inflater.inflate(R.layout.calcu, container, false);
c = getActivity().getApplicationContext();
lista = (ListView) rootView.findViewById(R.id.listView1);
lista.setAdapter(adapter);
cantidad = (EditText) rootView.findViewById(R.id.cantidad);
precio = (EditText) rootView.findViewById(R.id.precio);
id = (TextView) rootView.findViewById(R.id.id);
// how to get the position of the selected EditText to access him
position( X ) cantidad.getText().toString();
position( X ) precio.getText().toString();
return rootView;
}

Related

Access Fragment Layout Objects From Java Not Working

I'm trying to access from my code to listview and a textview, I read a lot of posts on the internet but I didn't find anything that works for me.
I have a blank fragment like this:
public class MyFrag extends Fragment {
//My Code
List<String> Names = new ArrayList<String>();
List<Double> Prices = new ArrayList<Double>();
List<String> Dates = new ArrayList<String>();
List<String> Hours = new ArrayList<String>();
List<String> iDs = new ArrayList<String>();
Double TotalMy = 0.00;
SimpleAdapter adapter;
public MyFrag() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//RelativeLayout rootView = (RelativeLayout) inflater.inflate(R.layout.fragment_my_frag, container, false);
//View view = inflater.inflate(R.layout.fragment_my_frag, container, false);
LayoutInflater lf = getActivity().getLayoutInflater();
View view = lf.inflate(R.layout.fragment_my_frag, container, false);
ListView listview = (ListView) getView().findViewById(R.id.list);
TextView textTotalMy = (TextView) getView().findViewById(R.id.textTotalMy1);
return view;
}
But when I open the fragment the application crashes.
Please help me
I solved it by creating the same Relative Layout programmatically

App crash with Null Object Reference [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am using a AutoCompleteTextView in a Fragment...
but when I use a method which refreshes the Suggestions for AutoCompleteTextView, the App crashes with Null Object Reference
here's the method -
private ArrayList<String> sugs = new ArrayList<String>();
public void refreshSugs()
{
((AutoCompleteTextView) getView().findViewById(R.id.editText1)).setAdapter(new ArrayAdapter(getActivity(), 17367050, this.sugs));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
//Inflating the Layout
View rootView = inflater.inflate(R.layout.home_fragment, container, false);
Button buttongo = (Button) rootView.findViewById(R.id.button1);
EditText et1 = (EditText) rootView.findViewById(R.id.editText1);
Button buttonsave = (Button) rootView.findViewById(R.id.button2);
bar = (ProgressWheel) rootView.findViewById(R.id.progressBar1);
i = (ImageView) rootView.findViewById(R.id.imageView1);
background2 = (TextView) rootView.findViewById(R.id.textView1);
background3 = (TextView) rootView.findViewById(R.id.textView2);
readSugs();
refreshSugs();
return rootView;
}
Might be your getView() is null.
private ArrayList<String> sugs = new ArrayList<String>();
private View rootView;
public void refreshSugs() {
((AutoCompleteTextView) rootView.findViewById(R.id.editText1)).setAdapter(new ArrayAdapter(getActivity(), 17367050, this.sugs));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
//Inflating the Layout
rootView = inflater.inflate(R.layout.home_fragment, container, false);
..... your code
return rootView;
}
private ArrayList<String> sugs = new ArrayList<String>();
private View rootView;
public void refreshSugs(View rootView) {
((AutoCompleteTextView) rootView.findViewById(R.id.editText1)).setAdapter(new ArrayAdapter(getActivity(), 17367050, this.sugs));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
//Inflating the Layout
rootView = inflater.inflate(R.layout.home_fragment, container, false);
..... your code...........
refreshSugs0(rootView);
return rootView;
}

Can't set text in fragments TextView

I have strange problem with setText() function. I want to display listview item details when i click on specific item. I want to display this details in 3 tabs so I use fragments. This is my code from one of them.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Intent intent = getActivity().getIntent();
int position = intent.getExtras().getInt("Position");
View rootView = inflater.inflate(R.layout.album_tab1, container, false);
Log.e("Position", String.valueOf(position));
Log.e("Value: ", ParseJSONDetail.overview[position]);
final TextView title = (TextView) rootView.findViewById(R.id.album_title);
final TextView overview = (TextView) rootView.findViewById(R.id.album_overview);
final TextView band = (TextView) rootView.findViewById(R.id.album_band);
final ImageView okladka = (ImageView) rootView.findViewById(R.id.album_okladka);
overview.setText(ParseJSONDetail.overview[position]);
imgload.getInstance().displayImage(ParseJSONDetail.image[position], okladka);
band.setText(ParseJSONDetail.band[position]);
title.setText(ParseJSONDetail.title[position]);
return inflater.inflate(R.layout.album_tab1, container, false);
}
Strange is that in Log i can display this data what i want.
Any ideas what's wrong with this code?
replace
return inflater.inflate(R.layout.album_tab1, container, false);
with
return rootView;

Update textview in fragment

This might be a very simple problem, but I can't seem to get it to work.
I'm trying to update a textview in a Fragment on "OnCreateView"
My code looks like this
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_display_product, container, false);
txtAge = (TextView)v.findViewById(R.id.txtAge);
txtCountry = (TextView)v.findViewById(R.id.txtCountry);
txtName = (TextView)v.findViewById(R.id.txtName);
txtAge.setText("Hallo world");
return inflater.inflate(R.layout.fragment_display_product, container,false);
}
The text in textview txtAge never get set and I can't seem to find a reason.
I'm new to android so sorry if this is a stupid question.
/Birger
Do it like this-
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_display_product, null);
txtAge = (TextView)v.findViewById(R.id.txtAge);
txtCountry = (TextView)v.findViewById(R.id.txtCountry);
txtName = (TextView)v.findViewById(R.id.txtName);
txtAge.setText("Hallo world");
return v;
}
Return the same view which you have inflated already.

Setting spinners in fragment

I am trying to set the values for my spinner from a string Array in one of my fragments in the onCreateView in my public final class Manual extends Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.manual, container, false);
String [] values =
{"Time at Residence","Under 6 months","6-12 months","1-2 years","2-4 years","4-8 years","8-15 years","Over 15 years",};
Spinner spinner = (Spinner) v.findViewById(R.id.spinner1);
ArrayAdapter<String> LTRadapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values);
LTRadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(LTRadapter);
return inflater.inflate(R.layout.manual, container, false);
}
I get no errors with my code, however it doesn't set the spinner. The spinner remains blank with no values. Any ideas on why my code isn't setting the spinner?
The problem was I was returning a new view, not the one I set. I had to return v; and it worked fine.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.manual, container, false);
String [] values =
{"Time at Residence","Under 6 months","6-12 months","1-2 years","2-4 years","4-8 years","8-15 years","Over 15 years",};
Spinner spinner = (Spinner) v.findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adapter);
return v;
}

Categories

Resources