The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks
          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="    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="
              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>








          主站蜘蛛池模板: 桐柏县| 边坝县| 沧源| 梁平县| 泰安市| 姜堰市| 佛学| 怀远县| 景泰县| 封开县| 建水县| 县级市| 沽源县| 那坡县| 靖西县| 炉霍县| 乡城县| 柳州市| 上林县| 蓬安县| 陆丰市| 玉溪市| 芦山县| 婺源县| 巴林左旗| 曲周县| 景泰县| 名山县| 扎鲁特旗| 永寿县| 罗甸县| 商水县| 阿克陶县| 红安县| 临夏县| 永新县| 西充县| 彭阳县| 乌鲁木齐市| 宁阳县| 增城市|