0

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

这是我的代码包 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" };

}

4

2 回答 2

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;
            }
        }
    }
         }
       });
于 2013-01-08T07:29:14.090 回答
-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
 }

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

于 2013-01-08T05:01:16.120 回答