Я новичок в Android Development. Я создал приложение, которое будет отображать соответствующее текстовое представление при нажатии на текстовое представление.
Для этого я дал условное выражение if-else-if для отображения текста.Поскольку у меня много текстовых обзоров (около 100), при создании проекта андроид-студия говорит, что CODE IS LONG.
Как решить эту проблему.Можно ли использовать MAP для перебора IDS textview?
Пожалуйста, помогите мне.
public void click(View v) {
switch (v.getId()) {
case R.id.fam:
// check if a child is set to a specific String
String toSearchFor = "badaatext";
boolean found = false;
ArrayList<View> allViewsWithinMyTopView = getAllChildren(findViewById(R.id.linear));
for (View child : allViewsWithinMyTopView) {
if (child instanceof TextView) {
TextView childTextView = (TextView) child;
int intName = childTextView.getId();
System.out.println("The Child TextView is : " + childTextView.getResources().getResourceEntryName(intName));
if (childTextView.getResources().getResourceEntryName(intName).startsWith("badaatext")) {
found = true;
System.out.println("The text is foudn inside the layout");
System.out.println("And the Textview ID is :" + childTextView.getResources().getResourceEntryName(intName));
childTextView.setVisibility(View.GONE);
String BadaaString = childTextView.getResources().getResourceEntryName(intchildName);
System.out.println("The Big TextView is : " + BadaaString);
String ChotaString = BadaaString.substring(9, 12);
System.out.println("The Small Textview is : " + ChotaString);
if (ChotaString.equalsIgnoreCase("rel")) {
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.rel);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("nam")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.nam);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("mom")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.mom);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("dad")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.dad);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("elb")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.elb);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("els")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.els);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("yos")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.yos);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("sil")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.sil);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("yob")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.yob);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("hus")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.hus);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("wif")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.wif);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("fre")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.fre);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("tea")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.tea);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("ser")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.ser);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("mai")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.mai);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("wom")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.wom);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("stm")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.stm);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("man")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.man);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("boy")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.boy);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("gir")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.gir);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("gue")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.gue);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("cus")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.cus);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("hoi")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.hoi);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("gif")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.gif);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("son")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.son);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("dau")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.dau);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("gpa")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.gpa);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("gma")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.gma);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("gdp")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.gdp);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("gdm")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.gdm);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("und")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.und);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("aud")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.aud);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("maa")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.maa);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("mam")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.mam);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("mad")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.mad);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("ata")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.ata);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("fla")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.fla);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("mla")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.mla);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("sod")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.sod);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("gru")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.gru);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("som")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.som);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("grm")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.grm);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("unp")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.unp);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("aup")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.aup);
Engtxt1.setVisibility(View.VISIBLE);
}else{
System.out.println("No Matches found");
}
}
}
}
if (!found) {
//fail("Text '" + The text is not available in the layout");
System.out.println("The TextView is not present inside the layout");
}
//Make the Clicked View to be Invisible
TextView Engfam = (TextView) findViewById(R.id.fam);
Engfam.setVisibility(View.GONE);
//Adding the corresponding dynamic textview
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear);
linearLayout.setOrientation(LinearLayout.VERTICAL);
/* Add textview 1 */
TextView textViewFamily = new TextView(lessonSix.this);
// textView1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
// LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams textviewLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(textViewFamily, 100,600, 2, TypedValue.COMPLEX_UNIT_SP);
textviewLayoutParams.setMargins(0, 9, 0, 0);
// linearLayout.addView(textView1, 1, textviewLayoutParams);
textViewFamily.setId(R.id.badaatextFam);
textViewFamily.setLayoutParams(textviewLayoutParams);
textViewFamily.setBackgroundResource(R.drawable.shapelineinsideroundedrect
);
textViewFamily.setText("FAMILY - குடும்பம்");
textViewFamily.setGravity(Gravity.TOP | Gravity.CENTER);
textViewFamily.setTextSize(20);
textViewFamily.setTypeface(null, Typeface.BOLD);
textViewFamily.setPadding(0, 25, 0, 25);
textViewFamily.setVisibility(View.VISIBLE);
linearLayout.addView(textViewFamily, 1);
break;
case R.id.gir:
// check if a child is set to a specific String
found = false;
ArrayList<View> allViewsWithinMyTopView21 = getAllChildren(findViewById(R.id.linear));
for (View child : allViewsWithinMyTopView21) {
if (child instanceof TextView) {
childTextView = (TextView) child;
intchildName = childTextView.getId();
System.out.println("The Child TextView is : " + childTextView.getResources().getResourceEntryName(intchildName));
if (childTextView.getResources().getResourceEntryName(intchildName).startsWith("badaatext")) {
found = true;
System.out.println("The text is foudn inside the layout");
System.out.println("And the Textview ID is :" + childTextView.getResources().getResourceEntryName(intchildName));
childTextView.setVisibility(View.GONE);
String BadaaString = childTextView.getResources().getResourceEntryName(intchildName);
System.out.println("The Big TextView is : " + BadaaString);
String ChotaString = BadaaString.substring(9, 12);
System.out.println("The Small Textview is : " + ChotaString);
//if (ChotaString.equalsIgnoreCase("fam")) {
// //Display the Small TextView:
// TextView Engtxt1 = (TextView) findViewById(R.id.fam);
//Engtxt1.setVisibility(View.VISIBLE);
//}
int textViewId = context.getResources().getIdentifier(ChotaString, "id", context.getPackageName());
TextView Engtxt1 = (TextView) findViewById(textViewId);
Engtxt1.setVisibility(View.VISIBLE);
}
}
}
if (!found) {
//fail("Text '" + The text is not available in the layout");
System.out.println("The TextView is not present inside the layout");
}
//Make the Clicked View to be Invisible
TextView Enggir = (TextView) findViewById(R.id.gir);
Enggir.setVisibility(View.GONE);
//Adding the corresponding dynamic textview
LinearLayout linearLayout21 = (LinearLayout) findViewById(R.id.linear);
linearLayout21.setOrientation(LinearLayout.VERTICAL);
/* Add textview 1 */
TextView textViewGirl = new TextView(lessonSix.this);
//textView1.setLayoutParams(new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
//LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams textviewLayoutParams21 = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(textViewGirl, 100,600, 2, TypedValue.COMPLEX_UNIT_SP);
textviewLayoutParams21.setMargins(0, 9, 0, 0);
//linearLayout.addView(textView1, 1, textviewLayoutParams);s
textViewGirl.setId(R.id.badaatextGir);
textViewGirl.setLayoutParams(textviewLayoutParams21);
textViewGirl.setBackgroundResource(R.drawable.shapelineinsideroundedrect);
textViewGirl.setText("GIRL - சிறுமி");
textViewGirl.setGravity(Gravity.TOP | Gravity.CENTER);
textViewGirl.setTextSize(20);
textViewGirl.setTypeface(null, Typeface.BOLD);
textViewGirl.setPadding(0, 25, 0, 25);
textViewGirl.setVisibility(View.VISIBLE);
linearLayout21.addView(textViewGirl, 40);
break;
Соответствующий код XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue"
tools:context=".lessonSix">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--<TextView-->
<!--android:id="@+id/badaatextOne"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="130dp"-->
<!--android:layout_marginTop="5dp"-->
<!--android:paddingTop="25dp"-->
<!--android:background="@drawable/shapelineinsideroundedrect"-->
<TextView
android:id="@+id/fam"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_marginTop="5dp"
android:background="@drawable/shaperoundedcornerrect"
android:text="FAMILY"
android:onClick="click"
android:clickable="true"
android:gravity="center"
android:textSize="20dip"
android:textStyle="bold"
tools:ignore="OnClick" />
<TextView
android:id="@+id/rel"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_marginTop="5dp"
android:background="@drawable/shaperoundedcornerrect"
android:text="RELATION"
android:clickable="true"
android:onClick="click"
android:gravity="center"
android:textSize="20dip"
android:textStyle="bold" />
<TextView
android:id="@+id/gir"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_marginTop="5dp"
android:background="@drawable/shaperoundedcornerrect"
android:text="GIRL"
android:clickable="true"
android:onClick="click"
android:gravity="center"
android:textSize="20dip"
android:textStyle="bold"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Добавление исключения: FATAL EXCEPTION: main Процесс: com.seekho.noushad.dictionary, PID: 30013 java.lang.IllegalStateException: Не удалось выполнить метод для android: onClick at android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick (AppCompatViewInflater.java:293) в android.view.View.ViewformClick (Java: 4788) на android.view.View $ PerformClick.run (View.java:19923) на android.os.Handler.handleCallback (Handler.java:739) на android.os.Handler.dispatchMessage (Handler.java:95) на android.os.Looper.loop (Looper.java:135) на android.app.ActivityThread.main (ActivityThread.java:5401) на java.lang.reflect.Method.invoke (собственный метод) на java.lang.refle.Method.invoke (Method.java:372) в com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:919) в com.android.internal.os.ZygoteInit.main (ZygoteInit.java Причиненныйавтор: java.lang.reflect.InvocationTargetException Applicдействие прекращено.
New Log(Exception):
D / AndroidRuntime: завершение работы VM E / AndroidRuntime: FATAL EXCEPTION: main Процесс: com.seekho.noushad.dictionary, PID: 24918 java.lang.IllegalStateException: не удалось выполнить методдля android: onClick на android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick (AppCompatViewInflater.java:293) на android.view.View.performClick (View.java:4788) на android.view.View $ PerformClick.run(View.java:19923) на android.os.Handler.handleCallback (Handler.java:739) на android.os.Handler.dispatchMessage (Handler.java:95) на android.os.Looper.loop (Looper.java:135) на android.app.ActivityThread.main (ActivityThread.java:5401) на java.lang.reflect.Method.invoke (собственный метод) на java.lang.reflect.Method.invoke (Method.java:372) на ком.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:919) в com.android.internal.os.ZygoteInit.main (ZygoteInit.java:714) Вызвано: java.lang.reflect.InvocationTarget.lang.reflect.Method.invoke (собственный метод) в java.lang.reflect.Method.invoke (Method.java:372) в android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick (AppCompatViewInflater.java:288) в android.view.View.performClick (View.java:4788) на android.view.View $ PerformClick.run (View.java:19923) на android.os.Handler.handleCallback (Handler.java:739) на android.os.Handler.dispatchMessage(Handler.java:95) в android.os.Looper.loop (Looper.java:135) в android.app.ActivityThread.main (ActivityThread.java:5401) в java.lang.reflect.Method.invoke (собственный метод) в java.lang.reflect.Method.invoke (Method.java:372) в com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:919) в com.android.internal.os.ZygoteInit.Основной (ZygoteInit.java:714)Вызывается: java.lang.NullPointerException: попытка вызвать виртуальный метод 'android.content.res.Resources android.content.Context.getResources ()' для ссылки на пустой объект в com.seekho.noushad.dictionary.lessonSix.click (lessonSix.java:4893) в java.lang.reflect.Method.invoke (собственный метод) в java.lang.reflect.Method.invoke (Method.java:372) в android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick (AppCompatViewInflater.java:288) на android.view.View.performClick (View.java:4788) на android.view.View $ PerformClick.run (View.java:19923) на android.os.Handler.handleCallback (обработчик).java: 739) на android.os.Handler.dispatchMessage (Handler.java:95) на android.os.Looper.loop (Looper.java:135) на android.app.ActivityThread.main (ActivityThread.java:5401)в java.lang.reflect.Method.invoke (собственный метод) в java.lang.reflect.Method.invoke (Method.java:372) в com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:919) на com.android.internal.os.ZygoteInit.main (ZygoteInit.java:714) I / Процесс: отправка сигнала.PID: 24918 SIG: 9 Приложение прекращено.