android popup-Fenster nicht anzeigen
Erstelle ich ein popup-Fenster mit einer listview. In der listview jede Zeile enthält eine textbox und radio-Buttons, aber die popup-Fenster nicht angezeigt. Ich kann nicht herausfinden, die Fehler.
Dies ist der code.
import java.util.List;
import java.util.Vector;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;
public class SettingsActivity extends Activity {
PopupWindow popup;
ListView fontSizeListView;
View popupView;
int currentFontSize;
Vector<FontSize> fontData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_view);
TextView tv = (TextView) findViewById(R.id.font_size_opt);
Typeface tf = TsciiTypeface.getTypeface(this);
tv.setTypeface(tf);
tv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showPopupMenu();
}
});
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
currentFontSize = Integer.parseInt(sp.getString("font_size", "1"));
SharedPreferences.Editor editor = sp.edit();
editor.clear();
editor.commit();
}
private void showPopupMenu() {
try {
popup = new PopupWindow(this);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.font_popup_view, null, false);
fontSizeListView = (ListView) popupView
.findViewById(R.id.font_size_list);
String[] arrLabel = getResources().getStringArray(
R.array.font_size_label_array);
String[] arrValue = getResources().getStringArray(
R.array.font_size_value_array);
fontData = new Vector<FontSize>();
for (int i = 0; i < arrLabel.length; i++) {
fontData.add(new FontSize(Integer.parseInt(arrValue[i]), arrLabel[i]));
}
CustomAdapter adapter = new CustomAdapter(this, R.layout.custom_list_pref_row, fontData);
fontSizeListView.setAdapter(adapter);
popup.setContentView(popupView);
popup.showAtLocation(this.findViewById(R.id.font_size_opt), Gravity.CENTER, 0, 0);
adapter.notifyDataSetChanged();
} catch (Exception e) {
MessageBox(e.toString());
}
}
private View getRowView(String label, int value,ViewGroup parent) {
try {
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.custom_list_pref_row, parent,false);
TextView text = (TextView) row
.findViewById(R.id.custom_list_view_row_text_view);
text.setText(label);
RadioButton rButton = (RadioButton) row
.findViewById(R.id.custom_list_view_row_radio_button);
rButton.setId(value);
row.setTag(value);
row.setClickable(true);
if (value == currentFontSize) {
rButton.setChecked(true);
}
row.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
return row;
} catch (Exception e) {
MessageBox(e.toString());
return null;
}
}
public void MessageBox(String message) {
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Error");
adb.setMessage(message);
adb.setPositiveButton("Ok", null);
adb.show();
}
class CustomAdapter extends ArrayAdapter<FontSize> {
public CustomAdapter(Context context, int resource, List<FontSize> objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//widgets displayed by each item in your list
FontSize rowData = getItem(position);
if (null == convertView) {
convertView = getRowView(rowData.label,rowData.value,parent);
}
return convertView;
}
}
private class FontSize {
public int value;
public String label;
FontSize(int pvalue, String plabel){
value = pvalue;
label = plabel;
}
@Override
public String toString() {
return value + " " + label;
}
}
}
Dies ist das popup-Fenster layout-xml -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/popup_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="demo value"
/>
<ListView
android:layout_height="wrap_content"
android:id="@+id/font_size_list"
android:layout_width="wrap_content"></ListView>
</LinearLayout>
- Wie wäre es mit einer Warnung.Dialog statt Popup-Fenster sollte es funktionieren. Habe es getan, mehrere Male.
InformationsquelleAutor Srini2k6 | 2011-12-07
Schreibe einen Kommentar Antworten abbrechen
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ich hatte das gleiche problem und aufrufen von update das problem behoben. Ich vermute, dass die Breite und Höhe des popup sind noch nicht bekannt. Also in deinem Fall würde der code sein:
Ging ich zusammen mit einem tutorial gefunden hier. Ich denke, dass ich gefunden, was dein problem ist. Aus Ihrer Sicht:
Sind Sie wieder, die einen null-Wert und false. Nicht sicher, was das falsche ist, aber anstelle von null, versuchen Sie eine
ViewGroup
:Hoffe, das funktioniert für Sie.