package www.puyufanyi.com;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import www.puyufanyi.com.test.Word;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.TextView;
public class UIActivity extends Activity{
private ArrayList<Word> words = new ArrayList<Word>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv= (ListView)findViewById(R.id.wordlist);
Word word1 = new Word("index1", "result1");
Word word2 = new Word("index2", "result2");
Word word3 = new Word("index3", "result3");
Word word4 = new Word("index4", "result4");
Word word5 = new Word("index5", "result5");
this.words.add(word1);
this.words.add(word2);
this.words.add(word3);
this.words.add(word4);
this.words.add(word5);
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
for (int i = 0; i < words.size(); i++) {
Map<String, Object> listItem = new HashMap<String, Object>();
listItem.put("Index", words.get(i).getIndex());
listItem.put("Result", words.get(i).getResult());
listItems.add(listItem);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(
this,
listItems,
R.layout.words,
new String[]{"Index","Result"},
new int[]{R.id.index,R.id.result}
);
lv.setAdapter(simpleAdapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String indexValue = ((TextView)view.findViewById(R.id.index)).getText().toString();
Toast.makeText(getApplicationContext(), indexValue, Toast.LENGTH_SHORT).show();
}
});
}
} main.xml
<?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="fill_parent"
android:orientation="vertical" >
<!-- ListView (words) -->
<ListView
android:id="@+id/wordlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
words.xml
<?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="horizontal" >
<TextView
android:id="@+id/index"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp" />
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp" />
</LinearLayout>