Skip to content

Instantly share code, notes, and snippets.

@aztack
Forked from neufuture/MyCustomListView.java
Created January 22, 2012 04:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aztack/1655404 to your computer and use it in GitHub Desktop.
Save aztack/1655404 to your computer and use it in GitHub Desktop.
Android ListView without ListActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/standingText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/scoreText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/userText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/leaderboard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
</LinearLayout>
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
public class MyCustomListView extends Activity{
static final ArrayList<HashMap<String,String>> myList =
new ArrayList<HashMap<String,String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView leaderboard = (ListView) findViewById(R.id.leaderboard);
SimpleAdapter adapter = new SimpleAdapter(
this,
myList,
R.layout.leaderboard_item,
new String[] {"standing","score","user"},
new int[] {R.id.standingText,R.id.scoreText, R.id.userText}
);
populateList();
leaderboard.setAdapter(adapter);
}
private void populateList() {
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("standing","FIRST");
temp.put("score", "2.4sec");
temp.put("user", "daredevil");
myList.add(temp);
temp.put("standing","SECOND");
temp.put("score", "1.8sec");
temp.put("user", "hardcoredropper");
myList.add(temp);
temp.put("standing","THIRD");
temp.put("score", "1.6sec");
temp.put("user", "batman");
myList.add(temp);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment