用android studio做一个汽车推荐app的实现代码
时间: 2023-03-09 21:59:51 浏览: 213
要实现一个汽车推荐的app,你可以使用Android Studio来构建一个汽车推荐的搜索引擎,这个搜索引擎可以把所有汽车的信息收集到一个数据库中,然后提供用户搜索的功能,最后根据用户的搜索结果,返回推荐的汽车信息。
相关问题
用android studio做一个日记app的实现代码
以下是一个简单的日记App的实现代码,使用了SQLite数据库存储数据:
1. activity_main.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">
<EditText
android:id="@+id/titleEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title" />
<EditText
android:id="@+id/contentEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Content" />
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
<ListView
android:id="@+id/diaryListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
```
2. Diary.java
```
public class Diary {
private int id;
private String title;
private String content;
private String date;
public Diary(int id, String title, String content, String date) {
this.id = id;
this.title = title;
this.content = content;
this.date = date;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
```
3. DiaryAdapter.java
```
public class DiaryAdapter extends BaseAdapter {
private Context context;
private List<Diary> diaryList;
public DiaryAdapter(Context context, List<Diary> diaryList) {
this.context = context;
this.diaryList = diaryList;
}
@Override
public int getCount() {
return diaryList.size();
}
@Override
public Object getItem(int position) {
return diaryList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.diary_item_layout, parent, false);
}
TextView titleTextView = convertView.findViewById(R.id.titleTextView);
TextView contentTextView = convertView.findViewById(R.id.contentTextView);
TextView dateTextView = convertView.findViewById(R.id.dateTextView);
Diary diary = diaryList.get(position);
titleTextView.setText(diary.getTitle());
contentTextView.setText(diary.getContent());
dateTextView.setText(diary.getDate());
return convertView;
}
}
```
4. MainActivity.java
```
public class MainActivity extends AppCompatActivity {
private EditText titleEditText, contentEditText;
private Button saveButton;
private ListView diaryListView;
private List<Diary> diaryList;
private DiaryAdapter diaryAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titleEditText = findViewById(R.id.titleEditText);
contentEditText = findViewById(R.id.contentEditText);
saveButton = findViewById(R.id.saveButton);
diaryListView = findViewById(R.id.diaryListView);
diaryList = new ArrayList<>();
diaryAdapter = new DiaryAdapter(this, diaryList);
diaryListView.setAdapter(diaryAdapter);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = titleEditText.getText().toString().trim();
String content = contentEditText.getText().toString().trim();
if (TextUtils.isEmpty(title) || TextUtils.isEmpty(content)) {
Toast.makeText(MainActivity.this, "Title and content cannot be empty", Toast.LENGTH_SHORT).show();
return;
}
Diary diary = new Diary(0, title, content, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date()));
DiaryDBHelper dbHelper = new DiaryDBHelper(MainActivity.this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("title", diary.getTitle());
values.put("content", diary.getContent());
values.put("date", diary.getDate());
db.insert("diary", null, values);
diaryList.add(diary);
diaryAdapter.notifyDataSetChanged();
titleEditText.setText("");
contentEditText.setText("");
}
});
DiaryDBHelper dbHelper = new DiaryDBHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("diary", null, null, null, null, null, "id desc");
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String content = cursor.getString(cursor.getColumnIndex("content"));
String date = cursor.getString(cursor.getColumnIndex("date"));
Diary diary = new Diary(id, title, content, date);
diaryList.add(diary);
} while (cursor.moveToNext());
}
cursor.close();
diaryAdapter.notifyDataSetChanged();
}
}
```
5. DiaryDBHelper.java
```
public class DiaryDBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "diary.db";
private static final int DB_VERSION = 1;
public DiaryDBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE diary (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, content TEXT, date TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS diary");
onCreate(db);
}
}
```
6. diary_item_layout.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="10dp">
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/contentTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp" />
<TextView
android:id="@+id/dateTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
```
用Android Studio做一个天气预报app详细代码
这是一个简单的天气预报app的示例代码,使用了OpenWeatherMap的API获取天气数据。你需要先注册OpenWeatherMap账号获取API Key。
1. 布局文件
在`res/layout`目录下创建一个名为`activity_main.xml`的布局文件,添加以下代码:
```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">
<TextView
android:id="@+id/city_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Loading..." />
<TextView
android:id="@+id/temp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Loading..." />
<TextView
android:id="@+id/weather_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Loading..." />
</LinearLayout>
```
2. Java代码
在`app/src/main/java`目录下创建一个名为`MainActivity.java`的Java类,添加以下代码:
```java
public class MainActivity extends AppCompatActivity {
private static final String API_KEY = "YOUR_API_KEY"; // 替换成你的API Key
private static final String API_URL = "https://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=metric";
private TextView cityNameTextView;
private TextView tempTextView;
private TextView weatherDescTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cityNameTextView = findViewById(R.id.city_name);
tempTextView = findViewById(R.id.temp);
weatherDescTextView = findViewById(R.id.weather_desc);
String city = "beijing"; // 替换成你要查询的城市
String apiUrl = String.format(API_URL, city, API_KEY);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, apiUrl, null,
response -> {
try {
JSONObject main = response.getJSONObject("main");
JSONArray weatherArray = response.getJSONArray("weather");
JSONObject weather = weatherArray.getJSONObject(0);
String cityName = response.getString("name");
String temp = main.getString("temp") + " °C";
String weatherDesc = weather.getString("description");
cityNameTextView.setText(cityName);
tempTextView.setText(temp);
weatherDescTextView.setText(weatherDesc);
} catch (JSONException e) {
e.printStackTrace();
}
}, error -> {
Toast.makeText(this, "Failed to fetch weather data", Toast.LENGTH_SHORT).show();
Log.e("MainActivity", "Failed to fetch weather data", error);
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
}
}
```
注意替换代码中的`YOUR_API_KEY`和`city`变量。
3. 运行app
运行app并输入查询的城市名称,即可获取天气数据。
阅读全文