“Справочник рыбака” Урок 14

Код который изменился "Справочник рыбака"

MainActivity.java

package com.neco_desarrollo.fisher_hanbook;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import androidx.core.view.GravityCompat;
import androidx.appcompat.app.ActionBarDrawerToggle;
import android.view.MenuItem;
import com.google.android.material.navigation.NavigationView;
import com.neco_desarrollo.fisher_hanbook.settings.SettingsActivity;
import com.neco_desarrollo.fisher_hanbook.utils.CustomArrayAdapter;
import com.neco_desarrollo.fisher_hanbook.utils.ListItemClass;

import androidx.drawerlayout.widget.DrawerLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

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

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private ListView list;
private String[] array, arraySecName;
private CustomArrayAdapter adapter;
private Toolbar toolbar;
private int category_index;
private int[] array_fish_color = new int []{R.color.green_2, R.color.yellow, R.color.green_2, R.color.red,R.color.red};
private List<ListItemClass> listItemMain;
private ListItemClass listItem;









@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

list = findViewById(R.id.listView);
listItemMain = new ArrayList<>();

fillArray(R.string.fish,getResources().getStringArray(R.array.fish_array),getResources().getStringArray(R.array.fish_array_2),array_fish_color, 0);
adapter = new CustomArrayAdapter(this,R.layout.list_view_item_1,listItemMain,getLayoutInflater());
list.setAdapter(adapter);

DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent intent = new Intent(MainActivity.this,Text_Content_Activity.class);
intent.putExtra("category",category_index);
intent.putExtra("position",position);
startActivity(intent);

}
});


}

@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
toolbar.setTitle(R.string.fish);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {

Intent i = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(i);
}

return super.onOptionsItemSelected(item);
}



@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.id_fish) {

fillArray(R.string.fish,getResources().getStringArray(R.array.fish_array),getResources().getStringArray(R.array.fish_array_2),array_fish_color, 0);
}
else if
(id == R.id.id_na)
{


}
else if (id == R.id.id_sna)
{

}
else if (id == R.id.id_pri)
{

}
else if (id == R.id.id_history)
{

}
else if (id == R.id.id_advice)
{

}

DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void fillArray(int title,String[] nameArray,String[] secName,int[] image,int index )
{

toolbar.setTitle(title);
if(adapter != null) adapter.clear();

for(int i = 0; i < nameArray.length; i++)
{
listItem = new ListItemClass();
listItem.setNameF(nameArray[i]);
listItem.setSecond_name(secName[i]);
listItem.setImage_id(image[i]);

listItemMain.add(listItem);
}
if(adapter != null) adapter.notifyDataSetChanged();
category_index = index;
}
}

ListItemClass.java

package com.neco_desarrollo.fisher_hanbook.utils;

public class ListItemClass {
String name;
String second_name;
int image_id;

public String getNameF() {
return name;
}

public void setNameF(String name) {
this.name = name;
}

public String getSecond_name() {
return second_name;
}

public void setSecond_name(String second_name) {
this.second_name = second_name;
}

public int getImage_id() {
return image_id;
}

public void setImage_id(int image_id) {
this.image_id = image_id;
}
}

CustomArrayAdapter.java

package com.neco_desarrollo.fisher_hanbook.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.neco_desarrollo.fisher_hanbook.R;

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


public class CustomArrayAdapter extends ArrayAdapter<ListItemClass> {
private LayoutInflater inflater;
private List<ListItemClass> listItem = new ArrayList<>();
private Context context;


public CustomArrayAdapter(@NonNull Context context, int resource, List<ListItemClass> listItem,LayoutInflater inflater) {
super(context, resource, listItem);
this.inflater = inflater;
this.listItem = listItem;
this.context = context;

}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
ListItemClass listItemMain = listItem.get(position);
if(convertView == null){
convertView = inflater.inflate(R.layout.list_view_item_1, null, false);
viewHolder = new ViewHolder();
viewHolder.image = convertView.findViewById(R.id.imItem);
viewHolder.name = convertView.findViewById(R.id.tvName);
viewHolder.secName = convertView.findViewById(R.id.tvSecName);
convertView.setTag(viewHolder);



}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.name.setText(listItemMain.getNameF());
viewHolder.secName.setText(listItemMain.getSecond_name());
viewHolder.image.setImageResource(listItemMain.image_id);








return convertView;
}
private class ViewHolder{
TextView name;
TextView secName;
ImageView image;

}
}

arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="fish_array">
<item>Карп</item>
<item>Щука</item>
<item>Сом</item>
<item>Осетр</item>
<item>Налим</item>
</string-array>
<string-array name="fish_array_2">
<item>Карпиус</item>
<item>Щукариус</item>
<item>Сомариус</item>
<item>Осетрариус</item>
<item>Налимиус</item>
</string-array>

<string-array name="na_array">
<item>Червяк</item>
<item>Кукуруза</item>
<item>Хлеб</item>
<item>Рис</item>
</string-array>
<string-array name="sna_array">
<item>Грузила</item>
<item>Крючки</item>
<item>Леска</item>
<item>Блесна</item>
</string-array>
<string-array name="pri_array">
<item>Кукуруза</item>
<item>Хлеб</item>
<item>Рис</item>

</string-array>
<string-array name="history_array">
<item>Рыбак и море</item>
<item>Рыбак и щюка</item>
<item>На дону</item>
<item>Случай на рыбалке</item>

</string-array>
<string-array name="advice_array">
<item>Советы по прикормке</item>
<item>Сезон рыбалки</item>
<item>На что клюет</item>
<item>Большой улов</item>

</string-array>
<string-array name="main_text_size_array">
<item>Большой</item>
<item>Средний</item>
<item>Маленький</item>

</string-array>


</resources>

все остальные файлы без изменений. В файле arrays.xml добавляем если хотим свои собственные названия рыб и.т.д но помним если изменим название самого массива то нужно также поменять это в кодею 

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *