Java 如何将带有按钮的列表视图中的项目添加到其他活动的另一个列表视图中?
我在我的Java 如何将带有按钮的列表视图中的项目添加到其他活动的另一个列表视图中?,java,android,listview,button,add,Java,Android,Listview,Button,Add,我在我的main活动中编程了一个列表视图,其中包含一个项目视图和一个汽车类 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 = m
main活动
中编程了一个列表视图
,其中包含一个项目视图和一个汽车类
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();
}
}
项目视图的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" />
(首先,不要被www.google.com上的按钮链接或每辆车的相同图像(ic_launcher)搞糊涂了,我只是用它来测试我的代码。按钮就在那里,比如,你可以在那里买到汽车。)
所以一切都很好。我可以毫无错误地运行应用程序。所有按钮都正常工作。我在我的main活动
中获得了一个不同车辆的列表视图
,在工具栏上有一个图标,我可以单击该图标并切换到观察列表
活动。一切都是我想要的
但我找不到下一步的解决方案
首先:我想在我的项目\u view.xml
中添加另一个按钮。它必须像一个“添加到观察列表”按钮。如果我单击该按钮,汽车项目必须添加到监视列表
活动的列表视图
。(位于监视列表
类的列表视图
尚不存在)
第二:如果一个项目被添加到观察列表
,它必须是一个自己的布局(ownitem\u WatchList\u view.xml
),其中只包括(例如)来自MainActivity
的项目的名称和图像。如果项目(watchlist)view.xml
上有一个delete按钮
,如果我不想在观察列表
中再使用它,它也可以用于从观察列表
中删除汽车
因此,该应用程序基本上必须像“购物车系统”一样运行,但区别在于我只有汽车作为产品,我可以将它们添加到观察列表
或收藏列表
(“购物车”)
两周来我尝试了很多东西,但都不管用。。。所以我决定在这个社区注册,希望能得到帮助
如果有人能给我一个详细的答案,那就太好了
谢谢。。对不起,我的英语不好 要将参数传递给新活动,请通过调用
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);
通常,您应该创建一个类
观察列表
,在该类中包含一个列表,并将该类传递给第二个活动。然后使用适配器访问类监视列表中的信息,以在第二个活动中填充ListView。谢谢您的回答。我以前在一个论坛上看到过类似的解决方案,但我无法用这个答案解决我的问题。你对该解决方案有什么问题?例如:我如何将该观察列表类传递给另一个活动?你有不同的选择。一种方法是将该监视列表及其条目存储在数据库中,比如SQLite,然后将所需列表的ID作为整数传递给Bundle,然后将该列表加载到新活动中。或者,您可以使WatchList
实现Parcelable
并添加所需的所有方法,然后直接在Intent
中传递WatchList
。我将给出一个答案,告诉你如何通过意图传递参数。
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);