-2
我在主要活動中有一個RecyclerView,並且在recyclerview的每個片段上都有一個按鈕。當我點擊那個按鈕時,數據就會被添加到另一個活動的列表視圖中。而且我的操作欄上有一個按鈕,當我點擊該按鈕時只想看到列表視圖。提前致謝。 我MainAcitivity代碼如下:我想在點擊後退按鈕後將數據保存在列表視圖中,並在按下查看按鈕後查看它。但我沒有看到它
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
// ArrayList<Integer> images;
// ArrayList<String> imageNames, contents;
public static int[] images = {R.drawable.bengalithali,R.drawable.chikanthali,R.drawable.eggthali, R.drawable.gujratithali,
R.drawable.maharstrianthali, R.drawable.keralathali, R.drawable.rajsthanithali, R.drawable.tamilthali};
public static String[] imageNames = {"Bengali Thali","Chikan Thali", "Egg Thali", "Gujrathi Thali", "Maharashtrian Thali", "Kerala Thali",
"Rajsthani Thali", "Tamil Thali"};
public static String[] contents = {"As seen in Pic","As seen in Pic","As seen in Pic","As seen in Pic","As seen in Pic","As seen in Pic",
"As seen in Pic","As seen in Pic"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
// images = new ArrayList<>();
// images.add(R.drawable.bengalithali);
// images.add(R.drawable.chikanthali);
// images.add(R.drawable.eggthali);
// images.add(R.drawable.gujratithali);
// images.add(R.drawable.maharstrianthali);
// images.add(R.drawable.keralathali);
// images.add(R.drawable.rajsthanithali);
// images.add(R.drawable.tamilthali);
//
// imageNames = new ArrayList<>();
// imageNames.add("Bengali Thali");
// imageNames.add("Chikan Thali");
// imageNames.add("Egg Thali");
// imageNames.add("Gujrathi Thali");
// imageNames.add("Maharashtrian Thali");
// imageNames.add("Kerala Thali");
// imageNames.add("Rajsthani Thali");
// imageNames.add("Tamil Thali");
//
// contents = new ArrayList<>();
// contents.add("As seen in Pic");
// contents.add("As seen in Pic");
// contents.add("As seen in Pic");
// contents.add("As seen in Pic");
// contents.add("As seen in Pic");
// contents.add("As seen in Pic");
// contents.add("As seen in Pic");
// contents.add("As seen in Pic");
adapter = new RecyclerAdapter(MainActivity.this,images, imageNames, contents);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_button,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent(MainActivity.this, MyCartList.class);
startActivity(intent);
return super.onOptionsItemSelected(item);
}
}
我Recyclerview適配器代碼如下: 公共類RecyclerAdapter擴展
RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
String[] imageNames, contents;
int[] images;
Context context;
public RecyclerAdapter(Context context,int[] images,String[] imageNames,String[] contents) {
this.images = images;
this.imageNames = imageNames;
this.contents = contents;
this.context = context;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_items, parent, false);
RecyclerViewHolder rvh = new RecyclerViewHolder(view);
return rvh;
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
holder.productImage.setFitsSystemWindows(true);
holder.productImage.setImageResource(images[position]);
holder.productName.setText(imageNames[position]);
holder.productContent.setText(contents[position]);
}
@Override
public int getItemCount() {
return images.length;
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder{
ImageView productImage;
TextView productName, productContent, quantity;
CircleButton plusButton, minusButton;
FButton addToCartButton;
public RecyclerViewHolder(final View itemView) {
super(itemView);
productImage = (ImageView) itemView.findViewById(R.id.product_image);
productName = (TextView) itemView.findViewById(R.id.product_name);
productContent = (TextView) itemView.findViewById(R.id.product_content);
plusButton = (CircleButton) itemView.findViewById(R.id.plus_button);
minusButton = (CircleButton) itemView.findViewById(R.id.minus_button);
quantity = (TextView) itemView.findViewById(R.id.show_quantity);
addToCartButton = (FButton) itemView.findViewById(R.id.add_button);
plusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int Quantity = Integer.parseInt(quantity.getText().toString());
quantity.setText(String.valueOf(Quantity+1));
if (Quantity == 10 && plusButton.isClickable()){
quantity.setText("10");
}
}
});
minusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int Quantity = Integer.parseInt(quantity.getText().toString());
quantity.setText(String.valueOf(Quantity-1));
if (Quantity == 0 && minusButton.isClickable()){
quantity.setText("0");
}
}
});
addToCartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, MyCartList.class);
// Bundle bundle = new Bundle();
// bundle.putString("KEYONE",imageNames[getAdapterPosition()]);
// bundle.putString("KEYTWO", contents[getAdapterPosition()]);
// intent.putExtras(bundle);
intent.putExtra("KEYONE", imageNames[getAdapterPosition()]);
intent.putExtra("KEYTWO", contents[getAdapterPosition()]);
view.getContext().startActivity(intent);
Toast.makeText(context, ""+imageNames[getAdapterPosition()]+ "\n"+ contents[getAdapterPosition()], Toast.LENGTH_SHORT).show();
}
});
}
}
}
我CartList活動代碼如下:
public class MyCartList extends AppCompatActivity {
ListView myList = null;
MyListViewAdapter myListViewAdapter;
public static ArrayList<String> c = new ArrayList<>();
public static ArrayList<String> d = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_cart_list);
Intent i = getIntent();
String a = i.getStringExtra("KEYONE");
String b = i.getStringExtra("KEYTWO");
c.add(a);
d.add(b);
myList = (ListView) findViewById(R.id.my_list_view);
myListViewAdapter = new MyListViewAdapter(MyCartList.this,c,d);
myList.setAdapter(myListViewAdapter);
}
}
我的列表視圖適配器代碼如下:
public class MyListViewAdapter extends BaseAdapter{
Context mContext;
public static ArrayList<String> thaliNames, thaliContent;
SharedPreferences sharedPreferences;
public MyListViewAdapter(Context context,ArrayList<String> thaliNames, ArrayList<String> thaliContent) {
this.mContext = context;
this.thaliNames = thaliNames;
this.thaliContent = thaliContent;
}
@Override
public int getCount() {
return thaliNames.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.my_list, null);
TextView listItem = (TextView) view.findViewById(R.id.my_product_name);
listItem.setText(thaliNames + "\n" + thaliContent);
return view;
}
}
activity_main.xml中如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.gaurya.carttask.MainActivity"
android:scrollbars="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
list_tems.xml如下:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.makeramen.roundedimageview.RoundedImageView
android:id="@+id/product_image"
android:layout_width="150dp"
android:layout_height="200dp"
app:riv_border_color="@android:color/holo_red_dark"
app:riv_corner_radius="30dip"
app:riv_border_width="2dip"
app:riv_mutate_background="true"
app:riv_tile_mode="repeat"
app:riv_oval="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical">
<TextView
android:id="@+id/product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="20dp"
android:text="Name"
android:textSize="20sp"/>
<TextView
android:id="@+id/product_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:text="Name"
android:textSize="15sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<at.markushi.ui.CircleButton
android:id="@+id/plus_button"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_marginTop="25dp"
android:layout_marginLeft="15dp"
app:cb_color="#64daed"
android:src="@drawable/plus"
/>
<TextView
android:id="@+id/show_quantity"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_marginTop="25dp"
android:text="0"
android:gravity="center"
android:textSize="30sp"
android:padding="5dp"
android:layout_marginLeft="10dp"/>
<at.markushi.ui.CircleButton
android:id="@+id/minus_button"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_marginTop="25dp"
app:cb_color="#64daed"
android:src="@drawable/negative"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<info.hoang8f.widget.FButton
android:id="@+id/add_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="add to cart"
app:shadowEnabled="true"
app:shadowHeight="5dp"
app:cornerRadius="15dp"
android:layout_marginTop="17dp"/>
</LinearLayout>
</LinearLayout>
activity_my_cart_list.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_my_cart_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.gaurya.carttask.MyCartList">
<ListView
android:id="@+id/my_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</ListView>
</RelativeLayout>
my_list.xml是如下:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/my_product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
發佈您的代碼只是不給你的問題。請明確說明你做了什麼以及你如何需要它以及你出錯的地方。 –
在我的代碼中,其標籤上有一個recyclerview,其中有一個添加到購物車按鈕。當我點擊那個按鈕時,我只想要那個選項卡的名稱和內容會轉到另一個活動的列表視圖。但我不想在點擊按鈕上看到它。我想看到另一個按鈕上的列表,它位於我的操作欄上。就像在線購物應用程序一樣,我們將該項目添加到購物車,但無法看到它。但我們可以點擊購物車看到它。謝謝 –