Difference between “@+id/” and “@id/” in Android
Difference between "@+id/” and "@id/” in Android
android:id="@+id/xxx" unique identifier of view
@id/ a reference to the unique identifier
android:id="@+id/xxx" unique identifier of view
@id/ a reference to the unique identifier
<TextView
android:id="@+id/first_element_id"
.........
/>
<TextView
android:id="@+id/second_element_id"
android:layout_below="@id/first_element_id"
..........
/>
396LW NO topic_id
AD
Další témata ....(Topics)
How add item to ArrayList, sort ArrayList, search find index of item in ArrayList, min(), max() Java basic example.
import java.util.ArrayList;
import java.util.Collections;
public class MainClass {
public static void main(String[] arg) {
String[] arrayOfString = {"nothing", "Hello", "people"
, "bye-bye", "hello", "world!", "End" };
ArrayList<String> arrayList = new ArrayList<String>();
for(String s: arrayOfString)
arrayList.add(s);
Collections.sort(arrayList);
// foreach
for (String str: arrayList)
System.out.println(str);
Object objMin = Collections.min(arrayList);
System.out.println("Min is: " + objMin);
Object objMax = Collections.max(arrayList);
System.out.println("Max is: " + objMax);
int index = Collections.binarySearch(arrayList, "people");
System.out.println("Index of people is: " + index);
}
}
/*
End
Hello
bye-bye
hello
nothing
people
world!
Min is: End
Max is: world!
Index of people is: 5
*/
drawText(), setColor(), setTextSize(), setTextAlign()
public class ApokusActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static class SampleView extends View {
// CONSTRUCTOR
public SampleView(Context context) {
super(context);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.YELLOW);
paint.setTextSize(60);
//paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText("Hello world!", 0, 50, paint);
}
}
}
Problem in Android application:
Solution: check code for set and get selection
E/AndroidRuntime(416): FATAL EXCEPTION: main
E/AndroidRuntime(416): java.lang.IndexOutOfBoundsException
E/AndroidRuntime(416): at java.util.Arrays$ArrayList.get(Arrays.java:75)
E/AndroidRuntime(416): at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:298)
E/AndroidRuntime(416): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:351)
E/AndroidRuntime(416): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
E/AndroidRuntime(416): at android.widget.Spinner.makeAndAddView(Spinner.java:192)
E/AndroidRuntime(416): at android.widget.Spinner.layout(Spinner.java:151)
E/AndroidRuntime(416): at android.widget.Spinner.onLayout(Spinner.java:115)
Solution: check code for set and get selection
// Spinner _spin1 contain only 49 items
// you can set max 48 ( range 0 - 48)
// 50 is IndexOutOfBoundsException
_spin1.setSelection(50);
You can use for, do while, while cycle for example:
public void myFunction(){
for (int i = 0; i < 1; i++) {
// some code
int c = 10;
if(c==10)
break; // goto stop; in C++
} // end of for
// stop: // break moved process to end of for
// next code
}
public class Main extends Activity {
private TextView mTextView;
private Activity mAct;
private Intent mIntent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
mTextView = findViewById(R.id.mTextView);
mAct = getActivity();
mIntent = getIntent();
}
}
to:
public class Main extends Fragment{
private TextView mTextView;
private FragmentActivity mFrgAct;
private Intent mIntent;
private LinearLayout mLinearLayout;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_main, null);
return root;
}
public void onViewCreated(View view, Bundle savedInstanceState) {
// you can add listener of elements here
/*Button mButton = (Button) view.findViewById(R.id.button);
mButton.setOnClickListener(this); */
mTextView = view.findViewById(R.id.mTextView);
mLinearLayout = (LinearLayout)view;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mFrgAct = getActivity();
mIntent = mFrgAct.getIntent(); // Intent intent = new Intent(getActivity().getIntent());
}
}
Editace: 2015-12-01 11:31:06
Počet článků v kategorii: 396
Url:difference-between-id-and-id-in-android