有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用捆绑包将数据从适配器共享到viewpager中的片段

我需要获取适配器从活动中返回的数据,以便将其发送给在viewpager中工作的片段

这是我的适配器,它为一些卡片充气,上面有标题和新闻描述

public class FeedAdapter extends CursorAdapter {

/*
Etiqueta de Depuración
 */
private static final String TAG = FeedAdapter.class.getSimpleName();

/**
 * View holder para evitar multiples llamadas de findViewById()
 */
static class ViewHolder {
    TextView titulo;
    TextView descripcion;

    int tituloI;
    int descripcionI;
}

public FeedAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);

}

public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());

    View view = inflater.inflate(R.layout.item_layout, null, false);

    ViewHolder vh = new ViewHolder();

    // Almacenar referencias
    vh.titulo = (TextView) view.findViewById(R.id.titulo);
    vh.descripcion = (TextView) view.findViewById(R.id.descripcion);

    // Setear indices
    vh.tituloI = cursor.getColumnIndex(ScriptDatabase.ColumnEntradas.TITULO);
    vh.descripcionI = cursor.getColumnIndex(ScriptDatabase.ColumnEntradas.DESCRIPCION);

    view.setTag(vh);

    return view;
}

public void bindView(View view, Context context, Cursor cursor) {

    final ViewHolder vh = (ViewHolder) view.getTag();

    // Setear el texto al titulo
    vh.titulo.setText(cursor.getString(vh.tituloI));

    // Obtener acceso a la descripción y su longitud
    int ln = cursor.getString(vh.descripcionI).length();
    String descripcion = cursor.getString(vh.descripcionI);

    // Acortar descripción a 77 caracteres
     vh.descripcion.setText(descripcion);


}
}

这是我活动中的功能,我想在发送带有适配器返回的包的同时创建viewpager

public class LoadData extends AsyncTask<Void, Void, Cursor> {

        @Override
        protected Cursor doInBackground(Void... params) {
            // Carga inicial de registros
            return FeedDatabase.getInstance(MainActivity.this).obtenerEntradas();

        }

        @Override
        protected void onPostExecute(Cursor cursor) {
            super.onPostExecute(cursor);

            // Crear el adaptador
            adapter = new FeedAdapter(
                    MainActivity.this,
                    cursor,
                    SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

            // Relacionar la lista con el adaptador
            Bundle arguments = new Bundle();
            arguments.putString(adapter);

            viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), arguments);
            viewPager.setAdapter(viewPagerAdapter);
        }
    }

}

但每次我使用“arguments.putString”时,它都会返回一个错误。我很困惑,因为我不知道从适配器接收到的数据应该使用哪种“.put”,以及包的实际工作方式

你能帮我吗? 谢谢


共 (2) 个答案

  1. # 1 楼答案

    解决方案:

    首先,适配器必须实现可序列化

    public class FeedAdapter extends CursorAdapter implements Serializable{
    

    有效的捆绑是

    Bundle arguments = new Bundle();
    arguments.putSerializable("datos",adapter);
    

    然后我像这样把它发送到我的viewpager

    viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), arguments);
    viewPager.setAdapter(viewPagerAdapter);
    

    我的viewPager接收如下数据

    public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    private Bundle data;
    
    public ViewPagerAdapter(FragmentManager fm, Bundle data) {
        super(fm);
        this.data = data;
    }
    
    @Override
    public Fragment getItem(int position) {
    
        TabFragment tab1 = new TabFragment();
        tab1.setArguments(data);
    
        return tab1;    // Which Fragment should be displayed by the viewpager for the given position
        // In my case we are showing up only one fragment in all the three tabs so we are
        // not worrying about the position and just returning the TabFragment
    }
    
    @Override
    public int getCount() {
        return 3;           // As there are only 3 Tabs
    }
    

    }

    最后,我的片段获得参数并实现如下适配器

    public class TabFragment extends ListFragment {
    
    private Bundle bundle;
    private FeedAdapter adapter;
    
    
    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bundle = getArguments();
        adapter = (FeedAdapter) bundle.getSerializable("datos");
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View root = inflater.inflate(R.layout.tabs, container, false);
    
        ListView LV = (ListView) root.findViewById(R.id.lista);
        LV.setAdapter(adapter);
        return root;
    }
    

    }

    谢谢

  2. # 2 楼答案

    将其发送到活动并使用getIntent()。将getStringExtra(“名称”)放入片段中