Android studio 做一个新闻发布和查询页面代码
时间: 2024-05-06 21:18:08 浏览: 99
Android Studio项目源码---查看新闻app
5星 · 资源好评率100%
好的,下面是一个简单的新闻发布和查询页面代码:
1. 创建一个新的Android项目,并在布局文件中添加以下代码:
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/titleEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="标题" />
<EditText
android:id="@+id/contentEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="内容" />
<Button
android:id="@+id/submitBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发布" />
<EditText
android:id="@+id/searchEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="搜索关键字" />
<Button
android:id="@+id/searchBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询" />
<ListView
android:id="@+id/newsListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
```
2. 在MainActivity中添加以下代码:
```
public class MainActivity extends AppCompatActivity {
private EditText titleEditText;
private EditText contentEditText;
private Button submitBtn;
private EditText searchEditText;
private Button searchBtn;
private ListView newsListView;
private List<News> newsList = new ArrayList<>();
private NewsAdapter newsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titleEditText = findViewById(R.id.titleEditText);
contentEditText = findViewById(R.id.contentEditText);
submitBtn = findViewById(R.id.submitBtn);
searchEditText = findViewById(R.id.searchEditText);
searchBtn = findViewById(R.id.searchBtn);
newsListView = findViewById(R.id.newsListView);
// 设置ListView适配器
newsAdapter = new NewsAdapter(MainActivity.this, newsList);
newsListView.setAdapter(newsAdapter);
// 发布新闻
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = titleEditText.getText().toString();
String content = contentEditText.getText().toString();
// 将新闻添加到newsList中
News news = new News(title, content);
newsList.add(news);
// 刷新ListView
newsAdapter.notifyDataSetChanged();
// 清空输入框
titleEditText.setText("");
contentEditText.setText("");
}
});
// 查询新闻
searchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String keyword = searchEditText.getText().toString();
// 根据关键字查询新闻
List<News> resultList = new ArrayList<>();
for (News news : newsList) {
if (news.getTitle().contains(keyword) || news.getContent().contains(keyword)) {
resultList.add(news);
}
}
// 刷新ListView
newsAdapter.setData(resultList);
newsAdapter.notifyDataSetChanged();
}
});
}
}
```
3. 创建一个News类,用于表示新闻:
```
public class News {
private String title;
private String content;
public News(String title, String content) {
this.title = title;
this.content = content;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
```
4. 创建一个NewsAdapter类,用于将新闻显示在ListView中:
```
public class NewsAdapter extends BaseAdapter {
private Context context;
private List<News> data;
public NewsAdapter(Context context, List<News> data) {
this.context = context;
this.data = data;
}
public void setData(List<News> data) {
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_news, parent, false);
viewHolder = new ViewHolder();
viewHolder.titleTextView = convertView.findViewById(R.id.titleTextView);
viewHolder.contentTextView = convertView.findViewById(R.id.contentTextView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
News news = data.get(position);
viewHolder.titleTextView.setText(news.getTitle());
viewHolder.contentTextView.setText(news.getContent());
return convertView;
}
private static class ViewHolder {
TextView titleTextView;
TextView contentTextView;
}
}
```
5. 在布局文件中添加一个item_news.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="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/contentTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textSize="16sp" />
</LinearLayout>
```
这样就完成了一个简单的新闻发布和查询页面。
阅读全文