问题描述
我有一个ListView这是建立了两个textviews从单独的布局文件来了。我用的是 BaseAdapter
来建立从JSON文件列表。
I have a listview which is build up of two textviews coming from a separate layout file. I use a BaseAdapter
to build the list from a JSON file.
我想第一的TextView(标题)是点击,点击,如果它显示了第二的TextView(文本),如果再次单击它隐藏它。
I would like the first textview (Caption) to be clickable, if clicked that it shows the second textview (Text), and if clicked again that it hides it.
当我使用的onClick
(安卓的onClick =ClText
)我得到一个错误。我想我应该用的东西 onClickListener
,但由于我是新来的Android,我不太清楚如何使用它。
When I use an onClick
(android:onClick="ClText"
) I get an error. I think I should use something of an onClickListener
, but since I'm new to Android I'm not quite sure how to use it.
有人可以帮助我的code吗?
Can someone help me out with the code please?
推荐答案
您只需要设置onClickListener在您的适配器类的getView方法延伸BaseAdapter的第一个项目。下面就来说明你正在尝试做的一个例子。
You just need to set the onClickListener for the first item in the getView method of your adapter class that extends BaseAdapter. Here's an example to illustrate what you're trying to do.
public class CustomAdapter extends BaseAdapter{
private ArrayList<Thing> mThingArray;
public CustomAdapter(ArrayList<Thing> thingArray) {
mThingArray = thingArray;
}
// Get the data item associated with the specified position in the data set.
@Override
public Object getItem(int position) {
return thingArray.get(position);
}
// Get a View that displays the data at the specified position in the data set.
// You can either create a View manually or inflate it from an XML layout file.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
// LayoutInflater class is used to instantiate layout XML file into its corresponding View objects.
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.one_of_list, null);
}
TextView captionTextView = (TextView) convertView.findViewById(R.id.caption);
TextView txt2 = (TextView)findViewById(R.id.text);
captionTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(txt2.getVisibility() == View.INVISIBLE){
txt2.setVisibility(View.VISIBLE);
} else {
txt2.setVisibility(View.INVISIBLE);
}
}
});
return convertView;
}
}
这篇关于更改在ListView一个TextView的知名度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!