如何在listview中添加动态选择的gridview项

时间:2014-06-28 08:22:47

标签: android listview gridview

第一项活动:gridview,只要您点击gridview item dialogBox,就会打开包含微调器的活动。现在,我想在第二个Gridview包含item中显示已选定的spinner Activity和选定的Listview值。当您在第二次点击时,listview中的值将被替换,但不会添加。 任何人都可以帮助我............

... MainActivity

public class MainActivity extends Activity implements OnClickListener {

    SharedPreferences SharedPrefs;
    String sp_selected;
    Spinner sp;
    String s1;
    String partname;
    String partname1;
    Button Parts_history;

    Imageadapter image_adapter;

    private static final String[] paths = { "Select Your Choice", "Type1",
            "Type2", "Type3", "Type4" };
    private static final int position = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.partsrepair);

        image_adapter = new Imageadapter(this);

        SharedPrefs = getSharedPreferences("Preference", MODE_PRIVATE);

        GridView gridview = (GridView) findViewById(R.id.gridView1);
        gridview.setAdapter(new Imageadapter(this));

        Parts_history = (Button) findViewById(R.id.partshistory_button);
        Parts_history.setOnClickListener((this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(final AdapterView<?> parent, View v,
                    int position, long id) {

                SharedPreferences.Editor edit = SharedPrefs.edit();

                // partname = parent.getItemAtPosition(position).toString();
                partname1 = image_adapter.names[position].toString();
                image_adapter.names[position].toString();
                Toast.makeText(MainActivity.this, "you Selected:" + partname1,
                        Toast.LENGTH_SHORT).show();

                // edit.putString("SelectPart", partname1);

                final Dialog dialog = new Dialog(MainActivity.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.repairtype_spinner);
                dialog.setCancelable(true);

                Spinner sp = (Spinner) dialog.findViewById(R.id.spinner_1);

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        MainActivity.this,
                    android.R.layout.simple_spinner_item,paths); 

                sp.setAdapter(adapter);

                sp.setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> parent,
                            View view, int position, long id) {

                        if (position > 0) {
                    sp_selected = parent.getItemAtPosition(position)
                                    .toString();
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?>arg0) {
                        // TODO Auto-generated method stub

                    }
                });

        Button btnOk = (Button)dialog.findViewById(R.id.Button_sms_ok);
                btnOk.setOnClickListener(new View.OnClickListener() {

        @Override
    public void onClick(View v) {
        SharedPreferences.Editor edit = SharedPrefs.edit();

            if ((sp_selected != null)
                    && !sp_selected.equals("Select Your Choice")) { //
                            edit.putString("SelectType", sp_selected);
                        Toast.makeText(getApplicationContext(),
                    "You selected:" + sp_selected,
                Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(),
                    "Thank You!", Toast.LENGTH_LONG).show();

                            dialog.dismiss();
                        } else {

            Toast.makeText(MainActivity.this,
                "plz Select your Choice",
                    Toast.LENGTH_SHORT).show();
                        }} });


                dialog.show(); }});}

                    public void onClick(View v) {

                 switch (v.getId()) {
                case R.id.partshistory_button:

            Intent intent = new Intent(MainActivity.this, ListViewItems.class);
            intent.putExtra("Part", partname1);
            intent.putExtra("Type", sp_selected);
            startActivity(intent);
            finish();
               }}}

listview.class

public class ListViewItems extends Activity {

    ArrayList<String> part, type;

    ListView list;

    public ListViewItems() {
        // TODO Auto-generated constructor stub
        part = new ArrayList<String>();
        type = new ArrayList<String>();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms_summary);

        Intent i = getIntent();
        part.add(i.getStringExtra("Part"));
        type.add(i.getStringExtra("Type"));

        list = (ListView) findViewById(R.id.listView1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getBaseContext(), R.layout.listview, type);
        list.setAdapter(new CustomViewAdapter(ListViewItems.this));
        adapter.notifyDataSetChanged();
    }

    public class CustomViewAdapter extends BaseAdapter {

        Context context;

        public CustomViewAdapter(Context context) {
            // TODO Auto-generated constructor stub
            this.context = context;
        }
        private class ViewHolder {
            TextView text_part;
            TextView text_type;
        }
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            // Log.d("hao",""+position);

            LayoutInflater minflater = (LayoutInflater) context
                               .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
                convertView = minflater.inflate(R.layout.listview, null);
                holder = new ViewHolder();
                holder.text_part = (TextView) convertView
                        .findViewById(R.id.textView1);
                // Log.d("hao", ""+holder.text_desc);
                holder.text_type = (TextView) convertView
                        .findViewById(R.id.textView2);
                convertView.setTag(holder);
            }

            else
                holder = (ViewHolder) convertView.getTag();

            holder.text_type.setText(type.get(position));
            holder.text_part.setText(part.get(position));

            return convertView;

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return part.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    }}

