Question

I have a main Activity that has a button called show, i want to click on the show button and it will display a dialog with a listview with all the content from the SQLite database.

Here is the code i am using:

Main Activity:

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


      } 

It connects to the database properly and all the values are inserted properly, but the dialog is always empty and the listview is not displayed. Can somebody help me with that?

Thank you :)

Was it helpful?

Solution

Try changing your openpopup(View) to:

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

Change your showFood() to:

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

    return v;
}

What's happening is that you are inflating two views from the same layout file. You are setting up the ListView and its adapter in one view but not using dialog.setContentView() on it.

Instead, you use R.layout.custom_popup to set the content of the dialog. The ListView does not have an adapter set in this case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top