我遇到了一些问题,想弄清楚如何在安卓的.setOnItemClickListener示例项目中实现‘GitHub’。
我想选择列表视图的单元格,但是我可以让它工作。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);
}我已经阅读并测试了一些在“setOnItemClickListener ()”函数之后编码setAdapter的示例,但是它不起作用。
我正在尝试设置单元格选择,以显示带有文本的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"/>
谢谢你!!
发布于 2015-02-18 00:35:39
您可以这样提供一个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)代码放在OnCreate方法的顶部,就在Activity#setContentView(int)调用的下面。然后尝试扫描您的WifiManager#openSuccess()回调。
尝试重构您的代码,并替换您的所有if,否则,如果和其他块,检查一个整数与开关块。请不要返回空。
https://stackoverflow.com/questions/28573369
复制相似问题