Skip to content

Instantly share code, notes, and snippets.

View pwittchen's full-sized avatar

Piotr Wittchen pwittchen

View GitHub Profile
@Override
protected void onResume() {
List<Contact> contacts = getContactsFromYourSource();
ContactsAdapter contactsAdapter = new ContactsAdapter(contacts);
contactListView.setAdapter(contactsAdapter);
highlightListItem(2); // this simple function call does the trick
}
private void highlightListItem(int position) {
ContactsAdapter contactsAdapter = (ContactsAdapter) contactListView.getAdapter();
private void clickOnListItem(int position) {
contactList.performItemClick(contactListView, position, contactListView.getItemIdAtPosition(position));
}
public String multipartRequest(String urlTo, String post, String filepath, String filefield) throws ParseException, IOException {
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
InputStream inputStream = null;
String twoHyphens = "--";
String boundary = "*****"+Long.toString(System.currentTimeMillis())+"*****";
String lineEnd = "\r\n";
String result = "";
@pwittchen
pwittchen / searchString.java
Created March 30, 2014 15:56
Implementation of Knuth-Morris-Pratt (KMP) algorithm in Java for searching occurences of a specific word in a given string.
public class Main {
public static void main(String args[]) {
String givenString = "ABC ABCDAB ABCDABCDABDE";
String searchedString = "ABCDABD";
int givenStringLetterPosition = 0;
int searchedStringLetterPosition = 0;
int foundAt = -1;
while (givenStringLetterPosition < givenString.length()) {
if (givenString.charAt(givenStringLetterPosition) == searchedString.charAt(searchedStringLetterPosition)) {
public interface SampleView {
void displayData(String data);
}
public class SampleController {
private SampleView view;
public SampleController(SampleView view) {
this.view = view;
}
public void generateData() {
String generatedData = "Exemplary data generated by controller.";
view.displayData(generatedData);
public class SampleActivity extends Activity implements SampleView {
@InjectView(R.id.sample_textview)
private sampleTextView;
private SampleController controller;
@Override
public displayData(String data) {
sampleTextView.setText(data);
}
@pwittchen
pwittchen / loadJson.js
Created April 19, 2014 18:25
Loading JSON file with JavaScript. Please note: it won't run as a "local" script. JavaScript does not allow to load local files due to security reasons. You should deploy it on the server in order to load JSON file.
function loadJson(callback) {
var XmlHttpRequest = new XMLHttpRequest();
XmlHttpRequest.overrideMimeType("application/json");
XmlHttpRequest.open('GET', 'file.json', true);
XmlHttpRequest.onreadystatechange = function () {
if (XmlHttpRequest.readyState == 4 && XmlHttpRequest.status == "200") {
// .open will NOT return a value
// but simply returns undefined in async mode so use a callback
callback(XmlHttpRequest.responseText);
}
@pwittchen
pwittchen / update_synced_fork.txt
Created June 4, 2014 20:36
How to update synced fork
Original topic: http://stackoverflow.com/questions/7244321/how-to-update-github-forked-repository
In your local clone of your forked repository, you can add the original GitHub repository as a "remote". ("Remotes" are like nicknames for the URLs of repositories - origin is one, for example.) Then you can fetch all the branches from that upstream repository, and rebase your work to continue working on the upstream version. In terms of commands that might look like:
# Add the remote, call it "upstream":
git remote add upstream https://github.com/whoever/whatever.git
# Fetch all the branches of that remote into remote-tracking branches,
# such as upstream/master:
@pwittchen
pwittchen / ubuntu-boot-error
Created August 17, 2014 19:37
Ubuntu boot error
Gave up waiting for root device. Common problems:
- Boot args (cat /proc/cmdline)
- Check rootdelay= (did the system wait long enough?)
- Check root= (did the system wait for the right device)
- Missing modules (cat /proc/modules; ls /dev)
ALERT! /dev/disk/by-uuid/96889309-5f73-4688-8354-e64cd1bb158f does not exist. Dropping to a shell!
BusyBox v1.21.1 (Ubuntu 1:1.21.0-1ubuntu1) built-in shell (ash)
Enter 'help' for a list of build-in commands.