0

我有一个主 Activity,它有一个名为 show 的按钮,我想单击 show 按钮,它将显示一个带有列表视图的对话框,其中包含 SQLite 数据库中的所有内容。

这是我正在使用的代码:

主要活动:

public class MainmenuActivity extends SlidingActivity{


Button buttononside;
Button orderbutton;
TextView title;
FragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
int Quantity=0;
int Total=0;
TextView Quantity_txt;
static TextView Total_txt;
final Context context = this;
CustomListViewAdapter CLVA;
View v;

DBAdapter myDB;
String[][] V = new String[100][100];
int VLenght;
ListView myList;
ArrayList<ListViewItemOrder> items;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_mainmenu);
    setBehindContentView(R.layout.sidemenu);

    CLVA = new CustomListViewAdapter(context, Quantity, null);

    mAdapter = new FragmentAdapter(getSupportFragmentManager());
    mPager = (ViewPager)findViewById(R.id.pager);

    mPager.setAdapter(mAdapter);
    mPager.setOffscreenPageLimit(4);
    mIndicator = (PageIndicator)findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);

    getSlidingMenu().setBehindOffset(200);
    getSlidingMenu().setMode(SlidingMenu.RIGHT);
    getSlidingMenu().setFadeDegree(0.35f);

    Quantity_txt=(TextView)findViewById(R.id.qty);
    Total_txt=(TextView)findViewById(R.id.total);

    opendb();
}

private void opendb() {
    myDB = new DBAdapter(this);
    myDB.open();        
}


public boolean onCreateOptionsMenu(android.view.Menu menu) {
    getMenuInflater().inflate(R.menu.mainmenu, menu);
    return true;
}


public void onClick(View v) {
    getSlidingMenu().toggle();

}

public void changecolor(View v){
    buttononside = (Button) findViewById(v.getId());


    buttononside.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction()==MotionEvent.ACTION_DOWN)
                buttononside.setBackgroundColor(Color.BLACK); 
            else
                buttononside.setBackgroundResource(R.drawable.buttonshape);
            return true;
        }

    });
}

public void openpopup(View v){
    orderbutton = (Button) findViewById(v.getId());
    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.custom_popup);
    dialog.setTitle("Choose your topings before ordering..");
    dialog.show();
    V = CLVA.getValues();
    VLenght = CLVA.getValuesLenght();
    Add(V,VLenght);
    showFood();
    //CLVA.addToDB(context);
}


public void Add(String[][] T,int i){
    for(int k=0;k<i;k++){
        Log.i("Add "+k,""+T[k][0]+" "+T[k][1]+" "+T[k][2]);
        myDB.insertRow(T[k][0].toString(),T[k][1].toString(),T[k][2].toString());
    }
}

@SuppressWarnings("deprecation")
public void showFood(){
    LayoutInflater inflater = (LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inflater.inflate(R.layout.custom_popup, null);
    myList = (ListView) v.findViewById(R.id.checkedItems);
    final Cursor cr = myDB.getAllRows();
    int i = cr.getCount() -1;
    while(i>=0){
        Log.i("---------Item"+i,""+cr.getString(cr.getColumnIndex("name"))+"  "+cr.getString(cr.getColumnIndex("quantity"))+"  "+cr.getString(cr.getColumnIndex("price")));
        i--;
        cr.moveToNext();
    }
    startManagingCursor(cr);

    String[] fromFieldNames = new String[]{
            DBAdapter.KEY_NAME,
            DBAdapter.KEY_QUANTITY,
            DBAdapter.KEY_PRICE
    };
    int[] toViewIDs = new int [] {
            R.id.nameItemOrder,
            R.id.quantityItemOrder,
            R.id.priceItemOrder
    };

    SimpleCursorAdapter myCursorAdapter = 
            new SimpleCursorAdapter(
                    this,
                    R.layout.itemorder,
                    cr,
                    fromFieldNames,
                    toViewIDs
                    );
    myList.setAdapter(myCursorAdapter);
    Log.i("teme","sssssssssss");
}
@Override
public void onBackPressed(){

}


static class ListViewItem{
    public String ItemTitle;
    public int price;
    public String Description;
    public TextView title;
    public TextView pricetitle;
    public TextView Descriptiontitle;
    public CheckBox cb;
    public Spinner sp;
    public boolean isChecked;
    public int spinnerValue;
    public TextView t=Total_txt;
    public int valueBefore;
}


      } 

它正确连接到数据库并正确插入了所有值,但对话框始终为空并且不显示列表视图。有人可以帮我吗?

谢谢 :)

4

1 回答 1

0

尝试将您的更改openpopup(View)为:

public void openpopup(View v){
    orderbutton = (Button) findViewById(v.getId());
    final Dialog dialog = new Dialog(context);
    // dialog.setContentView(R.layout.custom_popup);
    dialog.setTitle("Choose your topings before ordering..");

    View dialogView = showFood();
    dialog.setContentView(dialogView);

    dialog.show();
    V = CLVA.getValues();
    VLenght = CLVA.getValuesLenght();
    Add(V,VLenght);
    // showFood();
    //CLVA.addToDB(context);
}

将您更改showFood()为:

public View showFood(){
    ....
    ....

    return v;
}

发生的事情是您正在从同一个布局文件中扩充两个视图。您正在一个视图中设置 ListView 及其适配器,但未dialog.setContentView()在其上使用。

相反,您使用R.layout.custom_popup来设置对话框的内容。在这种情况下,ListView 没有设置适配器。

于 2013-08-17T22:31:20.113 回答