我想知道为什么它告诉我:“自定义列表视图已停止”吗?我该如何解决?
这是我在“主要活动”中的代码
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:layoutDirection="rtl"
android:padding="16dp"
android:orientation="vertical"
tools:context="customlistviews.oxbir.com.customlistview.ActivityMain">
<EditText
android:id="@+id/editTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnAdd"
android:text="Add Row"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
ActivityMain.java
package customlistviews.oxbir.com.customlistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class ActivityMain extends AppCompatActivity {
Button btnAdd;
EditText editTitle;
ArrayList<News> newsArrayList;
ListView listView = (ListView)findViewById(R.id.listView);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTitle = (EditText)findViewById(R.id.editTitle);
btnAdd = (Button)findViewById(R.id.btnAdd);
newsArrayList = new ArrayList<>();
final NewsAdapter newsAdapter = new NewsAdapter(ActivityMain.this, R.layout.activity_customnews, newsArrayList);
listView.setAdapter(newsAdapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newsArrayList.add(new News(R.drawable.newsletter, editTitle.getText().toString()));
newsAdapter.notifyDataSetChanged();
}
});
}
}
activity_customnews.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:layoutDirection="rtl">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageNews"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/newsletter" />
<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_gravity="center"
android:textSize="20dp"
android:text="Title" />
</LinearLayout>
</LinearLayout>
News.java
package customlistviews.oxbir.com.customlistview;
public class News {
public int imageNews;
public String titleNews;
public News (int imageNews, String titleNews) {
this.imageNews = imageNews;
this.titleNews = titleNews;
}
}
NewsAdapter.java
package customlistviews.oxbir.com.customlistview;
import android.app.Activity;
import android.media.Image;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class NewsAdapter extends ArrayAdapter {
public int resourceID;
public Activity activity;
public ArrayList<News> data;
public NewsAdapter(Activity activity, int resourceID, ArrayList<News> object) {
super(activity, resourceID, object);
this.resourceID = resourceID;
this.activity = activity;
this.data = object;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
view = this.activity.getLayoutInflater().inflate(this.resourceID, null);
ImageView imageView = (ImageView) view.findViewById(R.id.imageNews);
TextView txtTitle = (TextView) view.findViewById(R.id.txtTitle);
News news = data.get(position);
imageView.setImageResource(R.drawable.newsletter);
txtTitle.setText(news.titleNews);
return view;
}
}
现在,我只是想在文本字段中写一些东西,每次单击按钮后,我都会在列表视图中显示照片和文本。