如何检查列中的所有元素是否都有相同的文本?

时间:2013-01-08 04:54:35

标签: android listview

我在自定义列表视图中有2个文本视图textview1和textview2。列表视图中的总行数为10.如果textview 2中的所有元素都具有值“true”。单击第10个元素的textview1时,它应显示消息“成功”,或者即使一个textview2的值不是“true”,单击第10个元素textview1也应显示消息“失败”。

这是我的代码     package com.example.list;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity 
{
ListView lv;
private  static boolean test= false;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView)findViewById(R.id.lv);   
    lv.setAdapter(new slist(this));         
}

public class slist extends BaseAdapter
{
     private Context context;

      public slist(Context c) 
      {
           context = c;
      }

    @Override
    public int getCount()
    {           
          return names.length;
    }

    @Override
    public Object getItem(int position) 
    {           
        return null;
    }

    @Override
    public long getItemId(int position) 
    {           
        return 0;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {

           LayoutInflater li = getLayoutInflater();
           v = li.inflate(R.layout.customlist, null);

           final TextView t1 = (TextView) v.findViewById(R.id.tv1);
           final TextView t2 = (TextView) v.findViewById(R.id.tv2);

           t1.setText(names[position]);
           t2.setText(bikes[position]);


           t1.setOnClickListener(new OnClickListener() 
           {                
            public void onClick(View v)
             {
                    if(t1.getText().toString().equalsIgnoreCase(names[names.length-1]))
                {

                 //Here i should loop through and check all the elements in the bikes[] 
                }
                else
                {
                  Toast.makeText(getApplicationContext(), t1.getText().toString(), Toast.LENGTH_SHORT).show();
                }

             }
           });

           return v;
    }

}

String [] names = { "rams", "venu", "pavan", "sravan", "vishnu", "krishna", "hari", "prasad", "raghu" };
    String [] bikes = { "true", "true", "false", "true", "true", "false", "true", "false", "true" };

}

2 个答案:

答案 0 :(得分:0)

您可以使用以下代码

 t1.setOnClickListener(new OnClickListener() 
       {                
        public void onClick(View v)
         {
    //check for the position of last item
    if(position==(names.length-1))
    {
        //check for second element of other 9 items
        for(int i=0;i<bikes.length-1;i++)
        {

            if(bikes[i].equals("true"))
            {
                result=true;
            }
            else
            {
                result=false;
            }
        }
    }
         }
       });

答案 1 :(得分:-1)

试试这个

 index=Integer.parseInt(v.getTag().toString());
    View view=yourlistviewname.getChildAt(index);
    TextView tv1=view.findViewByID(R.id.textview1);
    TextView tv2=view.findViewByID(R.id.textview2);

    String first=tv1.getText().toString();
    String second=tv2.getText().toString();  

 if(first.equals("yourArrayItemHere")){
 //your message here
 }
 if(second.equals("false")){
 //your message here
 }

并且您可以根据textview的文本设置消息“成功”或“失败”;