문제

일반적으로 저는 Fragment보다는 Intent를 사용하여 작업하는 경우가 많습니다. 왜냐하면 다소 복잡하다는 것을 알았기 때문입니다.배열의 일부 데이터를 표시하기 위해 ListView가 있는 클래스가 있고 이를 Fragment로 변환하고 싶고 새로운 조각화된 클래스에 추가 항목을 전달하고 싶습니다.

리스트뷰 클래스:

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);

            }
        });

    }

}

CustomList 클래스:

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;
    }
}

조각을 사용하여 생성된 세 개의 탭이 있는 기본 클래스가 있고 이 클래스에 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 메소드를 사용하려면 이제 인수에 매개변수를 추가하고 Fragment를 반환하기만 하면 됩니다.당신은 당신의 주장을 확인해야합니다 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