我的项目有效,但ListView
没有显示任何内容!
我的代码出了什么问题?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:orientation="vertical"
android:padding="6dp">
<TextView
android:id="@+id/textViewTitle"
style="@style/styleText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/titleAddition"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#EAF7C0" />
<ListView
android:id="@+id/listViewExamples"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="6dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#EAF7C0"
/>
<TextView
android:id="@+id/textViewCalculate"
style="@style/styleText"
android:paddingTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom|center"
android:text="@string/Addition1"
android:paddingBottom="5dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center"
android:orientation="horizontal">
<EditText
android:id="@+id/Num1"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
android:hint="@string/zero"
android:inputType="number"
android:textSize="24sp" />
<TextView
android:id="@+id/textViewSign"
style="@style/styleText"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
android:text="@string/AdditionMark"
android:textSize="28sp" />
<EditText
android:id="@+id/Num2"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
android:hint="@string/zero"
android:inputType="number"
android:textSize="24sp" />
<TextView
style="@style/styleText"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
android:text="@string/eqwal"
android:textSize="28sp" />
<TextView
android:id="@+id/textViewTotal"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
android:hint="@string/zero"
android:textSize="24sp" />
</LinearLayout>
<Button
android:id="@+id/buttonResult"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="0"
android:background="#EAF7C0"
android:onClick="result"
android:text="@string/result"
android:textAppearance="@android:style/TextAppearance" />
</LinearLayout>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/background"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageViewQuality"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:scaleType="fitXY"
app:srcCompat="@drawable/additionex1"
/>
<LinearLayout
android:id="@+id/qualityColor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textViewQualty"
style="@style/styleText2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|left"
android:paddingLeft="3dp"
android:text="@string/sum"
/>
</LinearLayout>
这些是课程。
public class Examples {
private String textQuality;
private int imageResource;
public Examples(String textQuality, int imageResource) {
this.textQuality = textQuality;
this.imageResource = imageResource;
}
public String getTextQuality() {
return textQuality;
}
public int getImageResource() {
return imageResource;
}
}
public class ExampleAdapter extends ArrayAdapter<Examples> {
private int mColorResourceId;
public ExampleAdapter(@NonNull Activity context, ArrayList<Examples> examples , int mColorResourceId) {
super(context,0, mColorResourceId);
this.mColorResourceId = mColorResourceId;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.exampl_activity, parent, false);
}
Examples currentExample = getItem(position);
TextView textView = (TextView) listItemView.findViewById(R.id.textViewQualty);
textView.setText(currentExample.getTextQuality());
ImageView imageView = (ImageView) listItemView.findViewById(R.id.imageViewQuality);
imageView.setImageResource(currentExample.getImageResource());
View textContainer = listItemView.findViewById(R.id.qualityColor);
// Find the color that the resource ID maps to
int color = ContextCompat.getColor(getContext(), mColorResourceId);
// Set the background color of the text container View
textContainer.setBackgroundColor(color);
return listItemView;
}}
public class Addition extends AppCompatActivity {
TextView titleTextView;
TextView calculateText;
EditText num1;
EditText num2;
TextView textMark;
TextView total;
Button button;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
titleTextView = (TextView) findViewById(R.id.textViewTitle);
calculateText = (TextView) findViewById(R.id.textViewCalculate);
textMark = (TextView) findViewById(R.id.textViewSign);
button = (Button) findViewById(R.id.buttonResult);
listView = (ListView) findViewById(R.id.listViewExamples);
titleTextView.setText(getString(R.string.titleAddition));
calculateText.setText(getString(R.string.Addition1));
titleTextView.setText(R.string.titleAddition);
calculateText.setText(R.string.Addition1);
textMark.setText(R.string.AdditionMark);
button.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(View v) {
num1 = (EditText) findViewById(R.id.Num1);
num2 = (EditText) findViewById(R.id.Num2);
total = (TextView) findViewById(R.id.textViewTotal);
int Num1 = Integer.parseInt(num1.getText().toString());
int Num2 = Integer.parseInt(num2.getText().toString());
int val = Num1 + Num2;
total.setText(Integer.toString(val));
}
});
final ArrayList<Examples> arrayList = new ArrayList<Examples>();
arrayList.add(new Examples("5 + 0 = 5", R.drawable.additionex1));
arrayList.add(new Examples("1 + 1 = 2", R.drawable.additionex2));
arrayList.add(new Examples("3 + 3 = 6", R.drawable.additionex3));
arrayList.add(new Examples("2 + 7 = 9", R.drawable.additionex4));
arrayList.add(new Examples("5 + 3 = 8", R.drawable.additionex5));
arrayList.add(new Examples("6 + 7 = 13", R.drawable.additionex6));
arrayList.add(new Examples("9 + 2 = 11", R.drawable.additionex7));
arrayList.add(new Examples("8 + 2 = 10", R.drawable.additionex8));
ExampleAdapter adapter = new ExampleAdapter(this,arrayList,R.color.colorAddition);
listView.setAdapter(adapter);
}
}
我没有在代码中看到任何错误,只是Logcat中的这条消息:
E / OpenGLRenderer:从GradienCache获取MAX_TEXTURE_SIZE MAX_TEXTURE_SIZE:16384 E / OpenGLRenderer:从Caches :: initConstraints()获取MAX_TEXTURE_SIZE MAX_TEXTURE_SIZE:16384
请帮帮我,抱歉我不会说英语。