繁体   English   中英

如何将参数从一个选项卡传递到另一个选项卡并使用按钮更改选项卡?

[英]How to pass a parameter from one tab to another and change the tab with a button?

我解释。 我有一个带有 2 个选项卡的 tabhost,一个带有上下文菜单的 ListView。 菜单有 2 个选项。 我感兴趣的是编辑。 按编辑应该带我到另一个选项卡并允许我编辑数据。

为此,我的想法是使用 Listenerlongclic 获取位置的值并将该位置传递给 ArrayList 以取出对象并在其对应的 TextLayout 中设置每个对象的 getter。 我不知道我是否解释了自己。 我留下代码。

    public class MainActivity extends AppCompatActivity {
private ArrayList datos = new ArrayList();
private ListView listCuadros;
private AdaptadorCuadros adaptador;
private Context contexto;
private int i=0;
private EditText titulo;
private EditText autor;
private EditText estilo;
private EditText precio;
private EditText fecha;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TabHost tabs = findViewById(android.R.id.tabhost);
    tabs.setup();
    TabHost.TabSpec spec = tabs.newTabSpec("mitab1");
    spec.setContent(R.id.tab1);
    spec.setIndicator("Lista del museo");
    tabs.addTab(spec);
    spec = tabs.newTabSpec("mitab2");
    spec.setContent(R.id.tab2);
    spec.setIndicator("Editor");
    tabs.addTab(spec);
    tabs.setCurrentTab(0);
    contexto = this;
    datos.add(new Cuadros("La Mona Lisa", "Leonardo da Vinci", "Renacentista", "$713.000.000", "1503"));
    datos.add(new Cuadros("El grito", "Munch", "Expresionismo", "$119.900.000", "1893"));
    datos.add(new Cuadros("La persistencia de la memoria", "Dalí ", "Surrealismo", "$400.000.000", "1931"));
    datos.add(new Cuadros("El nacimiento de Venus", "Sandro Botticelli", "Gotico", "Desconocido", "1482"));
    datos.add(new Cuadros("Guernica", "Pablo Picasso", "Cubismo", "€300.000.000", "1937"));
    datos.add(new Cuadros("Los jugadores de cartas", "Paul Cézanne", "Posimpresionismo", "€191.000.000", "1890"));
    adaptador = new AdaptadorCuadros(this, datos);
    titulo = findViewById((R.id.titulo));
    autor = findViewById((R.id.autor));
    estilo = findViewById(R.id.estilo);
    precio = findViewById(R.id.precio);
    fecha = findViewById(R.id.fecha);
    listCuadros = findViewById(R.id.listView1);
    listCuadros.setAdapter(adaptador);
    registerForContextMenu(listCuadros);
    listCuadros.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            i = position;
            return false;
        }
});
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mimenu, menu);
}

public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
        case R.id.mi1:
            editar();
            return true;
        case R.id.mi2:
            borrar();
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

private void editar() {

}

private void borrar() {
    String mensaje = "Va a borrar uno de los cuadros, esta seguro?";
    AlertDialog.Builder builder = new AlertDialog.Builder(contexto);
    builder.setTitle("Confirmacion");
    builder.setMessage(mensaje);
    builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Log.i("Dialogos", "Confirmacion Cancelada.");
            dialog.cancel();
        }
    })
    .setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Log.i("Dialogos", "Confirmacion Aceptada.");
             datos.remove(i);
                adaptador.notifyDataSetChanged();
            }

    });
    builder.show();
}

a) 如果你熟悉MVVM ,你可以通过ViewModel

b) 您可以在SharedPreferences保存和使用您的数据。 如何? 例子:

创建公共类,将其命名为SaveSharedPreference

public class SaveSharedPreference {
   static final String USER_NAME = "user_name";

   static SharedPreferences sharedPreferences;

   static SharedPreferences getSharedPreferences(Context ctx) {
        if (sharedPreferences == null) sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ctx);
        return sharedPreferences;
   }


   public static void setUserName(Context ctx, String user_name) {
        SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
        editor.putString(USER_NAME, user_name);
        editor.apply();
   }

   public static String getUserName(Context context) {
        if (null != getSharedPreferences(context).getString(USER_NAME, ""))
            return getSharedPreferences(context).getString(USER_NAME, "");
        else
            return "";
   }
}

保存并使用您在活动(或片段)中传递的数据:

String sValue = "Saving value";
SaveSharedPreference.setUserName(this, sValue);
String tmp = SaveSharedPreference.getUserName(this);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM