James Lee James Lee - 28 days ago 12
Android Question

trouble with View or something in Android

I am currently in trouble with my project.

I am trying to separate functions into different classes instead of putting all in Activity class.

So I think I need to pass "view" from SettingsActivity to ConfigPresenter, from ConfigPresenter to ConfigEventHandler.

but I don not know why it doesn't work at all.

can anyone help me to know what the problem is?

Appreciate all the time and sorry that I only come stackoverflow to get information.

I hope I become a professional someday and help people here. :)

I make errors bold below.




SettingsActivity.java

- configPresenter.optionClicked **(view)**

ConfigPresenter.java

- eh.checkOption **(view)**

ConfigEventHandler.java

- AlertDialog.Builder builder = new AlertDialog.Builder **(SettingsActivity.class)**
- LayoutInflater inflater = SettingsActivity.class.**getLayoutInflater()**
- TextView titleUrl = (TextView) **findViewById**(title);
- TextView optionUrl = (TextView) **findViewById**(option);





SettingsActivity.java

public class SettingsActivity extends AppCompatActivity implements ConfigPresenter.View {

private ConfigPresenter configPresenter;
private Context context = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
}

public void optionClicked(View view) {
configPresenter.optionClicked(view);
}
}


ConfigPresenter.java

public class ConfigPresenter {
private View view;
private ConfigEventHandler eh;
private ConfigFileHandler fh;

public ConfigPresenter(ConfigPresenter.View view) {
this.view = view;
eh = new ConfigEventHandler();
fh = new ConfigFileHandler();
}

public void optionClicked(View view) {
eh.checkOption(view);
Log.d("Config", "Presenter");
}
}


ConfigEventHandler.java

public class ConfigEventHandler {

public void checkOption(View view) {
if ( view.getId() == R.id.layout_url ) {
showDialog(R.id.title_url, R.id.option_url);
} else if ( view.getId() == R.id.layout_port ) {
showDialog(R.id.title_port, R.id.option_port);
} else {
showDialog(R.id.title_path, R.id.option_path);
}
}

public void showDialog(int title, int option) {
AlertDialog.Builder builder = new AlertDialog.Builder(SettingsActivity.class);

LayoutInflater inflater = SettingsActivity.class.getLayoutInflater();
View content = inflater.inflate(R.layout.dialog, null);

builder.setView(content);

TextView titleUrl = (TextView) findViewById(title);
TextView dialogTitle = (TextView) content.findViewById(R.id.dialog_title);
TextView optionUrl = (TextView) findViewById(option);
EditText dialogOption = (EditText) content.findViewById(R.id.dialog_option);
dialogTitle.setText(titleUrl.getText());
dialogOption.setText(optionUrl.getText());

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// ok
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// cancel
}
})
.show();
}
}

Answer

First of all, your classes aren't logical, but a few fixes for your current setup.

public class SettingsActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setcontentview etc.
}

public void optionClicked(View view){
    ConfigPresenter.optionClicked(view,getLayoutInflater(),this);
}
}

ConfigPresenter

public class ConfigPresenter {

public static void optionClicked(View view, LayoutInflater inflater, Context context){
    ConfigEventHandler.checkOption(view,inflater,context);
}
}

ConfigEventHandler

public class ConfigEventHandler {

public static void checkOption(View view, LayoutInflater inflater, Context context){
    showDialog(inflater,context);
}

private static void showDialog(LayoutInflater inflater, Context context){
    AlertDialog dialog = new AlertDialog.Builder(context).create();
    View content = inflater.inflate(R.layout.alert_add_item,null);

    dialog.setContentView(content);
    dialog.show();
}

}
Comments