i call the dialog in my parent activity.. where as the dialog is in another activity...i faced error while setting the adapter "setListAdapter(new ListViewAdapter(this));"... can any one plz assist me
CustomizeDialog.class
package org.me.dailogfrmchildact;
/** Class Must extends with Dialog */
/** Implement onClickListener to dismiss dialog when OK Button is pressed */
public class CustomizeDialog extends Dialog implements OnClickListener {
Button okButton;
public CustomizeDialog(Context context) {
super(context);
/** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
requestWindowFeature(Window.FEATURE_NO_TITLE);
/** Design the dialog in main.xml file */
setContentView(R.layout.main);
okButton = (Button) findViewById(R.id.OkButton);
okButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
/** When OK Button is clicked, dismiss the dialog */
if (v == okButton){
ListviewContent.add("sadas");
ListviewCount.add("dasasd");
setListAdapter(new ListViewAdapter(this));
}
}
private static ArrayList<String> ListviewContent = new ArrayList<String>();
private static ArrayList<String> ListviewCount = new ArrayList<String>();
private static class ListViewAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ListViewAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return ListviewContent.size();
}
public Object getItem(int position) {
return position;
}
public String getCount(int position) {
return ListviewCount.get(position);
}
public String[] getSizeType(int position) {
String[] str = new String[2];
str[0] = ListviewContent.get(position);
str[1] = ListviewCount.get(position);
return str;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ListContent holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listviewinflate, null);
holder = new ListContent();
holder.text = (TextView) convertView.findViewById(R.id.TextView01);
holder.text.setCompoundDrawables(null, null, null, null);
开发者_Python百科 holder.count = (TextView) convertView.findViewById(R.id.TextView02);
holder.count.setCompoundDrawables(null, null, null, null);
convertView.setTag(holder);
} else {
holder = (ListContent) convertView.getTag();
}
holder.text.setText(ListviewContent.get(position));
holder.count.setText(ListviewCount.get(position));
return convertView;
}
static class ListContent {
TextView text;
TextView count;
}
}
}
mainactivity.class
package org.me.dailogfrmchildact;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
/** Display Custom Dialog */
CustomizeDialog customizeDialog = new CustomizeDialog(this);
customizeDialog.show();
// ToDo add your GUI initialization code here
}
}
You can't just call setListAdapter
in a Dialog because this is a method of Activity
. But what you should do is create a ListView in your layout (right now it is called main
) and assign it an id:
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Now you can find your ListView
in the code and set an adapter for it:
ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(new ListViewAdapter(this));
As far as I know, SetListAdapter
is a method of the class ListView
.
So you have to be extending listActivity
, or call it from an object of listview
I solved the question, here is the answer. Follow the first answer to create a listview with name as usual.
public CustomizeDialog(Context context) {
super(context);
// access the context again...
final Context cs=context;
//then use this "cs" to set the adapter...
list.setAdapter(new ListViewAdapter(cs));
//...
}
精彩评论