demo14

/Users/alamps/AndroidStudioProjects/Demo12SimpleAdapter/Demo12SimpleAdapter/src/main/res/layout/table_list.xml

<TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp">

    <TextView
            android:textSize="25px"
            android:gravity="center_vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="信息列表INFOLIST" />

    <TableRow>
        <ImageView
                android:id="@+id/_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/icon"/>
        <TextView
                android:id="@+id/_id"
                android:textSize="15px"
                android:gravity="center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />

        <TextView
                android:id="@+id/_name"
                android:textSize="15px"
                android:gravity="center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
    </TableRow>
</TableLayout>


=================
/Users/alamps/AndroidStudioProjects/Demo12SimpleAdapter/Demo12SimpleAdapter/src/main/res/layout/activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <!--1,显示选择的信息-->
    <TextView
            android:id="@+id/_info"
            android:gravity="center_horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />

    <ListView
        android:id="@+id/data_list"
        android:layout_width="fill_parent"
       android:layout_height="wrap_content"/>

</LinearLayout>


================
package com.example.demo12simpleadapter;

import android.app.ListActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {
    private String data[][]={{"01","www.csdn.com"},{"02","www.baidu.com"},{"03","wwww.alamps.com"},{"04","www.iteye.com"},{"05","www.google.com"},{"06","www.qq.com"}};
    private List<Map<String,String>>  list = new ArrayList<Map<String,String>>();
    private ListView listView;
    private SimpleAdapter simpleAdapter=null;
    //onItemClickListener
    private TextView info=null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.listView= (ListView) super.findViewById(R.id.data_list);//mac android studio [alt+return] == eclipse[ctrl+1]


        this.info = (TextView) super.findViewById(R.id._info);



        for (int i =0;i<this.data.length;i++){

            Map<String,String> map = new HashMap<String, String>();
            map.put("_id",data[i][0]);
            map.put("_name",data[i][1]);
            this.list.add(map);
        }
        //public SimpleAdapter(android.content.Context context, java.util.List<? extends java.util.Map<java.lang.String,?>> data, int resource, java.lang.String[] from, int[] to) { /* compiled code */ }
        this.simpleAdapter = new SimpleAdapter(this,
                this.list,
                R.layout.table_list,
                new String[]{"_id","_name"},new int[]{R.id._id,R.id._name});


        this.listView.setAdapter(this.simpleAdapter);

        this.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int idx, long id) {
                Map<String,String> mapItem = (Map<String,String>)MainActivity.this.simpleAdapter.getItem(idx);
                String _id=mapItem.get("_id");
                String _name=mapItem.get("_name");
                MainActivity.this.info.setText("选择的数据ID:"+_id+"名称是:"+_name);
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

 

posted @ 2016-03-13 16:07  Alamps  阅读(202)  评论(0编辑  收藏  举报