我在自定義列表視圖中有2個文本視圖textview1和textview2。列表視圖中的總行數爲10.如果textview 2中的所有元素都有一個值,即「true」。當第10個元素的textview1被點擊時,它應該顯示一個消息「Success」,或者即使一個textview2的值不是「true」,那麼點擊第10個元素textview1應該顯示一個消息「Failure」。我的代碼 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" };
}
你總是可以依靠提供給適配器的模型數據,並做一些字符串比較,而不是依賴於視圖。 – bHaRaTh