Android programming for a web developer or a quick start for the smallest. Part 3

  • Tutorial

This article is a continuation of the part 1 and part 2 that I started .


Important: this lesson is not professional. The author of the lesson is not a specialist in programming for the Android platform. I apologize in advance for unjustified expectations. Since the previous lesson received more positive reviews, I decided to continue.

What are you talking about

In the first part I mentioned that the application will be able to log in and display server data. We realize:
  1. Login
  2. Listing of data received from the server

For me, authorization is:
  • Database query to verify user availability
  • Processing the response and starting the session with successfull

This is exactly what I do in a web application. For the client server, I decided to get by with simulating a session using local storage. Requests to the server, receiving and processing responses have already been discussed in Part 2, we will move on to storing the “session”.

Storing data with Preferences

Values ​​are saved as a pair: name, value. After authorization, we save the data. At the subsequent opening of the application, we will not need to re-authorize, just read the recorded data.


        SharedPreferences sPref = getPreferences(MODE_PRIVATE);
	    Editor ed = sPref.edit();
	    ed.putString(LOGIN,login.getText().toString() );
	    ed.putString(PASSORD,password.getText().toString() );


        SharedPreferences sPref = getPreferences(MODE_PRIVATE);
	    String login = sPref.getString(LOGIN, "");
	    String password = sPref.getString(PASSORD, "");

Reading Items into a List (RoomsActivity)

Suppose we have successfully logged in and an Activity has opened for us, which receives a list of chat rooms in the form:


Let's do it:

//  в объект наш ответ
JSONObject json = new JSONObject(result);
//  массив из объекта
JSONArray jsa = json.getJSONArray("rooms");
// находим список
ListView roomsLv = (ListView) findViewById(;
// записываем в строковой массив json массив
String[] StringArray = new String[jsa.length()];
for(int i = 0; i < jsa.length(); i++) {
     StringArray[i] = jsa.getString(i);
// создаем адаптер
ArrayAdapter<String> adapter = new ArrayAdapter<String>(RoomsActivity.this,android.R.layout.simple_list_item_1, StringArray);
// назначаем списку адаптер

After all, we get a working list. Now we will process clicking on the list item and transfer the name of the room to another Activity (RoomSetActivity).

roomsLv.setOnItemClickListener(new OnItemClickListener() {
     publicvoidonItemClick(AdapterView<?> parent, View view,int position, long id){
		     Intent intent = new Intent(RoomsActivity.this, RoomSetActivity.class);
		     intent.putExtra("roomName", parent.getItemAtPosition(position).toString());

In RoomSetActivity, you can read the name of the room like this:

String roomName =  getIntent().getExtras().getString("roomName");

The end.

Also popular now: