Skip to content

Instantly share code, notes, and snippets.

@ishitcno1
Last active December 29, 2015 17:39
Show Gist options
  • Save ishitcno1/7705425 to your computer and use it in GitHub Desktop.
Save ishitcno1/7705425 to your computer and use it in GitHub Desktop.
Android use ListFragment and SimpleAdapter. Creating event callbacks to the activity.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:name="package.name.SampleFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</fragment>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/icon"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="26dip"
android:id="@+id/firstLine"
android:layout_toRightOf="@id/icon"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="26dip"
android:id="@+id/secondLine"
android:layout_toRightOf="@id/icon"
android:layout_below="@id/firstLine"
android:textSize="12sp" />
</RelativeLayout>
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements SampleFragment.OnArticleSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onArticleSelected() {
Toast.makeText(this, "Article selected.", Toast.LENGTH_SHORT).show();
}
}
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
public class SampleFragment extends ListFragment {
private OnArticleSelectedListener mListener;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[]{"Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2"};
ArrayList list = new ArrayList();
for (int i = 0; i < values.length; i++) {
HashMap hashMap = new HashMap();
hashMap.put("name", values[i]);
hashMap.put("description", "some description");
hashMap.put("icon", R.drawable.ic_launcher);
list.add(hashMap);
}
String[] from = new String[]{"name", "description", "icon"};
int[] to = new int[]{R.id.firstLine, R.id.secondLine, R.id.icon};
this.setListAdapter(new SimpleAdapter(getActivity(), list, R.layout.detail, from, to));
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnArticleSelectedListener) activity;
} catch (ClassCastException e) {
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
mListener.onArticleSelected();
}
public interface OnArticleSelectedListener {
public void onArticleSelected();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment