Last active
July 5, 2016 06:56
-
-
Save DANISSIMO9699/3d70af4a0f725c7e039b17169fc798ff to your computer and use it in GitHub Desktop.
code for my app that needs to be fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//my ChordsListActiviy aka MainActivity | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
import android.widget.ImageButton; | |
import java.io.Serializable; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class ChordsListActivity extends Activity implements View.OnClickListener{ | |
private RecyclerView chordsList; | |
private RecyclerView.LayoutManager listLayoutManager; | |
private List<Accordo> chords = new ArrayList<Accordo>(); | |
private Accordo accordo; | |
private ArrayList<Accordo> getChords() { | |
ArrayList<Accordo> chords = new ArrayList<>(); | |
Accordo a=new Accordo(); | |
a.setName("Do maggiore"); | |
a.setNote("Do, Mi, Sol"); | |
a.setImage(R.drawable.do_maggiore); | |
chords.add(a); | |
a=new Accordo(); | |
a.setName("do 5"); | |
a.setNote("na, na, na"); | |
a.setImage(R.drawable.do5); | |
chords.add(a); | |
a=new Accordo(); | |
a.setName("do 6"); | |
a.setNote("na,na,na"); | |
a.setImage(R.drawable.do6); | |
chords.add(a); | |
a=new Accordo(); | |
a.setName("do 7"); | |
a.setNote("na,na,na"); | |
a.setImage(R.drawable.do7); | |
chords.add(a); | |
//many more!! | |
return chords; | |
} | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_chords_list); | |
for(int i=0; i<10; i++) { | |
accordo = new Accordo(); // You've added 3 data in your question, while I've added 10 data for sample code. | |
accordo.setName("My Name is " + i); | |
chords.add(accordo); | |
} | |
findViewById(R.id.tasto_ricerca).setOnClickListener(this); | |
chordsList = (RecyclerView) findViewById(R.id.chords_recycler); | |
//use a linear layout manager | |
listLayoutManager = new LinearLayoutManager(this); | |
chordsList.setLayoutManager(listLayoutManager); | |
ChordsListAdapter adapter = new ChordsListAdapter(this, getChords()); | |
chordsList.setAdapter(adapter); | |
} | |
@Override | |
public void onClick(View view) { | |
switch (view.getId()) { | |
case R.id.tasto_ricerca: | |
Intent intent = new Intent(this, SearchActivity.class); | |
intent.putExtra("data", (Serializable) chords); // This will pass the whole arraylist to search activity | |
startActivity(intent); | |
break; | |
} | |
} | |
} | |
//My SearchActivity that load when the search_button in the ChordsListActivity is pressed | |
public class SearchActivity extends Activity { | |
SearchView sv; | |
private RecyclerView recyclerView; | |
private List<Accordo> list; | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.search_layout); | |
try { | |
list = (List<Accordo>) getIntent().getSerializableExtra("data"); // Here you will get that list using Intent | |
Log.e("data===", list.get(0).getNome()); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
recyclerView = (RecyclerView) findViewById(R.id.lista_ricerca); | |
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); | |
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); | |
recyclerView.setLayoutManager(linearLayoutManager); | |
recyclerView.setHasFixedSize(true); | |
SearchAdapter recyclerViewAdapter = new SearchAdapter(this, list); // Added that arraylist in my recyclerview adapter and that's it.... | |
recyclerView.setAdapter(recyclerViewAdapter); | |
} | |
} | |
//My SearchAdapter | |
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.MyViewHolder> { | |
private List<Accordo> data = new ArrayList<Accordo>(); | |
private Context mContext; | |
public SearchAdapter(Context ctx, List<Accordo> mData) { | |
mContext = ctx; | |
data = mData; | |
} | |
@Override | |
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View view = null; | |
view = LayoutInflater.from(mContext).inflate(R.layout.search_row, parent, false); | |
return new MyViewHolder(view); | |
} | |
@Override | |
public void onBindViewHolder(MyViewHolder holder, int position) { | |
try { | |
if(!data.isEmpty()) { | |
holder.tvName.setText(data.get(position).getNome()); | |
} else { | |
holder.tvName.setText("No data available...."); | |
} | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public int getItemCount() { | |
return data.size(); | |
} | |
//My ViewHolder class | |
public class MyViewHolder extends RecyclerView.ViewHolder { | |
TextView tvName, tvNote; | |
public MyViewHolder(View itemView) { | |
super(itemView); | |
tvName = (TextView) itemView.findViewById(R.id.ricerca_nome); | |
} | |
} | |
} | |
//My SearchFilter | |
public class SearchFilter extends Filter{ | |
SearchAdapter adapter; | |
ArrayList<Accordo> filterList; | |
public SearchFilter(ArrayList<Accordo> filterList, SearchAdapter adapter) { | |
this.adapter=adapter; | |
this.filterList=filterList; | |
} | |
@Override | |
protected FilterResults performFiltering(CharSequence constraint) { | |
FilterResults results = new FilterResults(); | |
//CHECK CONSTRAINT VALIDITY | |
if(constraint!=null && constraint.length()>0){ | |
/**CHANGE TO UPPER*/ | |
constraint=constraint.toString().toUpperCase(); | |
/**STORE THE FILTERED CHORDS*/ | |
ArrayList<Accordo> filteredChords = new ArrayList<>(); | |
for(int i=0; i<filterList.size();i++){ | |
//CHECK | |
if(filterList.get(i).getNome().toUpperCase().contains(constraint)) | |
//ADD CHORD TO FILTEREDCHPRDS | |
filteredChords.add(filterList.get(i)); | |
} | |
results.count = filteredChords.size(); | |
results.values = filteredChords; | |
} else { | |
results.count = filterList.size(); //0 | |
results.values = filterList; //null | |
} | |
return results; | |
} | |
@Override | |
protected void publishResults(CharSequence constraint, FilterResults results) { | |
/** getting an error here because filterList is not in my adapter class anymore*/ | |
adapter.filterList = (ArrayList<Accordo>) results.values; | |
//REFRESH | |
adapter.notifyDataSetChanged(); | |
} | |
} | |
//and finally my Accordo.class | |
public class Accordo implements Serializable{ | |
private String name, note; | |
private int image; | |
/** meodi get e set */ | |
public String getNome() {return name;} | |
public String getNote() {return note;} | |
public int getImage() {return image;} | |
public void setName(String s) {this.name = s;} | |
public void setNote(String n) {this.note = n;} | |
public void setImage(int i) {this.image = i;} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment