android - OnClickListener must override a superclass method
The method onClick(DialogInterface, int) of type new DialogInterface.OnClickListener(){} must override a superclass method.
Try this:
Right click on project
Select Properties
In open dialogue select Java compiler
Set Enable project specific settings
Set Compiler compliance level to 1.6
Try this:
Right click on project
Select Properties
In open dialogue select Java compiler
Set Enable project specific settings
Set Compiler compliance level to 1.6


396LW NO topic_id
AD
Další témata ....(Topics)
BufferedWriter, FileWriter, write, close MODE_APPEND Java and Android example
Write to file Android example
Write to file Java example
Write to file Android example
try {
String MYFILE = "my_file";
String strText = "My text";
// MODE_APPEND, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE
// create new file or rewrite existing
FileOutputStream fos = openFileOutput(MYFILE, getApplicationContext().MODE_PRIVATE);
// append to file
FileOutputStream fos = openFileOutput(MYFILE, getApplicationContext().MODE_APPEND);
fos.write(strText.getBytes());
fos.close();
} catch (IOException e) {
e.toString();
}
Write to file Java example
try {
// new file
BufferedWriter out = new BufferedWriter(
new FileWriter("outfilename"));
// append text
BufferedWriter out = new BufferedWriter(new FileWriter("outfilename", true));
out.write("some text");
out.close();
} catch (IOException e) {
e.toString();
}
Spinner in Android application is equivalent of ComboBox in WinApi.
Spinner get selected item to string example.
Spinner get selected item position to int example.
Main activity class MainComboBox.java
File MyOnItemSelectedListener.java
strings.xml
main.xml
Spinner get selected item to string example.
Spinner get selected item position to int example.
Main activity class MainComboBox.java
public class MainComboBox extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.countries_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
Button myButton =(Button)findViewById(R.id.button1);
myButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Spinner sp = (Spinner)findViewById(R.id.spinner);
String spinnerString = null;
spinnerString = sp.getSelectedItem().toString();
int nPos = sp.getSelectedItemPosition();
Toast.makeText(getApplicationContext(), "getSelectedItem=" + spinnerString,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "getSelectedItemPosition=" + nPos,
Toast.LENGTH_LONG).show();
}
});
}
}
File MyOnItemSelectedListener.java
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "Item is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
strings.xml
<resources>
<string name="hello">Hello World, MainComboBox!</string>
<string name="app_name">ComboBox</string>
<string name="prompt">Choose a country</string>
<string-array name="countries_array">
<item>China</item>
<item>India</item>
<item>USA</item>
<item>Indonesia</item>
<item>Brazil</item>
<item>Pakistan</item>
<item>Nigeria</item>
<item>Bangladesh</item>
<item>Russia</item>
</string-array>
</resources>
main.xml
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/prompt"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/prompt"
/>
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Dil 4. ArticleFragment.java
V 1. dílu jsme se něco dozvěděli od XML souborech a typu procesoru pro správný běh Android Studia a emulátoru různých typů zařizení s Androidem.
V 2. dílu jsme rozebrali MainActivity.java
V 3. dílu jsme se zabývali HeadlinesFragment.java
V tomto dílu se podíváme na ArticleFragment.java soubor.
Používáme příklad i zip porojekt z https://developer.android.com/training/basics/fragments/creating.html Pozorně si jej nastudujte.
V 1. dílu jsme se něco dozvěděli od XML souborech a typu procesoru pro správný běh Android Studia a emulátoru různých typů zařizení s Androidem.
V 2. dílu jsme rozebrali MainActivity.java
V 3. dílu jsme se zabývali HeadlinesFragment.java
V tomto dílu se podíváme na ArticleFragment.java soubor.
Používáme příklad i zip porojekt z https://developer.android.com/training/basics/fragments/creating.html Pozorně si jej nastudujte.
package com.example.android.fragments;
// knihovna pro nižší verze Androidu
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
// extends Fragment - už nebude obsahovat funkci onCreate jako v Activity
// ale onCreateView
public class ArticleFragment extends Fragment {
// důležité pro uložení argumentu - argumentů (hodnot)
// pro obnovení předchozího stavu obsahu obrazovky
// např. při rotaci zařízení atd.
final static String ARG_POSITION = "position";
int mCurrentPosition = -1;
TextView article; // uložen do globální proměnné, v originale
// odchycen v updateArticleView() ale tam vracel NULL
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Když je activity znovu vytvořena (např. při rotaci zařízení),
// obnoví, v našem případě, text článku, jehož pozice
// byla uložena pomocí
// public void onSaveInstanceState(Bundle outState) viz níže
// důležité zejména pro dual-panel (dva panely vedle sebe)
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}
// umístíme, aktivujeme příslušný layout
// zde je zajímavé, že layout můžete měnit.
// Např. při kliknutí na pložku 1 v HeadlinesFragment
// zde můžete ochytit pozici a dle toho zvolit
// příslušný layout, který chcete zobrazit ve fragmentu
// ALE pak si musíte pohlídat ID prvků, které bude ten JINÝ
// layout obsahovat
// Oproti originalu odchytíme TextView již zde, v originalu to vyhazovalo chybu
View rootView = inflater.inflate(R.layout.vnitrek, container, false);
article = (TextView) rootView.findViewById(R.id.article);
return rootView;
}
@Override
public void onStart() {
super.onStart();
// Při startu fragmentu, zkontrolujte, zda existují nějaké argumenty
// předané do fragmentu.
// OnStart() je právě to správné místo, kde to udělat,
// protože layout s jednotlivými elementy byl již
// naloděn - aktivován, a můžeme bezpečně použít metody,
// které potřebují, aby jednotlivá ID elementů layoutu byla již
// aktivní, použitelná a nevracela NULL, což by mělo za následek
// pád aplikace
Bundle args = getArguments();
if (args != null) {
// vypsaní obsahu článku pomocí předaného argumentu (pozice) z HeadlinesFragment.java
updateArticleView(args.getInt(ARG_POSITION));
} else if (mCurrentPosition != -1) {
// vypsání článku dle pozice uložené např. při rotaci zařízení
// mCurrentPosition je definována (odchycena) v onCreateView
updateArticleView(mCurrentPosition);
}
}
/**
funkce která vypíše obsah článku do TextView.
Jako parametr int position je pozice položky,
na kterou bylo kliknuto v ListView v HeadlinesFragment.java
*/
public void updateArticleView(int position) {
// na rozdíl od Activity se ve Fragment používá k
// získání id ne jen findViewById()
// ALE getActivity().findViewById()
//Tento kod vracel article == NULL , PROTO bylo nutno odchytit TextView
// v onCreateView()
//TextView article = (TextView) getActivity().findViewById(R.id.article);
// vložení textu článku do TextView z Ipsum.java
// je to pole stringů, kde position je pozice stringu v poli
// static String[] Articles = {"","",""};
if (article != null)
article.setText(Ipsum.Articles[position]);
mCurrentPosition = position;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Uložení pozice článku - elementu, či jiných argumentů důležitých
// pro obnovení stavu v onCreateView() např. při rotaci zařízení
outState.putInt(ARG_POSITION, mCurrentPosition);
// TIP: zde můžeme vždy při rotaci zařízení podstrčit náhodnou pozici
// článku pomocí
// randomNum = minimum + (int)(Math.random() * maximum);
// a vytvořit tak zábavnou hru, například pro náhodné
// vypsání přísloví, či nějakého fyzikálního zákona atd.
// Stačí pak aby uživatel jen pootočil zařízení od 90° a zpět,
// k vypsání nové položky
}
}
Hashtable find value by key Java Android basic example.
import java.util.Hashtable;
public class MainClass {
public static void main(String[] arg) {
// english;germany dictionary
String[] arrayOfString = { "one;eine", "two;zwei", "three;drei" };
Hashtable hashTable = new Hashtable();
for(String s: arrayOfString){
String[] array = s.split(";");
String sKey ="", sValue="";
if(array.length > 1){
sKey = array[0]; sValue = array[1];
hashTable.put(sKey, sValue);
}
}
// check if key exists
if( hashTable.containsKey("two")){
System.out.print("two = " + hashTable.get("two"));
}
}
}
/*
two = zwei
*/
Where file(s) does eclipse store Java code style settings to?
If you need copy any old templates to the new workspace.
You can edit the Eclipse Templates from menu Window - Preferences - Java - Editor - Templates.
If you need copy any old templates to the new workspace.
workspace\.metadata\.plugins\org.eclipse.core.runtime\.settings\org.eclipse.jdt.ui.prefs
You can edit the Eclipse Templates from menu Window - Preferences - Java - Editor - Templates.
Editace: 2013-12-09 10:14:50
Počet článků v kategorii: 396
Url:android-onclicklistener-must-override-a-superclass-method