我在自定义列表视图中有 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" };
}