//布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hzx.ble_scan_broadcast.MainActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</RelativeLayout>
//activity
public class MainActivity extends AppCompatActivity {
private ListView listView;
private ArrayAdapter<String> adapter;
private List<String> data = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = MyDataSource.getDataSource();
//adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data);
adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_single_choice,data);
listView = (ListView)findViewById(R.id.listview);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this,"click me",1).show();
return false;
}
});
}
}
image.png
网友评论