Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Created June 18, 2012 06:22
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daichan4649/2947119 to your computer and use it in GitHub Desktop.
Save daichan4649/2947119 to your computer and use it in GitHub Desktop.
ListFragment sample
<?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" >
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text" />
</LinearLayout>
package test.fragment.list;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class ListTestActivity extends Activity {
private static final String TAG_LIST = "list";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fragment fragment = getFragmentManager().findFragmentByTag(TAG_LIST);
if (fragment == null) {
fragment = TestListFragment.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(android.R.id.content, fragment, TAG_LIST);
ft.commit();
}
}
}
package test.fragment.list;
import java.util.ArrayList;
import java.util.List;
import test.fragment.R;
import android.app.ListFragment;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class TestListFragment extends ListFragment {
public static TestListFragment newInstance() {
return new TestListFragment();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_column, R.id.text);
setListAdapter(adapter);
adapter.addAll(createDataList(100));
}
private static List<String> createDataList(int counts) {
List<String> list = new ArrayList<String>();
for (int i = 0; i < counts; i++) {
list.add("i=" + i);
}
return list;
}
}
@kaizer1
Copy link

kaizer1 commented May 2, 2014

Fucking shit. This is bad, very fucking bad example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment