我有一个主Activity,它有一个叫做show的按钮,我想点击show按钮,它会显示一个带有listview的对话框,其中包含来自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;
}
}
它正确连接到数据库并正确插入所有值,但对话框始终为空,并且不显示列表视图。 有人可以帮我吗?
谢谢:)
答案 0 :(得分: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没有设置适配器。