Why the listview inside the fragment is not working? - android

I'm trying to put a list view inside a fragment. I made tries and here is what I got (My app is working, but the list view simply doesn't appear.):
The Fragment I want the list in:
public class Trechos extends Fragment {
ListView list;
#Override
//Método para inflar o layout
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState){
//Variável view to tipo View infla o layout a ser usado
View view = inflater.inflate(R.layout.trechos, container, false);
Classe classe = new Classe(getActivity());
list = (ListView) view.findViewById(R.id.list);
list.setAdapter(classe);
//retorna a variável view
return view;
}
}
The xml for the fragment:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<ListView
android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="adasdasds">
</ListView>
</LinearLayout>
The custom adapter I created to insert the informations:
public class Classe extends ArrayAdapter<String> {
private final Activity context;
private String [] trechos = {"a", "b"};
private int [] posicao = {1,2};
public Classe(Activity context) {
super(context, R.layout.row_layout);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rootView = inflater.inflate(R.layout.row_layout, null, true);
TextView txtPosicao = (TextView) rootView.findViewById(R.id.posicao);
TextView txtTrecho = (TextView) rootView.findViewById(R.id.trechos);
txtPosicao.setText(posicao[position]);
txtTrecho.setText(trechos[position]);
return rootView;
}
}
And the xml for the custom rows:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:textSize="20sp"
android:textStyle="bold"
android:padding="5dp"
android:id="#+id/posicao"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:textStyle="italic"
android:textSize="20sp"
android:padding="5dp"
android:id="#+id/trechos"
android:layout_width="match_parent"
android:layout_height="50dp" />
</TableRow>
</TableLayout>

Related

Android ListView item clicked not working

I don't understand what's going wrong, when clicking an item on the list nothing appends.
my environment is minsdk:21, maxsdk:27, java 8 (openjdk), android-studio 3.1.4, terminal android 6.0
note : using FragmentManager or SupportFragmentManager provides the same result.
the fragment list layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
>
<ListView
android:id="#+id/list"
android:dividerHeight="1sp"
android:divider="#color/blue_serenity_transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
List row layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:useDefaultMargins="true"
android:background="#color/blue_serenity_transparent"
android:columnCount="3"
android:rowCount="2"
>
<ImageView
android:id="#+id/notif_type"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_column="0"
android:layout_row="0"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_column="1"
android:layout_row="0"
android:textSize="18dp"
android:textColor="#color/black"
/>
<TextView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="1"
android:layout_column="1"
/>
</GridLayout>
</LinearLayout>
list setup
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
List<NotificationBean> data = new NotificationBean().list(getActivity().getBaseContext());
View v = inflater.inflate(R.layout.fragment_notifications, container, false);
list = (ListView) v.findViewById(R.id.list);
listAdapter = new Adapter(getActivity().getBaseContext(), data);
list.setAdapter(listAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e(TAG, "row clicked");
}
});
return v;
}
List adapter
class NotificationsAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater = null;
private List<NotificationBean> notifications;
public NotificationsAdapter(Context context, List<NotificationBean> notifications){
this.context = context;
this.notifications = notifications;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return notifications.size();
}
#Override
public Object getItem(int position) {
return notifications.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null)
v = inflater.inflate(R.layout.fragment_notifications_row, null);
NotificationBean n = notifications.get(position);
GridLayout layout = (GridLayout) v.findViewById(R.id.row_layout);
if (dark) layout.setBackgroundColor(getResources().getColor(R.color.blue_serenity_transparent));
else layout.setBackgroundColor(getResources().getColor(R.color.white));
dark = !dark;
ImageView notifType = (ImageView) v.findViewById(R.id.notif_type);
notifType.setImageResource(n.getIcon());
TextView title = (TextView) v.findViewById(R.id.title);
title.setText(n.getTitle());
TextView date = (TextView) v.findViewById(R.id.date);
date.setText(Constants.DTF.format(n.getDate()));
return v;
}
}

ListView is not displayed in fragment

