質問

一般に、私はほとんどの場合、フラグメントではなくインテントを使用して作業します。それは、インテントが何らかの形で複雑であると感じたためです。配列からのデータを表示するための ListView を備えたクラスがあり、それをフラグメントで変換したいのですが、新しいフラグメント化されたクラスで余分なものを渡したいと考えています。

リストビュークラス:

public class MainActivity extends Activity {
    ListView list;
    String[] web = { "Google", "Twitter", "Windows", "Bing", "Itunes",
            "Wordpress", "Drupal" };
    Integer[] imageId = { R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher

    };

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

        CustomList adapter = new CustomList(MainActivity.this, web, imageId);
        list = (ListView) findViewById(R.id.list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Toast.makeText(MainActivity.this,
                        "You Clicked at " + web[+position], Toast.LENGTH_SHORT)
                        .show();


                Intent i = new Intent(MainActivity.this, intent.class);
                i.putExtra("name", web[+position]);
                startActivity(i);

            }
        });

    }

}

カスタムリストクラス:

public class CustomList extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] web;
    private final Integer[] imageId;

    public CustomList(Activity context, String[] web, Integer[] imageId) {
        super(context, R.layout.list_single, web);
        this.context = context;
        this.web = web;
        this.imageId = imageId;

    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_single, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);

        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        txtTitle.setText(web[position]);

        imageView.setImageResource(imageId[position]);
        return rowView;
    }
}

フラグメントを使用して作成された 3 つのタブを持つメイン クラスを取得しました。このクラスに ListView を追加したいと思います。

public class LayoutOne extends Fragment {


    public static Fragment newInstance(Context context) {
        LayoutOne f = new LayoutOne();  

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
        return root;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

    }

}

私がやりたいのは、フラグメントクラスでリストビューを表示し、リストアイテムがクリックされたときに新しいインテントを起動して、どのアイテムがクリックされたかを表示することです。

役に立ちましたか?

解決

フラグメントにパラメータを渡したい場合は、特定のパラメータを使用してフラグメントのインスタンスを生成する静的メソッドを作成するのが一般的です。あなたはすでに持っています newInstance メソッドの場合は、引数にパラメータを追加してフラグメントを返すだけです。引数を確認する必要があります onCreateView 以下は、その書き方の例です。

public class LayoutOne extends Fragment 
{

    public static Fragment newInstance(int someInt, String someString) 
    {
        LayoutOne f = new LayoutOne();

        Bundle args = new Bundle();
        args.putInt("someInt", someInt);
        args.putString("someString", someString);
        f.setArguments(args);

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
        Bundle args = getArguments();

        //From here you would check to see if your arguments are present 
        //proceed accordingly

        return root;

    }
}

特定のケースでは、String 配列を渡して引数として追加できます。

お役に立てれば。幸運を!

編集

あなたが直面している問題は、 ListView あなたの中で Activity 実際には自分の中でそれを行う必要があるとき Fragment. 。ここではインテントは必要ありません。必要なのは、 ListView に添付するレイアウトから取得する属性 Fragment. 。以下は、それがどのように機能するかを示す小さな例です。

このメソッドは LayoutOne クラス

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
{
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);

    //Be sure to use the actual id of your list view
    ListView lv = (ListView) root.findViewById(R.id.list_view_id);

    //Make sure that you retrieve the values for 'web' and 'imageId' from 
    //the activity using the "newInstance" method.
    CustomList adapter = new CustomList(getActivity(), web, imageId);
    lv.setAdapter(adapter);

    //From here, set your onClickListener and stuff as you did in your Activity

    return root;

}

注意点としては、 Fragmentは持っていません findViewById 方法。で呼び出す必要があります View そのレイアウトになるようにインフレートしているため、この場合はインフレートしました root そしてあなたは電話するでしょう root.findViewById(R.id.some_id) 景色を眺めるために。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top