A
A
Alisher Taubaldy2021-05-20 12:01:06
Java
Alisher Taubaldy, 2021-05-20 12:01:06

How to get convertView from BookAdapter?

package com.example.diplom;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.SearchView;

import com.github.barteksc.pdfviewer.PDFView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ListActivity {

    private String[] Booknames = {"Зов Ктулху", "Случай Чарльза Декстера Варда", "Дагон", "Ужас Данвича", "Обитающий во мраке", "Тень над Иннсмутом",
            "Изгой", "Крысы в стенах", "Реаниматор", "Хребты безумия", "Шепчущий во тьме"};

    private int[] Description = {R.string.ktulhu, R.string.charles,
            R.string.dagon, R.string.dunwich, R.string.dwellers,
            R.string.innsmouth, R.string.outcast,
            R.string.rats, R.string.reanimator,
            R.string.ridges, R.string.whisperers};

    int[] mImageIds = {R.drawable.ktulhu, R.drawable.charles,
            R.drawable.dagon, R.drawable.dunwich, R.drawable.dwellers,
            R.drawable.innsmouth, R.drawable.outcast, R.drawable.rats,
            R.drawable.reanimator, R.drawable.ridges, R.drawable.whisperers};

    private BookAdapter mAdapter;

    ArrayAdapter<String> arrayAdapter;

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

        mAdapter = new BookAdapter(this);
        setListAdapter(mAdapter);

        arrayAdapter = new ArrayAdapter<>(this, R.layout.list_item);
        this.convertView

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu, menu);
        MenuItem menuItem = menu.findItem(R.id.search_icon);
        SearchView searchView = (SearchView) menuItem.getActionView();
        searchView.setQueryHint("search here!");

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                arrayAdapter.getFilter().filter(s);
                return true;
            }
        });

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        String selection = mAdapter.getString(position);
        Toast.makeText(this, Booknames[position], Toast.LENGTH_LONG).show();
        switch (Booknames[position]){
            case "Зов Ктулху":
                Intent ktulhu = new Intent(MainActivity.this, ReadingActivity.class);
                ktulhu.putExtra("name", Booknames[position]);
                startActivity(ktulhu);
                break;
            case "Случай Чарльза Декстера Варда":
                Intent charles = new Intent(MainActivity.this, ReadingActivity.class);
                charles.putExtra("name", Booknames[position]);
                startActivity(charles);
                break;
            case "Дагон":
                Intent dagon = new Intent(MainActivity.this, ReadingActivity.class);
                dagon.putExtra("name", Booknames[position]);
                startActivity(dagon);
                break;
            case "Ужас Данвича":
                Intent dunwich = new Intent(MainActivity.this, ReadingActivity.class);
                dunwich.putExtra("name", Booknames[position]);
                startActivity(dunwich);
                break;
            case "Обитающий во мраке":
                Intent dwellers = new Intent(MainActivity.this, ReadingActivity.class);
                dwellers.putExtra("name", Booknames[position]);
                startActivity(dwellers);
                break;
            case "Тень над Иннсмутом":
                Intent innsmouth = new Intent(MainActivity.this, ReadingActivity.class);
                innsmouth.putExtra("name", Booknames[position]);
                startActivity(innsmouth);
                break;
            case "Изгой":
                Intent outcast = new Intent(MainActivity.this, ReadingActivity.class);
                outcast.putExtra("name", Booknames[position]);
                startActivity(outcast);
                break;
            case "Крысы в стенах":
                Intent rats = new Intent(MainActivity.this, ReadingActivity.class);
                rats.putExtra("name", Booknames[position]);
                startActivity(rats);
                break;
            case "Реаниматор":
                Intent reanimator = new Intent(MainActivity.this, ReadingActivity.class);
                reanimator.putExtra("name", Booknames[position]);
                startActivity(reanimator);
                break;
            case "Хребты безумия":
                Intent ridges = new Intent(MainActivity.this, ReadingActivity.class);
                ridges.putExtra("name", Booknames[position]);
                startActivity(ridges);
                break;
            case "Шепчущий во тьме":
                Intent whisperers = new Intent(MainActivity.this, ReadingActivity.class);
                whisperers.putExtra("name", Booknames[position]);
                startActivity(whisperers);
                break;
        }
    }

    private class BookAdapter extends BaseAdapter {
        private LayoutInflater mLayoutInflater;

        BookAdapter(Context context) {
            mLayoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return Booknames.length;
        }

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

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null)
                convertView = mLayoutInflater.inflate(R.layout.list_item, null);

            ImageView image = (ImageView) convertView.findViewById(R.id.image_view_icon);
            image.setImageResource(mImageIds[position]);

            TextView signTextView = (TextView) convertView.findViewById(R.id.title);
            signTextView.setText(Booknames[position]);

            TextView dateTextView = (TextView) convertView.findViewById(R.id.description);
            dateTextView.setText(Description[position]);

            return convertView;
        }

        String getString(int position) {
            return Booknames[position];
        }
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexandr_voloshin, 2021-05-21
@alexandr_voloshin

1. The code is not optimized
2. Getting a view from the adapter is fraught with a memory leak, and why such a mockery?
I can assume that you need to get all the item data (Description, Booknames), then return it via callback

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question