I implemented my own navigation drawer that comes from the left screen in my android application. The problem is that the ListView it is not visible and I can't figure out why.
This is the method onCreateView of the fragment where the list is (first I initialize the header of the layout and then the list with my custom adapter):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View firstAccessView = inflater.inflate(R.layout.fragment_navigation_drawer, null);
this.profileImageDrawer = (ImageView) firstAccessView.findViewById(R.id.imageViewProfileDrawer);
RoundImage roundedImage = new RoundImage(BitmapFactory.decodeResource(getResources(), R.mipmap.iconuseranonymous));
//this.profileImageDrawer.setImageDrawable(roundedImage);
Picasso.with((Activity) getActivity()).load("https://graph.facebook.com/" + Profile.getCurrentProfile().getId() + "/picture?height=105&width=105")
.placeholder(roundedImage)
.transform(new CircleTransform()).fit().centerCrop().into(this.profileImageDrawer);
this.nameSurnameDrawer = (TextView) firstAccessView.findViewById(R.id.textViewDrawner);
this.nameSurnameDrawer.setText(Profile.getCurrentProfile().getFirstName() + " " + Profile.getCurrentProfile().getLastName());
List<String> example = new ArrayList<String>();
example.add("Profile");
example.add("Ask");
example.add("Logout");
this.adapter = new DrawerAdapter(getActivity(), example);
list = (ListView) firstAccessView.findViewById(R.id.listDrawer);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
list.setItemChecked(mCurrentSelectedPosition,true);
return firstAccessView;
}
This is the code of my custom adapter
private Activity context;
private List<String> list;
private View view;
private TextView textView;
public DrawerAdapter(Activity context, List<String> list) {
super(context, R.layout.drawerlist_layout);
this.context=context;
this.list = list;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.drawerlist_layout, null);
textView = (TextView) view.findViewById(R.id.sectionDrawer);
textView.setText(this.list.get(position));
textView.setTextColor(Color.BLACK);
return view;
}
and finally this is the layout of my fragment: at the top I have an header with an image and a TextView and then I have my custom ListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="0.40"
android:background="#ffb200">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="#+id/imageViewProfileDrawer"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="20px" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Medium Text"
android:id="#+id/textViewDrawner"
android:layout_gravity="right|bottom"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:textIsSelectable="true" />
</FrameLayout>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="262dp" android:choiceMode="singleChoice"
android:divider="#cecece" android:dividerHeight="1dp"
android:background="#FFFFFF"
tools:context="com.bellantoni.chetta.lieme.NavigationDrawerFragment"
android:id="#+id/listDrawer"
android:drawSelectorOnTop="false"
android:smoothScrollbar="true"
android:longClickable="false"
android:nestedScrollingEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="true"
android:paddingLeft="10dp"
android:stackFromBottom="false"
android:scrollingCache="false"
android:paddingRight="10dp"
android:textFilterEnabled="false"
android:theme="#style/Base.Widget.AppCompat.ActionMode"
android:textAlignment="center"
android:transitionGroup="true"
android:layout_weight="0.60" />
I found my error in this line of code:
super(context, R.layout.drawerlist_layout);
where I missed the third parameter which in this case is list so it should be
super(context, R.layout.drawerlist_layout,list);
Set both FameLayout and ListView layout_width to 0dp instead of match_parent if you want to use layout_weight.

ListView from ArrayAdapter

I've Created a List view with ArrayAdapter and when i run it , its showing me an empty activity in the runtime and i don't know the wrong:
here is the MainActivity and the ArrayAdapter Classes
public class MainActivity extends Activity {
String [] title ;
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
title = getResources().getStringArray(R.array.titles);
list.setAdapter(new MyAdapter(MainActivity.this, title));
}
}
class MyAdapter extends ArrayAdapter{
String [] title;
Context context;
public MyAdapter(Context context, String[] title) {
super(context,R.layout.single_row,R.id.img);
this.title = title;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
ImageView iv;
View row = convertView;
if(row==null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_row, parent, false);
}
tv = (TextView) row.findViewById(R.id.title);
tv.setText(title[position]);
iv = (ImageView) row.findViewById(R.id.img);
iv.setImageResource(R.drawable.arrow);
return row;
}
}
activity_main.xml that contain the ListView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
single_row.xml that contains the elements of the listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/img"
android:src="#drawable/arrow"
android:layout_margin="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textSize="#dimen/abc_action_bar_stacked_max_height"
android:id="#+id/title"
android:layout_gravity="center_vertical" />
</LinearLayout>
</LinearLayout>
Change this line of code in your adapter.
LayoutInflater inflater = LayoutInflater.from(context);

Populate list of custom view using ListFragment

