英文:
display list of items on one activity
问题
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import java.util.ArrayList;
public class DataAdapter extends ArrayAdapter<Data> {
private int mColorResourceId;
public DataAdapter(@NonNull Context context, ArrayList<Data> resource, int ColorResourceId) {
super(context, 0, resource);
mColorResourceId = ColorResourceId;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItem = convertView;
if (listItem == null) {
listItem = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
Data currentWord = getItem(position);
TextView placeTextView = (TextView) listItem.findViewById(R.id.placetxt);
placeTextView.setText(currentWord.getPlaceWord());
View textContainer = listItem.findViewById(R.id.list_item);
int color = ContextCompat.getColor(getContext(), mColorResourceId);
textContainer.setBackgroundColor(color);
return listItem;
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.io.Serializable;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements Serializable {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView topAttraction = findViewById(R.id.topAttraction);
topAttraction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent topIntent = new Intent(MainActivity.this, TopAttraction.class);
topIntent.putExtra("attraction", getTopAttraction());
startActivity(topIntent);
}
});
// Similar onClick listeners for other categories
}
public ArrayList<Data> getTopAttraction() {
ArrayList<Data> attraction = new ArrayList<>();
// Add attraction data here
return attraction;
}
}
class Data {
private String placeWord;
public Data(String thePlace) {
placeWord = thePlace;
}
public String getPlaceWord() {
return placeWord;
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class TopAttraction extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_items);
ArrayList<Data> attraction = (ArrayList<Data>) getIntent().getSerializableExtra("attraction");
DataAdapter adapter = new DataAdapter(this, attraction, R.color.category_attraction);
ListView listView = findViewById(R.id.list);
listView.setAdapter(adapter);
}
}
(Note: I've provided the translated code for the parts you requested. If you encounter any issues with the implementation or further questions, feel free to ask.)
英文:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import java.util.ArrayList;
public class DataAdapter extends ArrayAdapter {
private int mColorResourceId;
public DataAdapter(@NonNull Context context, ArrayList<Data> resource ,int ColorResourceId) {
super(context,0, resource);
mColorResourceId = ColorResourceId ;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listitem = convertView;
if( listitem == null){
listitem = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
}
Data currentword = (Data) getItem(position);
TextView pd = (TextView) listitem.findViewById(R.id.placetxt);
pd.setText(currentword.getPlaceWord());
View textContainer = listitem.findViewById(R.id.list_item);
// Find the color that the resource ID maps to
int color = ContextCompat.getColor(getContext(), mColorResourceId);
// Set the background color of the text container View
textContainer.setBackgroundColor(color);
return listitem;
}
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.io.Serializable;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements Serializable {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView topAttraction = (TextView) findViewById(R.id.topAttraction);
topAttraction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent topIntent = new Intent(MainActivity.this,TopAttraction.class);
topIntent.putExtra("attraction" , getTopAttraction());
startActivity(topIntent);
}
});
TextView hosbitals = (TextView) findViewById(R.id.hosbitals);
hosbitals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent hosbitalsIntent = new Intent(MainActivity.this,Hospital.class);
startActivity(hosbitalsIntent);
}
});
TextView resturant = (TextView) findViewById(R.id.resturants);
resturant.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent resturantIntent = new Intent(MainActivity.this,Resturants.class);
startActivity(resturantIntent);
}
});
TextView publicplaces = (TextView) findViewById(R.id.publicplaces);
publicplaces.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent placesIntent = new Intent(MainActivity.this,PublicPlaces.class);
startActivity(placesIntent);
}
});
TextView buses= (TextView) findViewById(R.id.buses);
buses.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent busesIntent = new Intent(MainActivity.this,BusesNumbers.class);
startActivity(busesIntent);
}
});
}
public ArrayList<Data> getTopAttraction(){
ArrayList<Data> attraction = new ArrayList<>();
attraction.add(new Data("Search for Treasures in the Khan al-Khalili."));
attraction.add(new Data("Hit the Beach and Splash in the Red Sea."));
attraction.add(new Data("Admire Ancient Antiquities at the Egyptian Museum."));
attraction.add(new Data("Explore Temples, Temples and More Temples."));
attraction.add(new Data("Take a Sunset Felucca Ride in Aswan."));
attraction.add(new Data("Experience Alexandria’s European Flair."));
attraction.add(new Data("Mix and Mingle with Friendly Locals."));
attraction.add(new Data("It’s one of the cheapest destinations you can travel to."));
attraction.add(new Data("Our world-famous museums."));
attraction.add(new Data("Camping in the Sahara Desert."));
attraction.add(new Data("Religious significance for the three Abrahamic religions -- Islam, Christianity and Judaism."));
attraction.add(new Data("Ancient whale fossils in the desert."));
attraction.add(new Data("Kitesurfing."));
attraction.add(new Data("10,000 year old rock art."));
attraction.add(new Data("Egyptian food."));
attraction.add(new Data("Egypt is a great winter destination. And summer for that matter. \nBasically, it’s an all-year destination."));
return attraction;
}
}
class Data {
private String placeWord;
public Data(String thePlace){
placeWord = thePlace;
}
public String getPlaceWord(){
return placeWord;
}
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class TopAttraction extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_items);
ArrayList<Data> attraction = new ArrayList<Data>();
attraction = (ArrayList<Data>)getIntent().getSerializableExtra("attraction");
DataAdapter adapte = new DataAdapter(this,attraction,R.color.category_attraction);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapte);
}
}
I have 4 categories ,I need when the user click on one of them , the TopAttraction
activity display the data "arraylist attraction" . I tried to implement serializable but, when I run the app it stopped ,I know when I want to send data using intent I should use putExtra method and I was tried more but still not working
10-06 14:04:44.928 13643-13643/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 13643
java.lang.RuntimeException: Parcel: unable to marshal value com.example.myapplication.Data@6aad433
at android.os.Parcel.writeValue(Parcel.java:1397)
at android.os.Parcel.writeList(Parcel.java:738)
at android.os.Parcel.writeValue(Parcel.java:1344)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:665)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
at android.os.Bundle.writeToParcel(Bundle.java:1079)
at android.os.Parcel.writeBundle(Parcel.java:690)
at android.content.Intent.writeToParcel(Intent.java:7797)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2676)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1624)
at android.app.Activity.startActivityForResult(Activity.java:3927)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
at android.app.Activity.startActivityForResult(Activity.java:3888)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
at android.app.Activity.startActivity(Activity.java:4211)
at android.app.Activity.startActivity(Activity.java:4179)
at com.example.myapplication.MainActivity$1.onClick(MainActivity.java:21)
at android.view.View.performClick(View.java:5226)
at android.view.View$PerformClick.run(View.java:21266)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
答案1
得分: 1
// Data类必须是Serializable或Parcelable。对于Android,推荐使用Parcelable。
class Data implements Parcelable {
private String placeWord;
public Data(String thePlace){
placeWord = thePlace;
}
public String getPlaceWord(){
return placeWord;
}
protected Data(Parcel in) {
placeWord = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(placeWord);
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<Data> CREATOR = new Parcelable.Creator<Data>() {
@Override
public Data createFromParcel(Parcel in) {
return new Data(in);
}
@Override
public Data[] newArray(int size) {
return new Data[size];
}
};
}
**或者**
class Data implements Serializable {
private String placeWord;
public Data(String thePlace) {
placeWord = thePlace;
}
public String getPlaceWord() {
return placeWord;
}
}
英文:
The Data class has to be Serializable or Parcelable. For Android Parcelable is recommended.
class Data implements Parcelable {
private String placeWord;
public Data(String thePlace){
placeWord = thePlace;
}
public String getPlaceWord(){
return placeWord;
}
protected Data(Parcel in) {
placeWord = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(placeWord);
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<Data> CREATOR = new Parcelable.Creator<Data>() {
@Override
public Data createFromParcel(Parcel in) {
return new Data(in);
}
@Override
public Data[] newArray(int size) {
return new Data[size];
}
};
}
OR
class Data implements Serializable {
private String placeWord;
public Data(String thePlace) {
placeWord = thePlace;
}
public String getPlaceWord() {
return placeWord;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论