使用ListView将数据从片段传递到活动

时间:2020-09-12 21:48:36

标签: android fragment

我有使用Android Studio的这段代码,试图将数据从片段发送到活动,但出现错误。 我正在将CardView与list配合使用,但是当我尝试按ListView来从片段中获取更多信息时,它给我一个错误,应用程序崩溃了。

PlacesFragment

public class PlacesFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.list_view, container, false);
        final ArrayList<Word> words = new ArrayList<Word>();
        words.add(new Word(R.string.place1,R.string.about1,R.string.address1,R.string.time1,R.drawable.medinam1));
        words.add(new Word(R.string.place2,R.string.about2,R.string.address2,R.string.time2,R.drawable.medinam2));
        words.add(new Word(R.string.place3,R.string.about3,R.string.address3,R.string.time3,R.drawable.medinam3));



        WordAdapter adapter = new WordAdapter(getActivity(), words);
        ListView listView = (ListView)rootView.findViewById(R.id.listView);
        listView.setAdapter(adapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Word clickedCard = (Word) parent.getItemAtPosition(position);
                //Intent intent = new Intent(PlacesFragment.this, InformationActivity.class);
                Intent intent = new Intent(getActivity(),InformationActivity.class);
                intent.putExtra("pic_name", clickedCard.getImage());
                intent.putExtra("info_name", clickedCard.getName());
                intent.putExtra("some_information", clickedCard.getInfo());
                intent.putExtra("addressTextView", clickedCard.getAddress());
                intent.putExtra("timeTextView", clickedCard.getTime());
                startActivity(intent);
            }
        });
        return rootView;
    }

}

InformationActivity

public class InformationActivity extends AppCompatActivity {

    private ImageView imageView;
    private TextView nameOfpic;
    private TextView nameOfInfo;
    private TextView nameOfAdress;
    private TextView nameOfTime;

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

        imageView = findViewById(R.id.pic_name);
        nameOfpic =  findViewById(R.id.info_name);
        nameOfInfo = findViewById(R.id.some_information);
        nameOfAdress =  findViewById(R.id.addressTextView);
        nameOfTime =  findViewById(R.id.timeTextView);

        Intent intent = getIntent();
        imageView.setImageResource(intent.getIntExtra("pic_name",R.id.pic_name));
        nameOfpic.setText(intent.getStringExtra("info_name"));
        nameOfInfo.setText(intent.getStringExtra("some_information"));
        nameOfAdress.setText(intent.getStringExtra("addressTextView"));
        nameOfTime.setText(intent.getStringExtra("timeTextView"));

    }
}

2 个答案:

答案 0 :(得分:0)

您可以通过这种方式从“片段”开始活动,而实际上并不需要,因为该活动已经存在并保存了片段。

您应该做的是创建一个如下所示的回调接口:

public interface PlacesInterface {
    void onListViewItemClicked(Word word);
}

然后您通过InformationActivity来实现此接口,如下所示:

public class InformationActivity extends AppCompatActivity implements PlacesInterface {

    private ImageView imageView;
    private TextView nameOfpic;
    private TextView nameOfInfo;
    private TextView nameOfAdress;
    private TextView nameOfTime;

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

        imageView = findViewById(R.id.pic_name);
        nameOfpic =  findViewById(R.id.info_name);
        nameOfInfo = findViewById(R.id.some_information);
        nameOfAdress =  findViewById(R.id.addressTextView);
        nameOfTime =  findViewById(R.id.timeTextView);

        Intent intent = getIntent();
                imageView.setImageResource(intent.getIntExtra("pic_name",R.id.pic_name));
        nameOfpic.setText(intent.getStringExtra("info_name"));
        nameOfInfo.setText(intent.getStringExtra("some_information"));
        nameOfAdress.setText(intent.getStringExtra("addressTextView"));
        nameOfTime.setText(intent.getStringExtra("timeTextView"));

    }
    @Override
    public void onDataStateChange(Word word) {
        //Do what you want inside the activity with data passed from the clicked Item
    }
}

最后在PlacesFragment内执行以下操作:

public class PlacesFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.list_view, container, false);
        final ArrayList<Word> words = new ArrayList<Word>();
        words.add(new Word(R.string.place1,R.string.about1,R.string.address1,R.string.time1,R.drawable.medinam1));
        words.add(new Word(R.string.place2,R.string.about2,R.string.address2,R.string.time2,R.drawable.medinam2));
        words.add(new Word(R.string.place3,R.string.about3,R.string.address3,R.string.time3,R.drawable.medinam3));

        WordAdapter adapter = new WordAdapter(getActivity(), words);
        ListView listView = (ListView)rootView.findViewById(R.id.listView);
        listView.setAdapter(adapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Word clickedCard = (Word) parent.getItemAtPosition(position);
                //HERE IS THE NEW PART, PAY ATTENTION
                ((PlacesInterface)getActivity).onListViewItemClicked(clickedWord);
            }
        });
        return rootView;
    }
}

答案 1 :(得分:0)

从适配器的项单击侦听器中按位置获取项。尝试此操作。

public class PlacesFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.list_view, container, false);
    final ArrayList<Word> words = new ArrayList<Word>();
    words.add(new Word(R.string.place1,R.string.about1,R.string.address1,R.string.time1,R.drawable.medinam1));
    words.add(new Word(R.string.place2,R.string.about2,R.string.address2,R.string.time2,R.drawable.medinam2));
    words.add(new Word(R.string.place3,R.string.about3,R.string.address3,R.string.time3,R.drawable.medinam3));



    final WordAdapter adapter = new WordAdapter(getActivity(), words);
    ListView listView = (ListView)rootView.findViewById(R.id.listView);
    listView.setAdapter(adapter);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Word clickedCard = adapter.getItem(position);
            //Intent intent = new Intent(PlacesFragment.this, InformationActivity.class);
            Intent intent = new Intent(getActivity(),InformationActivity.class);
            intent.putExtra("pic_name", clickedCard.getImage());
            intent.putExtra("info_name", clickedCard.getName());
            intent.putExtra("some_information", clickedCard.getInfo());
            intent.putExtra("addressTextView", clickedCard.getAddress());
            intent.putExtra("timeTextView", clickedCard.getTime());
            startActivity(intent);
        }
    });
    return rootView;
}