This won't be released publicly, but that doesn't mean I want to have bad coding habits. I was not too far into this before realizing I could make it better. One of my main concerns is finding an alternative to the if
statement halfway down. The toast message is temporary as I was having trouble with something earlier in my development (48 hours ago).
package com.andyroo.mcrecipefinder;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AlphabetIndexer;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;
public class MyActivity extends ActionBarActivity {
/** Defined variables START **/
ListView listView;
ImageView image;
EditText editsearch;
List<String> recipes;
TextView itemname;
/** Defined variables END**/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
/** Defined Variables START **/
listView = (ListView) findViewById(R.id.list);
image = (ImageView) findViewById(R.id.imageView);
editsearch = (EditText) findViewById(R.id.editText);
itemname = (TextView) findViewById(R.id.textView3);
/** Defined Variables END **/
/** Array listview START **/
recipes = new ArrayList<String>();
String blocks[] = {"Chest","Bed","Wool","Wood Planks","Redstone Torch","Redstone Lamp","Redstone Repeater"};
for(int blockC = 0; blockC < blocks.length; blockC++){
recipes.add(blocks[blockC]);
}
/** Array listview END **/
String mcOreRecipe[] = {"Wood", "Leather", "Stone", "Iron", "Gold", "Diamond"};
String armorType[] = {"Chestplate", "Leggings", "Helmet", "Boots", "Hatchet",
"Sword", "Pickaxe", "Shovel", "Hoe"};
/** compile iron, diamond, & gold weapons/armor/tools **/
for (int num2 = 3; num2 < 6; num2++){
for (int num3 = 0; num3 < 9; num3++){
recipes.add(mcOreRecipe[num2]+" "+armorType[num3]);
}
}
/** compile leather armor **/
for(int num = 0; num < 4; num++){
recipes.add(mcOreRecipe[1]+" "+armorType[num]);
}
/** compile wood/stone weapons/tools **/
for(int num1 = 4; num1 < 9; num1++){
recipes.add(mcOreRecipe[2]+" "+armorType[num1]);
recipes.add(mcOreRecipe[0]+" "+armorType[num1]);
}
Collections.sort(recipes);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this , android.R.layout.simple_list_item_1 , recipes);
listView.setAdapter(adapter);
listView.setFastScrollEnabled(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String item = ((TextView)view).getText().toString();
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
if (itemValue.compareTo("Chest") == 0){
image.setImageResource(R.drawable.recipe_chest);
itemname.setText(itemValue);
}
if (itemValue.compareTo("Redstone Torch") == 0){
image.setImageResource(R.drawable.recipe_redstonetorch);
itemname.setText(itemValue);
}
if (itemValue.compareTo("Gold Chestplate") == 0){
image.setImageResource(R.drawable.recipe_goldchestplate);
itemname.setText(itemValue);
}
}
});
editsearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
ArrayList<String> temp = new ArrayList<String>();
int textlength = editsearch.getText().length();
temp.clear();
for (int i = 0; i < recipes.size(); i++){
if (textlength <= recipes.get(i).length()){
if(editsearch.getText().toString().equalsIgnoreCase((String) recipes.get(i).subSequence(0,textlength))){
temp.add(recipes.get(i));
}
}
}
ArrayAdapter<String> tempadapter = new ArrayAdapter<String>(MyActivity.this, android.R.layout.simple_list_item_1, temp);
listView.setAdapter(tempadapter);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}