如何在listview中动态添加选定的微调器值是另一个活动

时间:2014-06-27 07:08:35

标签: android listview

如何在listview中动态添加选定的微调器值是另一个活动。

当我第二次更改微调器值时,打开的列表视图活动值在列表视图中被替换但未添加。

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();
    }
}
}

listviewitems.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;
    }

}

}

0 个答案:

没有答案