当我将数据从弹出菜单传递到主活动时,没有任何内容显示在主活动列表视图中

时间:2016-04-19 15:58:00

标签: android android-layout listview android-fragments popup

我已经添加了startactivity来完成点击,但是当我添加新数据时它会删除旧数据请帮助

enter code here

public class popupmenu扩展了AppCompatActivity {

@Override
enter code here
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.popupmenu);

    DisplayMetrics DM = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(DM);

    int width = DM.widthPixels ;
    int height = DM.heightPixels;

    getWindow().setLayout((int) (width * .8), (int) (height * .6));

    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.dimAmount=0.0f;
    getWindow().setAttributes(lp);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    try {
        TextView ItemClicked = (TextView) findViewById(R.id.ListItemClicked);
        Bundle B = getIntent().getExtras();
        String itoma = B.getString("ItemClicko");
        ItemClicked.setText(itoma);
    }
    catch (Exception ex){
        ex.getStackTrace();
    }
}

public void done(View view) {
    TextView ItemClicked2 = (TextView) findViewById(R.id.ListItemClicked);
    Intent intent=new Intent(this,MainActivity.class);
    String finale_order=ItemClicked2.getText().toString();
    intent.putExtra("done_order", finale_order);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        }

}

公共类MainActivity扩展了AppCompatActivity {

final ArrayList<listitem2>items2=new ArrayList<listitem2>();

final adapter2 ss=new adapter2(items2);
String order;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent2=getIntent();

        if (intent2.hasExtra("done_order")){
        order=getIntent().getExtras().getString("done_order");
        lv = (ListView) findViewById(R.id.finalorder);
        items2.add(new listitem2(order));
        lv.setAdapter(ss);
            ss.notifyDataSetChanged();
        }
    }

public void pizzamenu(View view) {
    Intent pizzaintent=new Intent(this,pizzaactivity.class);
    startActivity(pizzaintent);

}

public void appetizers(View view) {
    Intent appetizersintent=new Intent(this,appetizersactivity.class);
    startActivity(appetizersintent);
    onPause();
}

public void maindishes(View view) {
    Intent maindishesintent=new Intent(this,MainDishes.class);
    startActivity(maindishesintent);

}

public void sandwiches(View view) {
    Intent sandwichesintent=new Intent(this,Sandwiches.class);
    startActivity(sandwichesintent);

}

public void pasta(View view) {
    Intent pastasintent=new Intent(this,pasta.class);
    startActivity(pastasintent);

}
public void seafood(View view) {
    Intent seafoodsintent=new Intent(this,SeaFood.class);
    startActivity(seafoodsintent);

}

public void soups(View view) {
    Intent soupsintent=new Intent(this,soupActivity.class);
    startActivity(soupsintent);

}


public void salad(View view) {
    Intent saladsintent=new Intent(this,salad.class);
    startActivity(saladsintent);
}

}  public class adapter2扩展了BaseAdapter {

    ArrayList<listitem2>items=new ArrayList<listitem2>();

    public adapter2(ArrayList<listitem2>items){
        this.items=items;
    }
    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position).name;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater=getLayoutInflater();
        View view1=inflater.inflate(R.layout.activity_order_listview, null);
        final TextView textname = (TextView) view1.findViewById(R.id.textView7);
        final Button deletebutton = (Button) view1.findViewById(R.id.button13);
        deletebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                items.remove(position);
                ss.notifyDataSetChanged();
                Toast.makeText(getApplicationContext(), "you removed:" + textname.getText().toString(), Toast.LENGTH_SHORT).show();
            }
        });
        textname.setText(items.get(position).name);
        return view1;
    }
}

0 个答案:

没有答案