Reputation: 1
Hello Im new to android studio and i probably made a silly mistake which i cant find. So basicaly i have made a tabbed activity with all the days and i want to make a list (I wanna make a custom schedule app which will have all the activities i do daily).My ListView doesn't show :(. What have i done wrong?
EditActivity class code : https://hastebin.com/amajusutid.java
package com.timemanagement.restrictedpower.timemanagement;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class EditActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_edit, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static String day(int id){
switch (id) {
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thirsday";
case 5:
return "Friday";
case 6:
return "Saturday";
case 7:
return "Sunday";
}
return null;
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_edit, container, false);
ListView lv = (ListView) v.findViewById(R.id.Activities);
List<String> fruits_list = new ArrayList<String>();
fruits_list.add("Apple");
fruits_list.add("Strawberry");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, fruits_list);
lv.setAdapter(arrayAdapter);
lv.invalidateViews();
View rootView = inflater.inflate(R.layout.fragment_edit, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.Day);
int id = getArguments().getInt(ARG_SECTION_NUMBER);
String day = day(getArguments().getInt(ARG_SECTION_NUMBER));
textView.setText(day);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 7;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
case 3:
return "SECTION 4";
case 4:
return "SECTION 5";
case 5:
return "SECTION 6";
case 6:
return "SECTION 7";
}
return null;
}
}
}
Xml code: https://hastebin.com/yapepideme.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1234">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Activities"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
style="@android:style/Widget.DeviceDefault.ExpandableListView" />
</LinearLayout>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_height="60dp"
android:layout_width="match_parent">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="5mm"
android:id="@+id/Day" />
<TextView
android:id="@+id/section_label"
android:layout_width="match_parent"
android:layout_height="0dp"
android:textColor="#FFCC91"
android:textSize="6mm"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Upvotes: 0
Views: 2704
Reputation: 1319
I have solved your problem, you have to change the height of the linear layout because it is hidden when the app bar layout is applied.Your listview should placed below the app bar layout.I have changed android:layout_height="300dp" , android:layout_marginTop="100dp" in your linear layout.Now your listview is appearing.
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1234">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="100dp"
android:layout_weight="1">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Activities"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
style="@android:style/Widget.DeviceDefault.ExpandableListView" />
</LinearLayout>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_height="60dp"
android:layout_width="match_parent">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="5mm"
android:id="@+id/Day" />
<TextView
android:id="@+id/section_label"
android:layout_width="match_parent"
android:layout_height="0dp"
android:textColor="#FFCC91"
android:textSize="6mm"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</android.support.design.widget.AppBarLayout>
Upvotes: 1