我已经在我的ListView
编了一个MainActivity
项目视图和一个汽车类。
public class Car {
private String make;
private int year;
private int iconID;
private String condition;
public Car(String make, int year, int iconID, String condition) {
this.make = make;
this.year = year;
this.iconID = iconID;
this.condition = condition;
}
public String getMake() {return make;}
public int getYear() {return year;}
public int getIconID() {return iconID;}
public String getCondition() {return condition;}
}
我的 MainActivity 类看起来像这样:
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
ActionBar actionBar;
private List<Car> myCars = new ArrayList<Car>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyListAdapter adapter = new MyListAdapter();
toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
populateCarList();
populateListView();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
private void populateCarList() {
myCars.add(new Car("Ford", 1940, R.mipmap.ic_launcher, "Needing work"));
myCars.add(new Car("Benz", 1960, R.mipmap.ic_launcher, "Cheap"));
myCars.add(new Car("Mustang", 2000, R.mipmap.ic_launcher, "Needing new owner"));
myCars.add(new Car("BMW", 2012, R.mipmap.ic_launcher, "Needing a lot of work!"));
myCars.add(new Car("Toyota", 1940, R.mipmap.ic_launcher, "Oldtimer"));
myCars.add(new Car("VW", 2003, R.mipmap.ic_launcher, "Cool"));
myCars.add(new Car("Ferrari", 2008, R.mipmap.ic_launcher, "Nice"));
}
private void populateListView() {
ArrayAdapter<Car> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.carsListView);
TextView textView = new TextView(MainActivity.this);
textView.setText("Here you can see all the Cars!");
textView.setTextSize(15);
textView.setGravity(Gravity.CENTER_HORIZONTAL);
textView.setTypeface(null, Typeface.BOLD);
textView.setTextColor(Color.parseColor("#a60b0b"));
list.addHeaderView(textView);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Car> {
public MyListAdapter() {
super(MainActivity.this, R.layout.item_view, myCars);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
}
String url;
switch(position){
case 0: url = "http://www.google.com"; break;
case 1: url = "http://www.google.com"; break;
case 2: url = "http://www.google.com"; break;
case 3: url = "http://www.google.com"; break;
case 4: url = "http://www.google.com"; break;
case 5: url = "http://www.google.com"; break;
case 6: url = "http://www.google.com"; break;
default: url = "http://www.google.com"; break;
}
Button button = (Button)itemView.findViewById(R.id.item_button);
button.setTag(url);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = (String)v.getTag();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
});
Car currentCar = myCars.get(position);
ImageView imageView = (ImageView) itemView.findViewById(R.id.item_icon);
imageView.setImageResource(currentCar.getIconID());
// Make:
TextView makeText = (TextView) itemView.findViewById(R.id.item_txtMake);
makeText.setText(currentCar.getMake());
// Year:
TextView yearText = (TextView) itemView.findViewById(R.id.item_txtYear);
yearText.setText("" + currentCar.getYear());
// Condition:
TextView conditionText = (TextView) itemView.findViewById(R.id.item_txtCondition);
conditionText.setText(currentCar.getCondition());
return itemView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == android.R.id.home){
onBackPressed();
return true;
}
if (id == R.id.watchList) {
startActivity(new Intent(this, WatchListActivity.class));
}
return super.onOptionsItemSelected(item);
}
public void addCarToWatchList (View view){
Toast.makeText(MainActivity.this, "Car has been added to WatchList", Toast.LENGTH_SHORT).show();
}
}
我还有一个WatchList
班级(或FavoriteCarsList
班级,无论你想叫什么)
public class WatchListActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_watch_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == android.R.id.home){
onBackPressed();
return true;
}
if (id == R.id.watchList) {
startActivity(new Intent(this, WatchListActivity.class));
}
return super.onOptionsItemSelected(item);
}
}
项目视图的xml代码如下所示:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_icon"
android:src="@mipmap/ic_launcher"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:layout_marginBottom="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Make shown here"
android:id="@+id/item_txtMake"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="2000"
android:id="@+id/item_txtYear"
android:layout_below="@+id/item_icon"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Condition shown Here"
android:id="@+id/item_txtCondition"
android:layout_below="@+id/item_txtYear"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy Car online!"
android:id="@+id/item_button"
android:layout_below="@+id/item_txtCondition"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add Car to WatchList"
android:onClick="addCarToWatchList"
android:id="@+id/item_watchlist_button"
android:layout_alignTop="@+id/item_icon"
android:layout_toRightOf="@+id/item_button"
android:layout_toEndOf="@+id/item_button" />
(首先:不要为每个车的按钮或相同图像(ic_launcher)的www.google.com链接感到困惑,我只用它来测试我的代码。按钮在那里,得到例如,你可以购买汽车的链接。)
所以一切都好。我可以运行应用程序没有错误。所有按钮工作。我在ListView
使用不同的汽车获得MainActivity
,ToolBar
我有一个Icon,我可以点击并切换到WatchList
活动。一切都完全按照我的意愿。
但我找不到下一步的解决方案。
首先:我想在item_view.xml
添加另一个按钮。它必须像“Add to WatchList”-Button。如果我单击该按钮,则必须将car-item添加到ListView
活动的WatchList
。 (ListView
类的WatchList
尚不存在)
第二:如果一个项目被添加到WatchList
,它必须是一个自己的布局(自己的item_watchlist_view.xml
),它只包含(例如)item_view.xml的名称和图像来自MainActivity
。如果item_watchlist_view.xml
上有一个删除按钮,如果我WatchList
中不再需要它,可以用来从WatchList
删除汽车,这也不错。 }。
所以基本上应用程序必须像“购物车系统”一样运行,但区别在于我只有汽车作为产品,我可以将它们添加到WatchList
或FavoriteList
(“购物车” “)。
我尝试了很多东西两个星期但没有任何作用......所以我决定在社区注册,希望得到帮助。
如果有人能给我一个详细的答案,那就太好了!
谢谢..抱歉我的英语不好!
答案 0 :(得分:1)
要将参数传递给新的Activity,请通过调用
来调用它Intent intent = new Intent(this, WatchListActivity.class);
Bundle b = new Bundle();
b.putInt("key", 1); //Your id
intent.putExtras(b); //Put your id to your next Intent
startActivity(intent);