I am implementing array list adopter uisng "simple_list_item_2" that is inbuilt but I get error. it says " getview from fragment cannot be applied" and it also cannot resolve "get" symbol from get.(position)
public class AboutFragment extends Fragment {
ListView listView2;
String[] items = {"Friendly Map", "Inc"};
public AboutFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_about, container, false);
listView2 = (ListView)v.findViewById(R.id.listView2);
ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_2, items);
listView2.setAdapter(arrayAdapter);
return v;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View view = super.getView(position, convertView, parent);
String[] entry = listView2.get(position);
return view;
}
}
Try this.
public class AboutFragment extends Fragment {
ListView listView2;
String[] items = {"Friendly Map", "Inc"};
public AboutFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_about, container, false);
listView2 = (ListView) v.findViewById(R.id.listView2);
ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_2, items);
listView2.setAdapter(arrayAdapter);
return v;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View view = convertView;
String entry = listView2.get(position);
return view;
}
}
Related
I am trying to show a listview in a fragment layout; for that i have used custom adapter.Note that on a button click, fragment is changed. But on that button click application crashes, I don't know what is actual problem please help.
This is my Fragment Class:
public class Fragment1 extends Fragment {
public void Fragment1()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_fragment1, container, false);
//return inflater.inflate(R.layout.fragment_fragment1, container, false);
ListView listView = (ListView)rootView.findViewById(R.id.listView);
CustomAdapter customAdapter = new CustomAdapter();
listView.setAdapter(customAdapter);
return rootView;
}
class CustomAdapter extends BaseAdapter
{
int img_arr[] = {R.drawable.emmastone,
R.drawable.chloegracemoretz,
R.drawable.salmankhan,
R.drawable.emilia,
R.drawable.kitharington,
R.drawable.scarlettjohansson,
R.drawable.sophie,
R.drawable.deepika,
R.drawable.leonardodicaprio};
String name_arr[] = {"Emma", "Chloe", "Salman", "Emilia", "Kit", "Scarlet", "Sophie", "Deepika", "Leonardo" };
String msg_arr[] = {"Free at 8pm?", "I miss you <3", "Mota ho raha hun kia karon?", "Not in mood", "Danny or yagrit?", "Where are you?", "got engaged <3", "Give my money", "50 is enough" };
#Override
public int getCount() {
return img_arr.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.chats,null);
ImageView img = (ImageView)convertView.findViewById(R.id.imageView);
TextView name = (TextView)convertView.findViewById(R.id.textView_name);
TextView msg = (TextView)convertView.findViewById(R.id.textView_msg);
img.setImageResource(img_arr[position]);
name.setText(name_arr[position]);
msg.setText(msg_arr[position]);
return convertView;
}
}
}
This is in logcat:
0-21 23:52:21.581 3040-3040/com.example.farhan.a13_f_8310_lab_10_oct_fragments E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.farhan.a13_f_8310_lab_10_oct_fragments, PID: 3040
java.lang.NoSuchMethodError: No virtual method getLayoutInflater()Landroid/view/LayoutInflater; in class
Lcom/example/farhan/a13_f_8310_lab_10_oct_fragments/Fragment1; or its super classes (declaration of 'com.example.farhan.a13_f_8310_lab_10_oct_fragments.Fragment1' appears in /data/app/com.example.farhan.a13_f_8310_lab_10_oct_fragments-1/split_lib_slice_1_apk.apk)
at com.example.farhan.a13_f_8310_lab_10_oct_fragments.Fragment1$CustomAdapter.getView(Fragment1.java:91)
Here is my code which is not working
I'm getting data from server using `AsyncTask` and set data to the `listview` using `baseadapter` but using `convertview.setonclicklistner` working in adapter class and below code contains in `Fragmentclass(import android.support.v4.app.Fragment;)` so I think it is not problematic
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view= inflater.inflate(R.layout.fragment_restaurants, container, false);
restaurant_list = (ListView)view.findViewById(R.id.restatrant_lv);
no_restaturants=(TextView)view.findViewById(R.id.no_restaturants);
// listening to single list item on click
restaurant_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.e("temp","temppppp");
}
});
//set layout slide listener
rowItems = new ArrayList<>();
history_list = new ArrayList<HashMap<String, String>>();
list_adapter = new ViewAdapter(getActivity(),rowItems);
restaurant_list.setAdapter(list_adapter);
}
What is the solution for this ?
add id to your parent layout for custom_list_item.xml
holder.parentLayout.setOnClickListener(new OnClickItem(position));
private class OnClickItem implements View.OnClickListener{
private int mPostion;
public OnClickItem(int position){
mPostion = position;
}
#Override
public void onClick(View v) {
YourActivity yourActivity = (YourActivity) mContext;
yourActivity.onItemClicked(mPostion);
}
}
android:descendantFocusability="blocksDescendants"
If any ListView contains focusable or clickable view then OnItemClickListener won't work. In that case we need to use this.
Try to set your setOnClickListener after setting the adapter:
Like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view= inflater.inflate(R.layout.fragment_restaurants, container, false);
restaurant_list = (ListView)view.findViewById(R.id.restatrant_lv);
no_restaturants=(TextView)view.findViewById(R.id.no_restaturants);
//set layout slide listener
rowItems = new ArrayList<>();
history_list = new ArrayList<HashMap<String, String>>();
list_adapter = new ViewAdapter(getActivity(),rowItems);
restaurant_list.setAdapter(list_adapter);
// listening to single list item on click
restaurant_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.e("temp","temppppp");
}
});
}
I have two fragments:AllItemsFragment, CreateItemDialogFragment
The AllItemsFragment displays a list of items in the sqlite,CreateItemDialogFragment is used to create the item to the SQLite.
Now I have a question is after I creating a item in createItemDialogFragment,the dialog dismiss, how to update the display of the items in AllItemsFragment
In AllItemsFragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(LIFETAG, "onCreateView");
View view = inflater.inflate(R.layout.fragment_all_items, container, false);
ButterKnife.bind(this, view);
tvTitle.setText("All Items");
DbHandler dbHandler = new DbHandler(getActivity(),null,null,1);
ArrayList<Item> items = dbHandler.getAllItems();
AllItemsAdapter adapter = new AllItemsAdapter(getActivity(),items);
lvItems.setAdapter(adapter);
return view;
}
In CreateItemDialogFragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
View view =inflater.inflate(R.layout.fragment_create_item_dialog,null);
ButterKnife.bind(this, view);
itemPrice.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
return view;
}
#OnClick(R.id.clear)
void clearDialog(){
dismiss();
}
//create item
#OnClick(R.id.save)
void saveItem() {
if(!itemName.getText().equals(null)||!itemPrice.getText().equals(null)){
String name = itemName.getText().toString();
String priceStr = itemPrice.getText().toString();
Double price = Double.parseDouble(priceStr);
Item item = new Item(name,price);
DbHandler dbHandler = new DbHandler(getActivity(),null,null,1);
dbHandler.addItems(item);
dismiss();
}
}
Try to update the data on onResume callback of the AllItemsFragment.
#Override
public void onResume(){
super.onResume();
// update data over here.
}
Don't know why the onItemClick event not working...
public class FragmentList extends Fragment {
View rootView;
ListView list;
public FragmentList (){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_list, container, false);
list=(ListView)rootView.findViewById(R.id.list);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Log.i("test", String.valueOf(position));
}
});
return rootView;
}
}
Anyone has idea on this. thanks so much
If any row item of list contains focusable or clickable view then OnItemClickListener won't work.
The row item must have a param like android:descendantFocusability="blocksDescendants".
Click here for more information.
How do i set the adapter in Spinner for the following bunch of code? I should set the adapter under
//set adapter for spinner here
public class ListViewFragment extends Fragment implements OnItemSelectedListener{
public ListViewFragment(){}
Spinner mspinner; //declare spinner globally
ArrayAdapter<String> adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_listview, container, false);
mspinner = (Spinner) view.findViewById(R.id.spinner1);
//set adapter for spinner here
return rootView;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
//do something when particular item is selected from spinner
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
Try this , Hope it works
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_listview, container, false);
mspinner = (Spinner) view.findViewById(R.id.spinner1);
//set adapter for spinner here
// you need to create a array which one is added to spinner
String [] name = {"A","B","C"};
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,name);
// you can use getActivity() or this
mspinner.setAdapter(adapter);
return rootView;
}
Thank you
//here are the contents its the same as you do in a listview it can be a ArrayList or a array
String[] content={"A","B","C"};
adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item,content);
mspinner.setAdapter(adapter);
try this hope this will work for you!!