Imageadapter.class

 public class Imageadapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mlayoutinflater;

public Imageadapter(Context c) {
    mContext = c;
    mlayoutinflater = LayoutInflater.from(c);
}

public int getCount() {
    return mThumbIds.length;
}
public Object getItem(int position) {
    return position;
}
public long getItemId(int position) {
    return position;
}

class ViewHolder {
    ImageView imageView;
    TextView textView;
}
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    ViewHolder childHolder;

    if (convertView == null) {
        // if it's not recycled, initialize some
        convertView = mlayoutinflater.inflate(R.layout.partsnames, null);
        childHolder = new ViewHolder();
        childHolder.imageView = (ImageView) convertView
                .findViewById(R.id.imageView1);
        childHolder.textView = (TextView) convertView
                .findViewById(R.id.textView1);
        convertView.setTag(childHolder);
    } else {
        childHolder = (ViewHolder) convertView.getTag();
    }
    childHolder.imageView.setImageResource(mThumbIds[position]);
    childHolder.textView.setText(names[position]);

    return convertView;
}
        public Integer[] mThumbIds = { R.drawable.ic_launcher, R.drawable.ic_launcher,
    R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher, };

         public String[] names = { "First", "Second", "Third", "Fourth",
        "Fifth", "Sixth", "Seventh" };
       }

1 个答案:

答案 0 :(得分:0)

似乎价值并未真正被覆盖。每次加载第二个活动时,都会创建新的零件和类型ArrayLists。在这些新列表中,只添加了最新的部分和类型(在onCreate函数中),这些部分和类型已经与意图一起旅行。

如果您希望列表继续添加用户选择的部件和类型,您可以维护MainActivity上的列表。因此,在选择每个项目和类型后,将其添加到MainActivity上的列表中,然后将其传递给ListViewItems活动。

这样,列表将始终包含您选择的所有项目。

我必须承认,如果我理解你的问题,我并不是百分百肯定。但我试着尽可能多地回答。

编辑:

MainActivity:

public class MainActivity extends ActionBarActivity {

    ArrayList<String> list;
    int clickCount = 0;

    static TextView textViewHelloWorld;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            list = new ArrayList<String>();

            textViewHelloWorld = (TextView) findViewById(R.id.textViewHelloWorld);


            Button buttonAddElements = (Button) findViewById(R.id.buttonAddElement);
            buttonAddElements.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    clickCount = clickCount + 1;
                    textViewHelloWorld.setText("Click Count" + clickCount);
                    list.add("Click number " + clickCount);             
                }
            });

            Button buttonStartNewActivity = (Button) findViewById (R.id.buttonStartNewActivity);
            buttonStartNewActivity.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Bundle bundle = new Bundle();
                    bundle.putStringArrayList("list", list);
                    startActivity(new Intent(MainActivity.this, Main2Activity.class).putExtras(bundle));

                }
            });
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

Main2Activity :( ListView活动):

public class Main2Activity extends ActionBarActivity {

    ArrayList<String> list;
    ListView listView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        list  = getIntent().getStringArrayListExtra("list");

        listView = (ListView) findViewById(R.id.listViewList);
        String []dsf = new String[list.size()];
        list.toArray(dsf);
        listView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, dsf));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main2, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main2,
                    container, false);
            return rootView;
        }
    }

}

EDIT2: MainActivity.Java

public class MainActivity extends Activity implements OnClickListener {

    SharedPreferences SharedPrefs;
    String sp_selected;
    Spinner sp;
    String s1;
    String partname;
    String partname1;
    Button Parts_history;

    private ArrayList<String> parts;
    private ArrayList<String> types;

    Imageadapter image_adapter;

