How to display only objects set to true in a new activity

0

I have here an arraylist that is shown in a listview, when I give a long click a dialog is displayed and contains the favorite option that when it is selected the object is marked as true, how do I display only the items marked in my ActivityFavorites ?

MainActivity.class

public class MainActivity extends AppCompatActivity {

    ListView lv;
    MediaPlayer mp;
    ArrayList<memes> item;
    ArrayAdapter<memes> arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_main);

        lv = findViewById(R.id.lv);
        mp = new MediaPlayer();

        item = new ArrayList<>();
        //itens
        item.add(new memes("Fique apertado sobre o meme para compartilhar", R.raw.sharebagui));
        item.add(new memes("2 mil anos", R.raw.milanos));
        item.add(new memes("Acelera jesus", R.raw.acelera_jesus));
        item.add(new memes("Azideia", R.raw.asideia));
        item.add(new memes("Acertou mizeravi", R.raw.mizeravi));

        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, item);
        lv.setAdapter(arrayAdapter);

        //play audio
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                playSong(position);
            }
        });
  //PROGRESS

        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int position, long l) {
           //PROGRESS
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Olá, Marilene!");
            builder.setItems(Nomes, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                        case 0: // Delete
                            sendWhatsAppAudio(position);
                            break;
                        case 1: // Copy
                            item.get(position).setmIsFavourite(true);
                            break;
                        default:
                            break;
                    }
                }
            });


            AlertDialog alertDialog = builder.create();
            builder.show();

            return true;

        }
    });
    }

    public void playSong(int songIndex) {

        mp.reset();
        mp = MediaPlayer.create(this, arrayAdapter.getItem(songIndex).getResId());

        mp.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mp.release();
    }

}

memes.class

    public class memes{

    private String nome;
    private int resID;
    private Boolean mIsFavourite;

    memes(String nome, int resID){

        this.nome = nome;
        this.resID = resID;
    }

    public String getNome(){
        return nome;
    }

    int getResId(){
        return resID;
    }

    @Override
    public String toString(){
        return nome;
    }

    public Boolean getmIsFavourite() {
        return mIsFavourite;
    }

    public void setmIsFavourite(Boolean mIsFavouriteResource) {
        this.mIsFavourite = mIsFavouriteResource;
    }

}

ActivityFavorites.class

public class ActivityFavoritos {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_favoritos);

        lv = findViewById(R.id.lvf);

        if (list.get(position).getmIsFavourite()) {
            //do want you want when its true
        } else {
            //do want your code when its false.
        }

        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, item);
        lv.setAdapter(arrayAdapter);
    }
}
    
asked by anonymous 17.01.2018 / 23:05

1 answer

0

See the following examples:

