Android如何识别项目在listview与复选框 [英] Android how to identify item in listview with checkbox

查看:240
本文介绍了Android如何识别项目在listview与复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的卡在这里。我想要的不是简单的(对我来说),但是我一年来一直在编程安卓。
我想要的是一个listview与一个imageview,一个textview,复选框和每一行中的另一个textview。
让我们在布局中有一个textview和一个复选框。
根据教程我管理做到这一点(有很多,但这似乎是对我最好的)。

I am really stuck here. What I want is not simple (for me), however I've been programming android a year now. What I want is a listview with an imageview, a textview, a checkbox, and another textview in each row. Let's have a textview and a checkbox first in the layout. Based on this tutorial I managed to do that (there are a lot, but this seems to be the best for me). I have a listview populated with textviews and checkboxes.

这是结果:

这是我如何得到textview的文本点击:

This is how I get the text of textview I click on:

 textView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            TextView tv = (TextView) v ;
            String text_of_clicked_textview = tv.getText().toString();
            Log.i("TAG", "" + text_of_clicked_textview);
        }
    });

所以当我点击Mercury时,我在text_of_clicked_textview变量中获取

So When I click on Mercury, I get Mercury in the text_of_clicked_textview variable.

但是如何检查我点击了哪个复选框?例如,我点击第三个复选框,我想现在它在地球的行。如果我知道listview( Earth )行中的textview的文本和项目的编号( 3 ),最好是。

But how can i check which checkbox I clicked on? E.g I click on the 3rd checkbox, I want to now it is in the row of Earth. Best would be if I get to know both the text of textview in the row of the listview(Earth) and and number of the item (3).

我想我必须在复选框上设置一个onClickListener,但是下一步是什么?

I guess I have to set an onClickListener on the checkbox but what next?

checkBox.setOnClickListener( new View.OnClickListener() {
      public void onClick(View v) {
        CheckBox cb = (CheckBox) v ;

      }
    });


推荐答案

这是ListView中自定义行的XML: p>

This is XML for Custom Row in ListView :

    <?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">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/camera_icon" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" 
     />

</LinearLayout>

这是完成其中实现列表的活动:
只需复制此活动以进行测试理解代码。在主活动中放置一个listview

This is Complete Activity in which List is implemented : Just Copy this Activity for test then understand code. Place one listview in main Activity

package com.DemoTest;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class CustomList extends Activity implements OnClickListener
{

    ListView listView;
    ArrayList<EachRow> list=new ArrayList<CustomList.EachRow>();
    EachRow each;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String[] color={"red","green","blue","white","yellow","cyan","purple","grey",
                "0red","0green","0blue","0white","0yellow","0cyan","0purple","0grey",
                "1red","1green","1blue","1white","1yellow","1cyan","1purple","1grey"};
        for(String str : color)
        {
            each=new EachRow();
            each.text=str;
            list.add(each);
        }
        listView=(ListView)findViewById(R.id.listView1);
        listView.setAdapter(new MyAdapter(this, 0, list)); 
        //listView.setOnItemClickListener(this);
    }
    class MyAdapter extends ArrayAdapter<EachRow>
    {
        LayoutInflater inflat;
        ViewHolder holder;
        public MyAdapter(Context context, int textViewResourceId,
                ArrayList<EachRow> objects) 
        {
            super(context, textViewResourceId, objects);
            // TODO Auto-generated constructor stub
            inflat=LayoutInflater.from(context);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            if(convertView==null)
            {
                convertView=inflat.inflate(R.layout.row_checkox, null);
                holder=new ViewHolder();
                holder.textView=(TextView)convertView.findViewById(R.id.textView1);
                holder.image=(ImageView)convertView.findViewById(R.id.imageView1);
                holder.check=(CheckBox)convertView.findViewById(R.id.checkBox1);
                holder.check.setOnClickListener(CustomList.this);
                convertView.setTag(holder);
            }
            holder=(ViewHolder) convertView.getTag();
            EachRow row= getItem(position);
            Log.d("size", row.text);
            holder.textView.setText(row.text); 
            holder.check.setChecked(row.checkBool); 
            holder.check.setTag(position);
            return convertView;
        }

        @Override
        public EachRow getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        private class ViewHolder
        {
            TextView textView;
            ImageView image;
            CheckBox check;
        }
    }
    private class EachRow
    {
        String text;
        boolean checkBool;
    }



    @Override
    public void onClick(View v) 
    {
        // TODO Auto-generated method stub

        EachRow row=list.get((Integer)v.getTag());
         row.checkBool=!row.checkBool;
         Log.d("item", "Item Click at "+(Integer)v.getTag()+" : "+row.text+" is "+row.checkBool); 
    }

}

这篇关于Android如何识别项目在listview与复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