    private static final String[] paths = { "Select Your Choice", "Type1",
            "Type2", "Type3", "Type4" };
    private static final int position = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.partsrepair);

        image_adapter = new Imageadapter(this);

        parts = new ArrayList<String>();
        types = new ArrayList<String>();

        SharedPrefs = getSharedPreferences("Preference", MODE_PRIVATE);

        GridView gridview = (GridView) findViewById(R.id.gridView1);
        gridview.setAdapter(new Imageadapter(this));

        Parts_history = (Button) findViewById(R.id.partshistory_button);
        Parts_history.setOnClickListener((this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(final AdapterView<?> parent, View v,
                    int position, long id) {

                SharedPreferences.Editor edit = SharedPrefs.edit();

                // partname = parent.getItemAtPosition(position).toString();
                partname1 = image_adapter.names[position].toString();
                parts.add(partname1);
                image_adapter.names[position].toString();
                Toast.makeText(MainActivity.this, "you Selected:" + partname1,
                        Toast.LENGTH_SHORT).show();

                // edit.putString("SelectPart", partname1);

                final Dialog dialog = new Dialog(MainActivity.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.repairtype_spinner);
                dialog.setCancelable(true);

                Spinner sp = (Spinner) dialog.findViewById(R.id.spinner_1);

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        MainActivity.this,
                    android.R.layout.simple_spinner_item,paths); 

                sp.setAdapter(adapter);

                sp.setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> parent,
                            View view, int position, long id) {

                        if (position > 0) {
                    sp_selected = parent.getItemAtPosition(position)
                                    .toString();
                    types.add(sp_selected);
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?>arg0) {
                        // TODO Auto-generated method stub

                    }
                });

        Button btnOk = (Button)dialog.findViewById(R.id.Button_sms_ok);
                btnOk.setOnClickListener(new View.OnClickListener() {

        @Override
    public void onClick(View v) {
        SharedPreferences.Editor edit = SharedPrefs.edit();

            if ((sp_selected != null)
                    && !sp_selected.equals("Select Your Choice")) { //
                            edit.putString("SelectType", sp_selected);
                        Toast.makeText(getApplicationContext(),
                    "You selected:" + sp_selected,
                Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(),
                    "Thank You!", Toast.LENGTH_LONG).show();

                            dialog.dismiss();
                        } else {

            Toast.makeText(MainActivity.this,
                "plz Select your Choice",
                    Toast.LENGTH_SHORT).show();
                        }} });


                dialog.show(); }});}

                    public void onClick(View v) {

                 switch (v.getId()) {
                case R.id.partshistory_button:

                    Bundle bundle = new Bundle();
                    bundle.putStringArrayList("parts", parts);
                    bundle.putStringArrayList("type", types);



            Intent intent = new Intent(MainActivity.this, ListViewItems.class);
            intent.putExtra("Part", partname1);
            intent.putExtra("Type", sp_selected);
            intent.putExtra("bundle", bundle);
            startActivity(intent);
            finish();
               }}}

的ListView:

public class ListViewItems extends Activity {

    ArrayList<String> part, type;

    ListView list;

    public ListViewItems() {
        // TODO Auto-generated constructor stub
        part = new ArrayList<String>();
        type = new ArrayList<String>();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms_summary);

        Intent i = getIntent();
//        part.add(i.getStringExtra("Part"));
//        type.add(i.getStringExtra("Type"));

        part = i.getExtras().getBundle("bundle").getStringArrayList("parts");
        type = i.getExtras().getBundle("bundle").getStringArrayList("types");

        list = (ListView) findViewById(R.id.listView1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getBaseContext(), R.layout.listview, type);
        list.setAdapter(new CustomViewAdapter(ListViewItems.this));
        adapter.notifyDataSetChanged();
    }

    public class CustomViewAdapter extends BaseAdapter {

        Context context;

        public CustomViewAdapter(Context context) {
            // TODO Auto-generated constructor stub
            this.context = context;
        }
        private class ViewHolder {
            TextView text_part;
            TextView text_type;
        }
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            // Log.d("hao",""+position);

            LayoutInflater minflater = (LayoutInflater) context
                               .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
                convertView = minflater.inflate(R.layout.listview, null);
                holder = new ViewHolder();
                holder.text_part = (TextView) convertView
                        .findViewById(R.id.textView1);
                // Log.d("hao", ""+holder.text_desc);
                holder.text_type = (TextView) convertView
                        .findViewById(R.id.textView2);
                convertView.setTag(holder);
            }

            else
                holder = (ViewHolder) convertView.getTag();

            holder.text_type.setText(type.get(position));
            holder.text_part.setText(part.get(position));

            return convertView;

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return part.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    }}