First you should create your own layout for the list item:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.LinearLayoutCompat
        xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="@dimen/margin_list_item"
    android:background="@color/colorItemListView"
    android:orientation="horizontal"

    >

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        app:cardElevation="4dp"
        app:cardPreventCornerOverlap="true"
        app:cardUseCompatPadding="true"
        app:cardCornerRadius="2dp"
        android:paddingBottom="@dimen/item_list_padding"
        android:paddingTop="@dimen/item_list_padding"


        >

        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/questionMoreOptions"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="right|top"
            android:src="@drawable/icon_more"
            android:padding="@dimen/appbar_padding_top"

            />

        <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/colorDialogGridLayout"
            android:columnCount="4"
            android:orientation="vertical"
            android:useDefaultMargins="true"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"

            >



            <!-- Primeira Parte (Números de Visualizações e Respostas -->
            <android.support.v7.widget.AppCompatImageView
                android:id="@+id/appCompatImageView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/icon_message"
                android:layout_column="0"
                android:layout_row="0"
                android:layout_gravity="center_horizontal|center_vertical"

                />

            <TextView
                android:id="@+id/numberAnswers"
                style="@style/textLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_row="1"
                android:gravity="center_horizontal|center_vertical"
                android:layout_gravity="top|center_horizontal"


                />


            <android.support.v7.widget.AppCompatImageView
                android:id="@+id/appCompatImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/icon_eye"
                android:layout_column="1"
                android:layout_row="0"
                android:layout_gravity="center_horizontal|center_vertical"


                />

            <TextView
                android:id="@+id/numberViews"
                style="@style/textLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/sizeAnswersExample"
                android:layout_column="1"
                android:layout_row="1"
                android:gravity="center_horizontal|center_vertical"
                android:layout_gravity="top|center_horizontal"

                />


            <!-- Fim da Primeira Parte -->

            <TextView
                android:id="@+id/titleQuestion"
                style="@style/textPrimary"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="2"
                android:layout_row="0"
                android:layout_columnSpan="2"
                android:gravity="left|top"
                android:layout_gravity="left"
                android:ellipsize="end"

                />



            <TextView
                android:id="@+id/titleArea"
                style="@style/textLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="2"
                android:layout_row="1"
                android:lines="1"
                android:scrollHorizontally="false"
                android:gravity="left|top"
                android:layout_gravity="left|top"
                android:ellipsize="end"
                android:layout_columnSpan="1"






                />

            <TextView
                android:id="@+id/nameUserQuestion"
                style="@style/textLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="3"
                android:layout_row="1"
                android:lines="1"
                android:layout_columnWeight="2"
                android:gravity="right|top"
                android:layout_gravity="right"

                />



        </GridLayout>


    </android.support.v7.widget.CardView>


</android.support.v7.widget.LinearLayoutCompat>

You must implement your own adapter, in the getView () method you must put the logic to hide the item from the list, it is in this method that the view representing each item is generated, I'm using the ViewHolder pattern to tune up better performance by reusing the already created items. To hide the list item I suggest you take a look at this topic: link

public class QuestionsListViewAdapter extends BaseAdapter {
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();

private String baseUrl;
private List<Question> questions;
private Activity activity;

public QuestionsListViewAdapter(List<Question> questions, Activity activity) {

    this.activity = activity;
    this.questions = questions;

    baseUrl = activity.getResources().getString(R.string.SERVER_URL) + activity.getResources().getString(R.string.SERVER_QUESTIONS) + "deleteQuestion";

}

@Override
public int getCount() {
    return questions.size();
}

@Override
public Object getItem(int position) {
    return questions.get(position);
}

@Override
public long getItemId(int position) {
    return questions.get(position).getId();
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder;

    Question question = questions.get(position);


    if (convertView == null) {

        viewHolder = new ViewHolder();

        LayoutInflater inflater = LayoutInflater.from(activity.getBaseContext());


        convertView = inflater.inflate(R.layout.item_list_questions, parent, false);

        viewHolder.numberAnswers = (TextView) convertView.findViewById(R.id.numberAnswers);
        viewHolder.numberViews = (TextView) convertView.findViewById(R.id.numberViews);
        viewHolder.titleQuestion = (TextView) convertView.findViewById(R.id.titleQuestion);
        viewHolder.titleArea = (TextView) convertView.findViewById(R.id.titleArea);
        viewHolder.nameUser = (TextView) convertView.findViewById(R.id.nameUserQuestion);
        viewHolder.moreOptions = (AppCompatImageView) convertView.findViewById(R.id.questionMoreOptions);

        viewHolder.moreOptions.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showPopup(v, position);
            }
        });

        convertView.setTag(viewHolder);

    } else {

        viewHolder = (ViewHolder) convertView.getTag();

    }
    try {

        viewHolder.numberAnswers.setText(String.valueOf(question.getNumberAnswers()));
        viewHolder.numberViews.setText(String.valueOf(question.getNumberViews()));
        viewHolder.titleQuestion.setText(String.valueOf(question.getTitleQuestion()));
        viewHolder.titleArea.setText(question.getTitleArea());
        viewHolder.nameUser.setText(String.valueOf("@" + question.getUser().getUserKey()));



    }catch (Exception e){

    }

    return convertView;
}

Then just create an instance of your Adapter in the target Activity, and set it in your listiview.

    
18.01.2018 / 14:07