我想在点击后退按钮后将我的数据保存在listview上,并在按下查看按钮后查看它。但我没有得到它

时间:2016-11-22 12:10:35

标签: android listview

我在主要活动中有一个Recyclerview,在recyclerview的每个片段上都有一个按钮。当我点击该按钮时,数据只会被添加到另一个活动中的listview。我的操作栏上有一个按钮,当我点击该按钮时只想查看列表视图。提前致谢。 我的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);

    }
}

我的listview适配器代码如下:

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>

1 个答案:

答案 0 :(得分:0)

最简单的方法是,您可以创建一个可以容纳List of Elements的公共类,您可以随时访问它。

使用字符串对象的示例

public static class MyListHolder{

static List<String> lstOfObj;

public Static MyListHolder(){
   lstOfObj = new ArrayList<String>();
}

public static void addNewObject(String object){
    lstOfObj.add(object);
}

public static List<String> getMyObjList(){
    return lstOfObj;
}
}

现在您可以使用此课程&amp;管理您的对象列表的方法。