I am trying to show elements in a list view using Fragments. I created my custom view as follow
graphical representation of list_row.xml
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="#+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:contentDescription="#string/image_desc"/>
</LinearLayout>
<!-- Menu name -->
<TextView
android:id="#+id/menu_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text="#string/menu_name"
android:textColor="#040404"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
<!-- Description -->
<TextView
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/thumbnail"
android:layout_below="#id/menu_name"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:text="#string/description"
android:textColor="#343434"
android:textSize="10sp"
tools:ignore="SmallSp" />
<!-- Price -->
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#id/menu_name"
android:layout_marginRight="5dip"
android:gravity="right"
android:text="#string/price"
android:textColor="#10bcc9"
android:textSize="10sp"
android:textStyle="bold"
tools:ignore="SmallSp" />
</RelativeLayout>
In fragment.xml file, I simply put a list view inside a linear layout
fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
I have an array of menu objects, that has the necessary fields to populate list_row.xml like menu name, description, price and image. I couldn't find a way to fill fragment.xml's listView with the list_row elements. Any help or idea would be appreciated.
PS: I think in fragment.xml instead of android:id="#+id/listView1" field, I have to write android:id="#id/list" since I'm using ListFragment
I solved my problem (as system32 suggests on comment) creating a CustomArrayAdapter class, and setting it as the adapter for my listView.
First I changed android:id="#+id/listView1" to android:id="#android:id/list" in fragment.xml.
CustomArrayAdapter.java
public class CustomArrayAdapter extends ArrayAdapter<Menu> {
Context context;
public CustomArrayAdapter(Context context, int textViewResourceId, List<Menu> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtMenuName;
TextView txtMenuDesc;
TextView txtPrice;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Menu rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.txtMenuName = (TextView) convertView.findViewById(R.id.menu_name);
holder.txtMenuDesc = (TextView) convertView.findViewById(R.id.description);
holder.txtPrice = (TextView) convertView.findViewById(R.id.price);
holder.imageView = (ImageView) convertView.findViewById(R.id.list_image);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtMenuDesc.setText(rowItem.getDescription());
holder.txtMenuName.setText(rowItem.getName());
holder.txtPrice.setText(String.valueOf(rowItem.getPrice()) + " TL");
//holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
}
Then I use it in my Fragment class
public static class Fragment extends ListFragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
private ListView listView;
private ArrayList<Menu> menuItems;
private CustomArrayAdapter mAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment,
container, false);
listView = (ListView) rootView.findViewById(android.R.id.list);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
int num = getArguments().getInt(ARG_SECTION_NUMBER);
// GlobalList is a class that holds global variables, arrays etc
// getMenuCategories returns global arraylist which is initialized in GlobalList class
menuItems = GlobalList.getMenuCategories().get(num).getMenu();
mAdapter = new CustomArrayAdapter(getActivity(), android.R.id.list, menuItems);
listView.setAdapter(mAdapter);
}
}

How to put a ListView in a Fragment

I had a good ListView, but now I added Fragments, now my Code doesn't work in the onCreateMenu...
How to make my ListView back?
So here's my code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView;
Bundle b = getArguments();
int Nr = b.getInt(ARG_SECTION_NUMBER);
if (Nr == 0) {
myFragmentView = inflater.inflate(R.layout.layoutexample, container, false);
} else {
myFragmentView = inflater.inflate(R.layout.another_layout_example, container, false);
}
return myFragmentView;
}
I don't understand, where to put my code (for example if I want a ListView)!
Can I use something similar to the inflater?
Please help, as a beginner I only understand half of the information, Google Developers gives me...
This is my ListView:
public class Home extends ListActivity {
ListView listView;
static final String[] Liste = {"a", "b", "c"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("CalcPlus");
listView = getListView();
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.text, Liste) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
String item = getItem(position);
TextView subTitleView = (TextView) view.findViewById(R.id.subtitle);
subTitleView.setText("Subtitle of " + item);
return view;
}
});
}
}
That's the list_item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAllCaps="false"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAllCaps="false"
android:textSize="16dp"
android:textStyle="normal" />
</LinearLayout>
Here's the example.xml (they both look the same):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
After having inflated your view, you can add :
ListView listView = myFragmentView.findViewById(android.R.id.list);
listView.setAdapter(....);
Edit : I didn't know the id you set to your ListView. Replace android.R.id.list by R.id.listView1

Categories

Resources