安卓笔记之点击listView条目并显示其内容

package com.example.day0328_listview_2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private ListView listView;
    private List<Map<String, Object>> data;
    private SimpleAdapter adapter;
    @Override
      //如果布局中的子控件可以获得焦点(Button,ImageButton,CheckBox)
      //整个父控件(LinearLayout)就失去了焦点
      //解决方法可以在父控件中设置    android:descendantFocusability="blocksDescendants"
      //让子控件不要获得焦点
      //beforeDescendants先于子控件获得焦点
      //afterDescendants后语子控件获得焦点(默认值)
      //blocksDescendants阻止子控件获得焦点
      // 同时在对应的子控件中设置
      //android:docusable="false"
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取ListView对象
        listView = (ListView) findViewById(R.id.lv);
        data =getData();//获取数据
        adapter = new SimpleAdapter(MainActivity.this, data,
                R.layout.item,
                new String[]{"图片","姓名","年龄","按钮"}, 
                new int[]{R.id.iv,R.id.tv1,R.id.tv2,R.id.bt});
        listView.setAdapter(adapter);
        //设置Listview条目点击侦听
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // 点击ListView每一项显示其内容
                //view是item.xml最外层的布局
                //判断view是否属于LinearLayout,是就进行强转,并获取其内容
                if (view instanceof LinearLayout) {
                    LinearLayout layout = (LinearLayout) view;
                    View view1 =layout.getChildAt(1);
                    //同样判断view1是否属于LinearLayout,是就进行强转,并获取其内容
                    if (view1 instanceof LinearLayout) {
                        LinearLayout layout2 = (LinearLayout) view1;
                        //获取第一个文本框的内容
                        TextView textView0 =(TextView) layout2.getChildAt(0);
                        //获取第二个文本框的内容
                        TextView textView1 =(TextView) layout2.getChildAt(1);
                        //Toast吐司显示内容
                        Toast.makeText(MainActivity.this,textView0.getText()+"\n"+ textView1.getText(), 0).show();
                    }
                    
                    
                }
                
            }
        });
    }
    
    //生成数据
    public List<Map<String, Object>> getData(){
        List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
        Map<String,Object> map = new HashMap<String, Object>();
        //往map里面添加数据
        map.put("图片", R.drawable.ic_launcher);
        map.put("姓名", "野比大雄");
        map.put("年龄", 88);
        map.put("按钮", "更多...");
        //把内容添加进list
        list.add(map);
        Map<String,Object> map2 = new HashMap<String, Object>();
        //往map2里面添加数据
        map2.put("图片", R.drawable.ic_launcher);
        map2.put("姓名", "野比先生");
        map2.put("年龄", 888);
        map2.put("按钮", "更多...");
        //把内容添加进list
        list.add(map2);
        Map<String,Object> map3 = new HashMap<String, Object>();
        //往map3里面添加数据
        map3.put("图片", R.drawable.ic_launcher);
        map3.put("姓名", "野比太太");
        map3.put("年龄", 888);
        map3.put("按钮", "更多...");
        //把内容添加进list
        list.add(map3);
        //返回数据list集合
        return list;
    }
}

//布局
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

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

</RelativeLayout>
//item布局
<?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"
    android:descendantFocusability="blocksDescendants"
     >
     <!-- 
   如果布局中的子控件可以获得焦点(Button,ImageButton,CheckBox)
   整个父控件(LinearLayout)就失去了焦点
   解决方法可以在父控件中设置    android:descendantFocusability="blocksDescendants"
   让子控件不要获得焦点
  beforeDescendants先于子控件获得焦点
  afterDescendants后语子控件获得焦点(默认值)
  blocksDescendants阻止子控件获得焦点
   同时在对应的子控件中设置
   android:docusable="false"
      -->
    <ImageView 
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        />
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tv1"
            />
       <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tv2"
            />
    </LinearLayout>
    <Button 
        
        android:focusable="false"
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        />
</LinearLayout>

 

转载于:https://www.cnblogs.com/zcl1314/p/5329652.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值