我在解决如何在Android的GitHub示例项目中实现'.setOnItemClickListener'方面遇到了一些问题。
我想选择列表视图的单元格,但我可以让它工作。 github项目是:
https://github.com/schrockblock/android-table-view
我正在尝试在我的主要活动中添加监听器,但我不能。这是代码:
public class MainActivity extends ActionBarActivity {
public String[] items={};
WifiHelper wifiHelper;
List<ScanResult> list;
//public WifiManager pWifiManager;
ListView tableView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 取得WifiManager对象
//pWifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiHelper = new WifiHelper(this);
//open wifi
wifiHelper.openWifi(new WifiHelper.openWifiCallback() {
@Override
public void openSuccess() {
//open success
}
@Override
public void openFail() {
//open fail
}
});
//scan wifi
list = wifiHelper.startScan();
tableView = (ListView)findViewById(R.id.table_view);
TableViewDataSource tvds = new TableViewDataSource(this){
@Override
protected int numberOfSections() {
return 3;
}
@Override
protected String titleForHeaderInSection(int section) {
if(section==0)
return "\nGENERAL";
else if(section==1)
return "Pull down to refresh networks information ot tap 'Scan' button in the top bar of the screen.\n\nNETWORKS";
else
return "Networks under -75dB (RSSI) may have connectivity problems. Try to be as near as you can of the network.\n\nOnly supported networks can use the exploit feature of this app. You can use iWepPRO as your wifi manager application";
}
@Override
protected int numberOfRowsInSection(int section){
if(section==0)
return 3;
else if(section==1)
return list.size();
else
return 1;
}
@Override
protected DefaultCell cellForRowAtIndexPath(JIndexPath indexPath) {
if(indexPath.section==0) {
DefaultCell cell = new DefaultCell();
cell.hashIBencryption = true;
cell.imageButtonImage = R.drawable.emptywhite2px;
switch (indexPath.row){
case 0:
cell.textLabelText = "WiFi";
cell.hasDisclosureIndicator = false;
cell.hasDisclosureIndicator2 = false;
cell.hasSwitch = true;
cell.hasSwitchOn = true;
break;
case 1:
cell.textLabelText = "Auto-Scan";
cell.hasDisclosureIndicator = false;
cell.hasDisclosureIndicator2 = false;
cell.hasSwitch = true;
cell.hasSwitchOn = false;
break;
case 2:
cell.textLabelText = "Information";
cell.hasDisclosureIndicator = true;
cell.hasDisclosureIndicator2 = false;
break;
default:
break;
}
return cell;
}else if(indexPath.section==1){
SubtitleCell cell1 = new SubtitleCell();
cell1.hasDisclosureIndicator = false;
cell1.hasDisclosureIndicator2 = true;
cell1.hashIBencryption = true;
//cell1.textLabelText = "network name " + indexPath.row;
ScanResult r = list.get(indexPath.row);
//String ssidName = r.SSID;
cell1.textLabelText = r.SSID;
//cell1.detailTextLabelText = "signal " + indexPath.row;
cell1.detailTextLabelText = "" + r.level;
//cell1.detailTextLabelText = "" + calculateSignalStength(pWifiManager, r.level);
cell1.imageButtonImage = R.drawable.deviceaccesssecure;
return cell1;
}else if(indexPath.section==2){
SubtitleCell cell2 = new SubtitleCell();
cell2.hashIBencryption = true;
cell2.imageButtonImage = R.drawable.emptywhite2px;
cell2.textLabelText = " ";
cell2.hasDisclosureIndicator2 = false;
cell2.hasDisclosureIndicator = false;
cell2.hasEmptyCell = true;
return cell2;
}
return null;
}
};
tableView.setAdapter(tvds);
}
我已经阅读并测试了'setAdapter()'函数之后编码setOnItemClickListener的一些示例,但它不起作用。
我正在设置单元格选择以显示带有一些文本的Toast,以查看它是否有效,但我不知道我做错了什么。
任何人都可以帮我一些提示吗?
非常感谢你。
这是我的两个布局:
单元格布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl"
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:padding="0dp"
android:background="#ffdedede">
<TextView
android:id="@+id/header_text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:textSize="12dp"
android:textColor="@android:color/darker_gray"
android:padding="0dp"
android:visibility="gone"
android:layout_marginLeft="20dp"
android:typeface="sans"
android:layout_marginRight="20dp"
android:elegantTextHeight="false" />
<RelativeLayout
android:id="@+id/rl2"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_below="@+id/header_text_view"
android:padding="0dp"
android:background="@android:color/white">
<RelativeLayout
android:id="@+id/rl3"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="0dp"
android:background="@android:color/white">
<TextView android:id="@+id/title_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cell Title"
android:textSize="18sp"
android:textColor="#000"
android:background="#0fff"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingBottom="2dp"
android:typeface="sans"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/ibencryption"
android:layout_toEndOf="@+id/ibencryption" />
<TextView
android:id="@+id/detail_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cell Subtitle"
android:paddingLeft="5dp"
android:paddingBottom="5dp"
android:textColor="#555"
android:background="#0fff"
android:textSize="14sp"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/ibencryption"
android:layout_toEndOf="@+id/ibencryption" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ibencryption"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:background="@android:color/white"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:adjustViewBounds="false" />
</RelativeLayout>
<ImageView
android:id="@+id/disclosure_indicator"
android:src="@drawable/nextitem"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginRight="20dp"
android:cropToPadding="true"/>
<ImageView
android:id="@+id/disclosure_indicator_2"
android:src="@drawable/actionabout"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginRight="20dp"
android:cropToPadding="true"/>
<Switch
android:id="@+id/switch_indicator"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginRight="20dp"
android:cropToPadding="true"/>
</RelativeLayout>
主要活动布局:
<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"
tools:context=".MainActivity" >
<ListView
android:id="@+id/table_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
谢谢!
答案 0 :(得分:0)
这是您提供OnItemClickListener的方法:
tableView.setOnItemClickListener(new OnItemClickListener(){
void onItemClick(AdapterView<?> parent, View view, int position, long id){
switch(position){
default: Toast.makeToast(MainActivity.this, "Some crappy text",Toast.LENGTH_LONG ).show();
}
}
}};
一般来说:
首先,将所有View#findViewById(int)
代码放在Activity#setContentView(int)
电话正下方的OnCreate方法的顶部。
然后尝试扫描您的WifiManager#openSuccess()回调..
尝试重构代码并替换所有if,else if和else块,这些块检查带有switch块的整数。 请不要返回null EVER 。