- Forum-Beiträge: 180
09.03.2014, 20:46:23 via Website
09.03.2014 20:46:23 via Website
Ich habe so einen Tabhost hier wo man so switchen kann zwischen den Titeln
Ich habe aber das problem ich weis nicht wo ich die java datei implementieren soll dass die ListView ausgeführt wird...
Hier der Code
MainActivity:
2
3
4
5
6import android.app.ActionBar;
7import android.graphics.Color;
8import android.graphics.drawable.ColorDrawable;
9import android.os.Bundle;
10import android.support.v4.app.Fragment;
11import android.support.v4.app.FragmentActivity;
12import android.support.v4.app.FragmentManager;
13import android.support.v4.app.FragmentPagerAdapter;
14
15import android.support.v4.view.ViewPager;
16
17import android.view.LayoutInflater;
18import android.view.Menu;
19import android.view.MenuItem;
20
21import android.view.View;
22import android.view.ViewGroup;
23import android.widget.TextView;
24
25public class MainActivity extends FragmentActivity {
26
27 /**
28 * The {@link android.support.v4.view.PagerAdapter} that will provide
29 * fragments for each of the sections. We use a
30 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
31 * will keep every loaded fragment in memory. If this becomes too memory
32 * intensive, it may be best to switch to a
33 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
34 */
35 SectionsPagerAdapter mSectionsPagerAdapter;
36
37 /**
38 * The {@link ViewPager} that will host the section contents.
39 */
40 ViewPager mViewPager;
41
42 @Override
43 protected void onCreate(Bundle savedInstanceState) {
44 super.onCreate(savedInstanceState);
45 setContentView(R.layout.activity_main);
46
47 ActionBar ab = getActionBar();
48 ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#33b5e5"));
49 ab.setBackgroundDrawable(colorDrawable);
50
51
52 // Create the adapter that will return a fragment for each of the three
53 // primary sections of the app.
54 mSectionsPagerAdapter = new SectionsPagerAdapter(
55 getSupportFragmentManager());
56
57 // Set up the ViewPager with the sections adapter.
58 mViewPager = (ViewPager) findViewById(R.id.pager);
59 mViewPager.setAdapter(mSectionsPagerAdapter);
60
61 }
62
63 @Override
64 public boolean onOptionsItemSelected(MenuItem item){
65 super.onOptionsItemSelected(item);
66 switch(item.getItemId()){
67 case R.id.about:
68 aboutMenuItem();
69
70 break;
71 }
72 return true;
73 }
74
75 private void aboutMenuItem(){
76 setContentView(R.layout.about);
77
78 }
79
80
81
82 @Override
83 public boolean onCreateOptionsMenu(Menu menu) {
84 // Inflate the menu; this adds items to the action bar if it is present.
85 getMenuInflater().inflate(R.menu.main, menu);
86 return true;
87 }
88
89 /**
90 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
91 * one of the sections/tabs/pages.
92 */
93 public class SectionsPagerAdapter extends FragmentPagerAdapter {
94
95 public SectionsPagerAdapter(FragmentManager fm) {
96 super(fm);
97 }
98
99 @Override
100 public Fragment getItem(int position) {
101 // getItem is called to instantiate the fragment for the given page.
102 // Return a DummySectionFragment (defined as a static inner class
103 // below) with the page number as its lone argument.
104 Fragment fragment = new DummySectionFragment();
105 Bundle args = new Bundle();
106 args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
107 fragment.setArguments(args);
108 return fragment;
109 }
110
111 @Override
112 public int getCount() {
113 // Show 3 total pages.
114 return 3;
115 }
116
117 @Override
118 public CharSequence getPageTitle(int position) {
119 Locale l = Locale.getDefault();
120 switch (position) {
121 case 0:
122 return getString(R.string.title_section1).toUpperCase(l);
123 case 1:
124 return getString(R.string.title_section2).toUpperCase(l);
125 case 2:
126 return getString(R.string.title_section3).toUpperCase(l);
127 }
128 return null;
129 }
130 }
131
132 /**
133 * A dummy fragment representing a section of the app, but that simply
134 * displays dummy text.
135 */
136 public static class DummySectionFragment extends Fragment {
137 /**
138 * The fragment argument representing the section number for this
139 * fragment.
140 */
141 public static final String ARG_SECTION_NUMBER = "section_number";
142
143 public DummySectionFragment() {
144 }
145
146 @Override
147 public View onCreateView(LayoutInflater inflater, ViewGroup container,
148 Bundle savedInstanceState) {
149 View rootView = inflater.inflate(R.layout.fragment_main_dummy,
150 container, false);
151 TextView dummyTextView = (TextView) rootView
152 .findViewById(R.id.section_label);
153 dummyTextView.setText(Integer.toString(getArguments().getInt(
154 ARG_SECTION_NUMBER)));
155 return rootView;
156 }
157 }
158 public void button (View view) {
159 setContentView(R.layout.activity_main);
160 return;
161
162
163
164
165
166 }
167}
TestLiestView:
2import android.content.Context;
3import android.util.AttributeSet;
4import android.view.View;
5import android.widget.AdapterView;
6import android.widget.ArrayAdapter;
7import android.widget.ListView;
8
9public class TestListView extends ListView
10{
11 private ArrayAdapter<String> test;
12 String[] testItems = {"Marcel P.", "Chantal H.", "Christian P.","Melanie T.", "Sarah L.", "Nicola S.","Patricia C.", "Katarina L.", "Lukas R.","Karam O.",
13 "Johannes B.", "Christina S.","Maida B.", "Carina W.", "Lukas Ro.","Michael G.", "David K.", "Sabrina W."};
14 //If built programmatically
15 public TestListView(Context context)
16 {
17 super(context);
18 init();
19 }
20 //This example uses this method since being built from XML
21 public TestListView(Context context, AttributeSet attrs)
22 {
23 super(context, attrs);
24 init();
25 }
26
27 //Build from XML layout
28 public TestListView(Context context, AttributeSet attrs, int defStyle)
29 {
30 super(context, attrs, defStyle);
31 init();
32 }
33
34 public void init()
35 {
36 test = new ArrayAdapter<String>(getContext(),R.layout.tabitems, R.id.label , testItems);
37 setAdapter(test);
38 setOnItemClickListener(new ListSelection());
39 }
40
41 private class ListSelection implements OnItemClickListener
42 {
43
44 @Override
45 public void onItemClick(AdapterView<?> parent, View view, int position,
46 long id)
47 {
48 AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
49 builder.setMessage("Bild bald verfügbar!");
50 builder.setPositiveButton("OK", null);
51 builder.show();
52 }
53
54 }
55}
tabitem xml:
2
3 android:layout_width="match_parent"
4 android:layout_height="wrap_content"
5 android:orientation="horizontal" >
6
7 <ImageView
8 android:id="@+id/icon"
9 android:layout_width="60dp"
10 android:layout_height="60dp"
11 android:layout_marginLeft="4dp"
12 android:layout_marginRight="10dp"
13 android:layout_marginTop="4dp"
14 android:src="@drawable/ic_launcher" />
15
16 <TextView
17 android:id="@+id/label"
18 android:layout_width="wrap_content"
19 android:layout_height="wrap_content"
20 android:layout_marginTop="18dp"
21 android:text="@+id/label"
22 android:textSize="20sp" />
23</LinearLayout>
activity main xml:
2 android:layout_width="match_parent"
3 android:layout_height="match_parent"
4 tools:context=".MainActivity" >
5
6 <!--
7 This title strip will display the currently visible page title, as well as the page
8 titles for adjacent pages.
9 -->
10
11 <android. support. v4.view. PagerTitleStrip
12 android:id="@+id/pager_title_strip"
13 android:layout_width="match_parent"
14 android:layout_height="50dp"
15 android:layout_gravity="top"
16 android:background="#99cc00"
17 android:paddingBottom="4dp"
18 android:paddingTop="4dp"
19 android:textColor="#fff" />
20
21</android.support.v4.view.ViewPager>
und noch fragment main dummy xml:
2 android:layout_height="match_parent"
3 android:paddingBottom="@dimen/activity_vertical_margin"
4 android:paddingLeft="@dimen/activity_horizontal_margin"
5 android:paddingRight="@dimen/activity_horizontal_margin"
6 android:paddingTop="@dimen/activity_vertical_margin"
7 tools:context=".MainActivity$DummySectionFragment" >
8
9 <TextView
10 android:id="@+id/section_label"
11 android:layout_width="wrap_content"
12 android:layout_height="wrap_content" />
13
14</RelativeLayout>
so dass ist mal das ganze projekt und ich möchte wissen wo man dass einfügen muss dass der 1. tab mit der listview gefüllt wird
danke für jede hilfe
MFG Lukas R.
Programmieren ist nicht nur eine Wissenschaft, sondern auch ein Lifestyle!
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.