我使用基本适配器创建了自定义列表视图,但由于某种原因无法单击各个列表视图中的按钮。
以下是主要布局的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Light List"
android:id="@+id/lightName"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/loginbutton"
android:id="@+id/button"
android:onClick="buttonClicked"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/lightName"
android:layout_alignStart="@+id/lightName" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lightList"
android:layout_below="@+id/lightName"
android:layout_above="@+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
以下是列表中视图的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lightList"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/lightName"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/statusText"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/buttonoff"
android:focusable="true"
android:enabled="true"
android:clickable="true" />
</LinearLayout>
最后这里是适配器代码:
public ArrayList<myLight> dataSet = new ArrayList<myLight>();
public void makeLightList(ArrayList<myLight> lights) {
Iterator<myLight> iter = lights.iterator();
while(iter.hasNext()) {
dataSet.add(iter.next());
}
}
@Override
public int getCount() {
return dataSet.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v;
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.lightlistlayout, null);
TextView name = (TextView) findViewById(R.id.lightName);
name.setText(dataSet.get(position).name);
TextView status = (TextView) findViewById(R.id.statusText);
if(dataSet.get(position).reachable) {
if(dataSet.get(position).isOn) {
status.setText("Light is On");
} else {
status.setText("Light is Off");
}
} else {
status.setText("Light is currently not reachable");
}
dataHolder dh = new dataHolder();
dh.name = dataSet.get(position).name;
//Handle Off button click from list view
Button offButton = (Button) findViewById(R.id.buttonoff);
offButton.setTag(dh);
offButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dataHolder dhv = new dataHolder();
dhv = (dataHolder)v.getTag();
Toast.makeText(getBaseContext(), dhv.name + " Turned Off", Toast.LENGTH_LONG).show();
}
});
return v;
}
};
我不确定我的onclicklistener是否正确,因为我无法点击按钮。这是一张显示按钮的图片。我在xml文件中搞乱了不同的焦点和可点击的设置,没有运气。我添加了一个我可以来回滑动的导杆,但按钮不会让我点击它。
答案 0 :(得分:0)
v = inflater.inflate(R.layout.lightlistlayout, null);
在这里,你已经为你的适配器的行文件充气了,下面是你的id初始化,为此你还没有将你的视图添加到你的id,所以试试这个。
TextView name = (TextView) v.findViewById(R.id.lightName);
TextView status = (TextView) v.findViewById(R.id.statusText);
Button offButton = (Button) v.findViewById(R.id.buttonoff);
答案 1 :(得分:0)
尝试更改
android:focusable="true" to android:focusable="false" in Button's xml