I have this code using Android Studio, trying to send data from fragment to activity but I am getting error. I am using CardView
with list, but when I try to get more information from fragment when pressing the ListView
, it is giving me an error and the app crashes.
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"));
}
}
You can start the Activity from Fragment this way and you don't need to actually because the activity already exists and holding the fragment.
What you should do is make a callback interface like the following:
public interface PlacesInterface {
void onListViewItemClicked(Word word);
}
Then you implement this interface by the InformationActivity like this:
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
}
}
And finally inside the PlacesFragment do the following:
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;
}
}
Get item by position from your adapter in item click listener....Try this....
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.