今天完成了列表与适配器的相关操作:
示意图:
Drink:
package bjfu.it.sun.starbuzz; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; public class TopLevelActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_top_level); //实现onItemClickListener,点击时打开二级界面 AdapterView.OnItemClickListener itemClickListener= new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> listView, View itemView, int position, long id) { if (position == 0){ Intent intent = new Intent(TopLevelActivity.this, DrinkCategoryActivity.class); startActivity(intent); } } }; //为listView注册单机监听器 ListView listView=findViewById(R.id.list_options); listView.setOnItemClickListener(itemClickListener); } }
DrinkActivity:
package bjfu.it.sun.starbuzz; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; public class TopLevelActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_top_level); //实现onItemClickListener,点击时打开二级界面 AdapterView.OnItemClickListener itemClickListener= new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> listView, View itemView, int position, long id) { if (position == 0){ Intent intent = new Intent(TopLevelActivity.this, DrinkCategoryActivity.class); startActivity(intent); } } }; //为listView注册单机监听器 ListView listView=findViewById(R.id.list_options); listView.setOnItemClickListener(itemClickListener); } }
DrinkCategoryActivity:
package bjfu.it.sun.starbuzz; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ComponentActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; public class DrinkCategoryActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_drink_category); ArrayAdapter<Drink>listAdpter=new ArrayAdapter<>( this, android.R.layout.simple_list_item_1,Drink.drinks); ListView listDrinks =findViewById(R.id.list_drinks ); listDrinks.setAdapter(listAdpter); //创建监听器 AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> listDrinks, View itemView, int position, long id) { //pass the drink the user clicks on to DrinkActivity Intent intent =new Intent(DrinkCategoryActivity .this,DrinkActivity.class); intent.putExtra(DrinkActivity.EXTRA_DRINKID,(int)id); startActivity(intent); } }; //Assign the linstener to the list view(将linstener指定给列表视图) listDrinks.setOnItemClickListener(itemClickListener ) ; } }
TopLevelActivity:
package bjfu.it.sun.starbuzz; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; public class TopLevelActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_top_level); //实现onItemClickListener,点击时打开二级界面 AdapterView.OnItemClickListener itemClickListener= new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> listView, View itemView, int position, long id) { if (position == 0){ Intent intent = new Intent(TopLevelActivity.this, DrinkCategoryActivity.class); startActivity(intent); } } }; //为listView注册单机监听器 ListView listView=findViewById(R.id.list_options); listView.setOnItemClickListener(itemClickListener); } }
activity_drink.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".DrinkActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/photo" android:layout_width="190dp" android:layout_height="190dp" app:srcCompat="@drawable/logo" /> <TextView android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <TextView android:id="@+id/description" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
activity_drink_category.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".DrinkCategoryActivity"> <ListView android:id="@+id/list_drinks" android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
activity_top_level.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".TopLevelActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/imageView" android:layout_width="200dp" android:layout_height="100dp" android:contentDescription="@string/starbuuzz_logo" android:src="@drawable/logo" app:srcCompat="@drawable/logo" /> <ListView android:id="@+id/list_options" android:layout_width="match_parent" android:layout_height="match_parent" android:entries="@array/options" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
<resources> <string name="app_name">Starbuzz</string> <string name="starbuuzz_logo">Starbuzz logo</string> <string-array name="options"> <item>Drinks</item> <item>Food</item> <item>Sores</item> </string-array> </resources>
遇到的问题:当时正要测试程序,但是又打开了另一个应用,给卡住了,最后又重建了一个虚拟机,这样才算好了