显示项目列表在一个活动中

huangapple go评论80阅读模式
英文:

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&lt;Data&gt; 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(&quot;attraction&quot; , 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&lt;Data&gt; getTopAttraction(){
ArrayList&lt;Data&gt; attraction = new ArrayList&lt;&gt;();
attraction.add(new Data(&quot;Search for Treasures in the Khan al-Khalili.&quot;));
attraction.add(new Data(&quot;Hit the Beach and Splash in the Red Sea.&quot;));
attraction.add(new Data(&quot;Admire Ancient Antiquities at the Egyptian Museum.&quot;));
attraction.add(new Data(&quot;Explore Temples, Temples and More Temples.&quot;));
attraction.add(new Data(&quot;Take a Sunset Felucca Ride in Aswan.&quot;));
attraction.add(new Data(&quot;Experience Alexandria’s European Flair.&quot;));
attraction.add(new Data(&quot;Mix and Mingle with Friendly Locals.&quot;));
attraction.add(new Data(&quot;It’s one of the cheapest destinations you can travel to.&quot;));
attraction.add(new Data(&quot;Our world-famous museums.&quot;));
attraction.add(new Data(&quot;Camping in the Sahara Desert.&quot;));
attraction.add(new Data(&quot;Religious significance for the three Abrahamic religions -- Islam, Christianity and Judaism.&quot;));
attraction.add(new Data(&quot;Ancient whale fossils in the desert.&quot;));
attraction.add(new Data(&quot;Kitesurfing.&quot;));
attraction.add(new Data(&quot;10,000 year old rock art.&quot;));
attraction.add(new Data(&quot;Egyptian food.&quot;));
attraction.add(new Data(&quot;Egypt is a great winter destination. And summer for that matter. \nBasically, it’s an all-year destination.&quot;));
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&lt;Data&gt; attraction = new ArrayList&lt;Data&gt;();
attraction = (ArrayList&lt;Data&gt;)getIntent().getSerializableExtra(&quot;attraction&quot;);
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(&quot;unused&quot;)
public static final Parcelable.Creator&lt;Data&gt; CREATOR = new Parcelable.Creator&lt;Data&gt;() {
@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;
}
}

huangapple
  • 本文由 发表于 2020年10月6日 19:07:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64224555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定