的Android如何识别项目列表视图中使用复选框 [英] Android how to identify item in listview with checkbox
问题描述
我真的在这里停留。我要的不是简单的(对我来说),但我一直在编程机器人现在一年。 我要的是与ImageView的,一个TextView,一个复选框,每行中的另一个TextView中列表视图。 让我们一个TextView和布局复选框第一。 基于本教程中,我成功地做到这一点(有有很多,但是这似乎是最适合我)。我有填充textviews和复选框列表视图。
这是结果:
这是我怎么弄的TextView我点击文本:
textView.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
TextView的电视=(TextView中)V;
。字符串text_of_clicked_textview = tv.getText()的toString();
Log.i(TAG,+ text_of_clicked_textview);
}
});
所以,当我点击水星,我得到的水星的在text_of_clicked_textview变量。
但我如何检查我点击了哪个复选框?例如我点击3号复选框,我想现在是地球一行。最好的是,如果我了解的TextView的两个列表视图的行中的文本(地球的),并与项目数量( 3 的)。
我想我一定要上的复选框设置onClickListener,但接下来呢?
checkBox.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
复选框CB =(复选框)V;
}
});
这是XML的ListView自定义行:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
< ImageView的
机器人:ID =@ + ID / imageView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:SRC =@可绘制/ camera_icon/>
<的TextView
机器人:ID =@ + ID / textView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=大文本
机器人:textAppearance =:/>中的Android ATTR / textAppearanceLarge?
<复选框
机器人:ID =@ + ID / checkBox1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=
/>
< / LinearLayout中>
这是完整的活动在列表中实现: 只是复制这个活动的测试就明白了code。发生在主要活动一的ListView
包com.DemoTest;
进口的java.util.ArrayList;
进口的java.util.List;
进口java.util.zip.Inflater中;
进口android.app.Activity;
进口android.content.Context;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.BaseAdapter;
进口android.widget.CheckBox;
进口android.widget.ImageView;
进口android.widget.ListView;
进口android.widget.TextView;
公共类CustomList扩展活动实现OnClickListener
{
ListView控件的ListView;
ArrayList的< EachRow>名单=新的ArrayList< CustomList.EachRow>();
EachRow每一个;
@覆盖
保护无效的onCreate(包savedInstanceState)
{
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
的String []颜色= {红,绿,蓝,白,黄,青色,紫色,灰色,
0red,0green,0blue,0white,0yellow,0cyan,0purple,0grey,
1red,1green,1blue,1white,1yellow,1cyan,1purple,1grey};
对于(字符串str:颜色)
{
每个=新EachRow();
each.text =海峡;
list.add(每个);
}
ListView的=(的ListView)findViewById(R.id.listView1);
listView.setAdapter(新MyAdapter(此,0,列表));
//listView.setOnItemClickListener(this);
}
类MyAdapter扩展ArrayAdapter< EachRow>
{
LayoutInflater inflat;
ViewHolder持有人;
公共MyAdapter(上下文的背景下,INT textViewResourceId,
ArrayList的< EachRow>对象)
{
超(背景下,textViewResourceId,对象);
// TODO自动生成构造函数存根
inflat = LayoutInflater.from(上下文);
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
// TODO自动生成方法存根
如果(convertView == NULL)
{
convertView = inflat.inflate(R.layout.row_checkox,NULL);
持有人=新ViewHolder();
holder.textView =(TextView中)convertView.findViewById(R.id.textView1);
holder.image =(ImageView的)convertView.findViewById(R.id.imageView1);
holder.check =(复选框)convertView.findViewById(R.id.checkBox1);
holder.check.setOnClickListener(CustomList.this);
convertView.setTag(保持器);
}
支架=(ViewHolder)convertView.getTag();
EachRow行=的getItem(位置);
Log.d(大小,row.text);
holder.textView.setText(row.text);
holder.check.setChecked(row.checkBool);
holder.check.setTag(位置);
返回convertView;
}
@覆盖
公共EachRow的getItem(INT位置){
// TODO自动生成方法存根
返回list.get(位置);
}
私有类ViewHolder
{
的TextView TextView的;
ImageView的形象;
复选框检查;
}
}
私有类EachRow
{
文本字符串;
布尔checkBool;
}
@覆盖
公共无效的onClick(视图v)
{
// TODO自动生成方法存根
EachRow行= list.get((整数)v.getTag());
row.checkBool = row.checkBool!;
Log.d(项目,项目在点击+(整数)v.getTag()+:+ row.text +就是+ row.checkBool);
}
}
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.
This is the result:
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);
}
});
So When I click on Mercury, I get Mercury in the text_of_clicked_textview variable.
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).
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 ;
}
});
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>
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如何识别项目列表视图中使用复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!