我编写了我的应用程序以使用listview,我创建了我的addapter类来扩展ArrayAddapter,并且我创建了我的getter类,以便将我的文字放入我想要膨胀的视图中,但是当我运行应用程序时它会停止。
我的addapter类代码
package charpman.com.quakereport;
import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by DCharpMan on 4/19/2017.
* <p>
* class to populate each list view item
*/
public class QuakeAdapter extends ArrayAdapter<earthquakeClass> {
/**
* Constructor
*
* @param context The current context.
* @param earthquakeClass the array to be populated
*/
public QuakeAdapter(@NonNull Context context, ArrayList<earthquakeClass> earthquakeClass) {
super(context, R.layout.description_house, earthquakeClass);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View Customize = convertView;
if (Customize == null) {
Customize = LayoutInflater.from(getContext()).inflate(R.layout.description_house, parent, false);
}
earthquakeClass earthquakeClass = getItem(position);
TextView textView = (TextView) Customize.findViewById(R.id.text_magnitude);
textView.setText(earthquakeClass.getMagnitude());
textView.setTypeface(Typeface.MONOSPACE, 2 + 1);
TextView textView1 = (TextView) Customize.findViewById(R.id.text_loctaion);
textView1.setText(earthquakeClass.getLocation());
textView1.setTypeface(Typeface.SERIF, 1);
TextView textView2 = (TextView) Customize.findViewById(R.id.text_daye);
textView2.setText(earthquakeClass.getDate());
textView2.setTypeface(Typeface.DEFAULT_BOLD, 3);
return Customize;
}
}
我的mainActivity.java代码
package charpman.com.quakereport;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<earthquakeClass> country = new ArrayList<earthquakeClass>();
country.add(new earthquakeClass("7.2", "nigeria", "23,07,2017"));
country.add(new earthquakeClass("7.2", "nigeria", "23,07,2017"));
country.add(new earthquakeClass("7.2", "nigeria", "23,07,2017"));
country.add(new earthquakeClass("7.2", "nigeria", "23,07,2017"));
country.add(new earthquakeClass("7.2", "nigeria", "23,07,2017"));
ListView listView = (ListView) findViewById(R.id.list_view);
QuakeAdapter quakeAdapter = new QuakeAdapter(this, country);
listView.setAdapter(quakeAdapter);
}
}
我的getter类代码
package charpman.com.quakereport;
/**
* Created by CharpMan on 4/19/2017.
* class to get the earthQuake details
* details includes Magnitude,location and date of earthquake
*/
public class earthquakeClass {
// globale variables that holds the earthquake information
String magnitude;
String location;
String date;
//contructor for the class takes in three params
// @param Magnitude tell the magnitude of the quake
// @params Location tells the location where the quake occured
// @params date tells the day of the quake
public earthquakeClass(String Magnitude, String Location, String Date){
magnitude = Magnitude;
location = Location;
date = Date;
}
// create public methods that returns each information
// method to the the magnitude
public String getMagnitude(){
return magnitude;
}
// methid to return the loctaion
public String getLocation(){
return location;
}
// methid to return the day of quake occurence
public String getDate (){
return date;
}
}
我的MainActivity.xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="charpman.com.quakereport.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@android:color/darker_gray"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="Magnitude"
android:textAllCaps="true" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:paddingLeft="10dp"
android:text="Location"
android:textAllCaps="true" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:paddingLeft="20dp"
android:text="date"
android:textAllCaps="true" />
</LinearLayout>
<ListView
android:background="@android:color/secondary_text_light"
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
我在Addapter类的GetView方法中膨胀的布局的xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/text_magnitude"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/white"
android:paddingLeft="14dp"
android:text="7.8" />
<TextView
android:id="@+id/text_loctaion"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/white"
android:gravity="center"
android:paddingLeft="5dp"
android:text="Nigeria"
android:typeface="serif" />
<TextView
android:id="@+id/text_daye"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:background="@android:color/white"
android:paddingLeft="25dp"
android:text="28,02,2017" />
</LinearLayout>
</LinearLayout>
的屏幕截图
答案 0 :(得分:0)
我试图阅读代码,我注意到我的ListView代码中添加了一个背景标签
机器人:背景= “@机器人:颜色/ secondary_text_light”
我应该在项目colors.xml中定义/复制颜色代码并使用它。感谢
答案 1 :(得分:0)
根据错误日志,我认为在您的XML文件中存在根据您的适配器不兼容的东西,或者您在适配器中使用了XML文件中不存在的对象。