Transfer data from customdialog class to parent class
here i call the customdialog through my parent activity.. where as the dialog is in another activity contains listview...from that dialog i transfer a selected content from listview.. but fail to achieve my target.. and tried so many way.. can any one assist me to achieve it. Note: i got error in on listitemclick listener...
customdialog.class
public class CustomizeDialog extends Dialog {
org.me.dailogfrmchildact.MainActivity ma;
Button cancelButton;
ListView list;
int item_pos;
public CustomizeDialog(Context context) {
super(context);
Context cs = context;
/** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog);
ma=new MainActivity();
l开发者_StackOverflow社区ist = (ListView) findViewById(R.id.list);
cancelButton = (Button) findViewById(R.id.OkButton);
ListviewContent.add("item1");
ListviewCount.add("20");
ListviewContent.add("item2");
ListviewCount.add("30");
list.setAdapter(new ListViewAdapter(cs));
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
item_pos = pos;
System.out.println("===========item_pos" + pos);
ma.txt.setText(ListviewContent.get(pos).toString());
dismiss();
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
dismiss();
}
});
}
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);
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:
public class MainActivity extends Activity {
org.me.dailogfrmchildact.CustomizeDialog cd;
Button btn;
EditText txt;
CustomizeDialog customizeDialog;
boolean click = false;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
cd = new CustomizeDialog(this);
btn = (Button) findViewById(R.id.btn1);
txt = (EditText) findViewById(R.id.txt1);
/** Display Custom Dialog */
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
customizeDialog = new CustomizeDialog(MainActivity.this);
customizeDialog.show();
}
});
}
}
The problem is you're creating a new MainActivity in your CustomizeDialog when you should be using the one that created the CustomizeDialog in the first place.
Have CustomizeDialog take in a MainActivity instead of Context and set ma equal to that.
Edit: Change CustomizeDialog's constructor to:
public CustomizeDialog(MainActivity mainActivity) {
super(mainActivity);
ma = mainActivity; // Instead of ma = new MainActivity()
// everything else is the same
}
精彩评论