Create array of strings in xml file Android example
Example source code for Android developers how create array of strings in *.xml file.
You can using array of strings in Spinner ( combobox - dropdown list ) or ListView etc.
You can using array of strings in Spinner ( combobox - dropdown list ) or ListView etc.
<resources>
<string-array name="colors">
<item>red</item>
<item>orange</item>
<item>yellow</item>
<item>green</item>
<item>blue</item>
</string-array>
</resources>
396LW NO topic_id
AD
Další témata ....(Topics)
Try this code:
final String ERROR = "my error message....";
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
// some code and method ..... see AsyncTask
@Override
protected String doInBackground(String... urls) {
URL urlL = null;
try {
urlL = new URL(url);//"//chmi.cz..../"
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) urlL.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
response = readStream(in);
return response;
} catch (IOException e) {
//throw new RuntimeException(e);
} finally {
if(urlConnection != null)
urlConnection.disconnect();
return ERROR;
}
} catch (MalformedURLException e) {
e.printStackTrace();
return ERROR;
}
}
return response;
}
private String readStream(InputStream is) {
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int i = is.read();
while(i != -1) {
bo.write(i);
i = is.read();
}
return bo.toString();
} catch (IOException e) {
return "";
}
}
Update TextView by runnable. Handler, runnable, timer Android example.
main.xml
public class TimerActivity extends Activity {
TextView hTextView;
Button hButton, hButtonStop;
private Handler mHandler = new Handler();
private int nCounter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hTextView = (TextView)findViewById(R.id.idTextView);
hButton = (Button)findViewById(R.id.idButton);
hButton.setOnClickListener(mButtonStartListener);
hButtonStop = (Button)findViewById(R.id.idButtonStop);
hButtonStop.setOnClickListener(mButtonStopListener);
} // end onCreate
View.OnClickListener mButtonStartListener = new OnClickListener() {
public void onClick(View v) {
try {
mHandler.removeCallbacks(hMyTimeTask);
// Parameters
// r The Runnable that will be executed.
// delayMillis The delay (in milliseconds) until the Runnable will be executed.
mHandler.postDelayed(hMyTimeTask, 1000); // delay 1 second
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private Runnable hMyTimeTask = new Runnable() {
public void run() {
nCounter++;
hTextView.setText("Hallo from thread counter: " + nCounter);
}
};
/**
*
*/
View.OnClickListener mButtonStopListener = new OnClickListener() {
public void onClick(View v) {
mHandler.removeCallbacks(hMyTimeTask);
}
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/idTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="Button"
android:id="@+id/idButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/idButtonStop"
android:text="Stop"></Button>
</LinearLayout>
Delete - unistal app from testing device - emulator and try again RUN - DEBUGG your app.
How start certain Activity if user clicked to ListView item
Start Activity from list – launches other activities from list - latest variant!!!!
Game class Game.java game.xml
You have to add class Game to AndroidManifest.xml
Start Activity from list – launches other activities from list - latest variant!!!!
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.main);
ListView mlistView = (ListView) findViewById(R.id.idListView);
mlistView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
new String[] {"Game", "Help", "Home site"}));
mlistView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text Game, Help, Home
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
String sText = ((TextView) view).getText().toString();
Intent intent = null;
if(sText.equals("Game"))
intent = new Intent(getBaseContext(),
Game.class);
//else if(sText.equals("Help")) ..........
if(intent != null)
startActivity(intent);
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Game class Game.java game.xml
You have to add class Game to AndroidManifest.xml
<activity android:name=".Game" android:label="GameLabel">
</activity>
package cz.okhelp.listview;
import android.app.Activity;
import android.os.Bundle;
public class Game extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// you have to create game.xml
setContentView(R.layout.game);
}
}
Warning: The application may be doing too much work on its main thread
Try this sorce code:
Try this sorce code:
import android.os.StrictMode;
public class MyActivity extends Activity {
static{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
@Override
public void onCreate(Bundle savedInstanceState) {
//.................. etc.
Editace: 2013-12-09 10:55:08
Počet článků v kategorii: 396
Url:create-array-of-strings-in-xml-file-android-example