I have the following code but I have tried for days now and I cannot seem to figure out how to start an activity based on the listview item clicked, the result[position] does not work for start intent.
public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
EDIT : If the first list item is clicked I want it to start an activity (images class) , if second clicked to start images2 class and so on..
Starting an activity based on a ListView's item click is not done in the adapter. It is done in the Activity where you show the ListView:
In your Activity:
mListView.setAdapter(mAdapter); //mAdapter is your CustomAdapter
mListView.setClickable(true);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Start the activity here.
//based on the value of position or id.
}
});
Please try this in onclick
if(position ==1){
--fire your first intent here.--
} else if(position ==2) {
-------your other intent------
}
----etc
Try something like this
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(v.getContext(),ActivityYouWantToGo.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
v.getContext().startActivity(intent);
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
EDIT
Just create this method on your BaseAdapter
public void StartActivityForImages(Context context, int i){
switch (i){
case 0:
Intent Intent = new Intent(context, Image0.class);
context.startActivity(Intent);
break;
case 1:
Intent Intent2 = new Intent(context, Image1.class);
context.startActivity(Intent2);
break;
case 2:
Intent Intent3 = new Intent(context, Image3.class);
context.startActivity(Intent3);
break;
......
}
}
And on your setOnClickListener()
You call this method as follows :
StartActivityForImages(v.getContext(),position);
Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show();
Efficient way to do it
If you don't want to create a method and do this stuff, you could do something like this :
Create a String with the name of your next class like :
String NextActivity = getPackageName()+".Image"+position;
And then you can do something like this :
try {
String className =getPackageName()+".Image"+2;
Intent openNewIntent = new Intent(v.getContext(), Class.forName( className ) );
startActivity( openNewIntent );
} catch (ClassNotFoundException e) {
Log.d("ERRORPEW", e.getMessage());
e.printStackTrace();
}
Don't make position as a final static parameter in getView(), its a wronge way. Use below getView method to startActivity from list Item,
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setTag(position);
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag;
Intent i = new Intent(context, YourActivity.class);
context.startActivity(i);
}
});
return rowView;
}
Related
Hi guys I need your help. I want to set listner imageview and button in gridview and when I click to imageview app should show dialog with image and when I click to button app should do something.
like this:
enter image description here
You need to create Custom Adapter for your GridView with Custom UI with ImageView and Button, and then inside the getVieew(){} you need to make click listener for Image and button, and there you can do your action with the help of position with your array list.
here I am posting demo adapter for your reference:
here you can change TextView to Button.
public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
ref:-https://caveofprogramming.com/guest-posts/custom-gridview-with-imageview-and-textview-in-android.html
Here is my Java:
public class Cantos extends AppCompatActivity {
ListView lv;
Context context;
ArrayList cantoList;
public static String[] cantos = {"1: Abre Tu Oido", "2: A Cristo Quiero", "3: Acerquese Mi Clamor", "4: A Cristo Yo Alabare",
"5: Acude Dios", "6: Adelante", "7: A Dios Canto", "8: Adios Para Siempre", "9: Ahora Senor", "10: A Jesucristo Ven",
"11: Alabad A Dios"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cantos);
initTypeface();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
toolbar.setNavigationIcon(getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Cantos.this, MainMenu.class);
startActivity(i);
}
});
}
private void initTypeface() {
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/AftaSerifThin-Regular.otf");
TextView text = (TextView) findViewById(R.id.toolbarCantos);
text.setTypeface(myTypeface);
}
}
Here is my other Java with BaseAdapter (Custom ListView):
public class CustomAdapter extends BaseAdapter {
String[] result;
Context context;
private static LayoutInflater inflater = null;
public CustomAdapter(Cantos cantos, String[] cantos1) {
result = cantos1;
context = cantos;
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return result.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView tv;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.cantos_list, null);
holder.tv = (TextView) rowView.findViewById(R.id.textCanto);
holder.tv.setText(result[position]);
Typeface myFont = Typeface.createFromAsset(context.getAssets(), "fonts/AftaSerifThin-Regular.otf");
holder.tv.setTypeface(myFont);
return rowView;
}
}
How can I make one certain item send me to another certain activity?
Thank you in advance!
P.S sorry if I spam too much! I am nearly done learning some of the basics! I beg you cope with me!
If you want to call another activity on selection of item in list view you can call OnClick listener to textview in custom adapter
Code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.cantos_list, null);
holder.tv = (TextView) rowView.findViewById(R.id.textCanto);
holder.tv.setText(result[position]);
holder.tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(context,NewAcivity.class);
((Activity) context).startActivity(intent);
}
});
Typeface myFont = Typeface.createFromAsset(context.getAssets(), "fonts/AftaSerifThin-Regular.otf");
holder.tv.setTypeface(myFont);
return rowView;
}
Use intent.putExtra("key", value) & getExtra() to send the data from one activity to another activity
//try this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent;
intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent)
}
});
First set adapter for your listview. then perform onItem click event.
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
displayAnotherListView(position);
}
});
public void displayAnotherListView(int position) {
switch (position) {
case 0: {
Intent i=new Intent(YourActivity.this,Activity_one.class);
startActivity(ii);
break;
}
case 1: {
Intent one=new Intent(YourActivity.this,Activity_two.class);
startActivity(one);
break;
}
case 2: {
Intent two = new Intent(YourActivity.this, Activity_three.class);
startActivity(two);
break;
}
}
}
you can do this for each item, but if you want to pass same intent for all no need to make method finding position, just pass intent on item click.
I have an listview in my application and when I add search to my listview it dosen't work at all.
when I add those code to my project, getfilter doesn't resolve.
enter code here
public class MyActivity extends Activity {
InputStream in;
BufferedReader reader;
String line = "1";
public ListView listView;
VehicleAdapter adapter;
ArrayList<String> dataItems = new ArrayList<String>();
public int star = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
G.currentActivity = this;
setContentView(R.layout.main);
// Adad.setTestMode(true);
ReadText();
// String[] dataArray = dataItems.toArray(new String[0]);
//String[] dataArray = getResources().getStringArray(R.array.listdata);
//List<String> dataTemp = Arrays.asList(dataArray);
// dataItems.addAll(dataTemp);
listView = (ListView) findViewById(R.id.mainList);
EditText ed = (EditText) findViewById(R.id.editText1);
ArrayList<Bitmap> arr_bitmaps = new ArrayList<Bitmap>(4);
adapter = new VehicleAdapter(MyActivity.this, arr_bitmaps, dataItems);
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
ed.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// vaghti kar bar harfi vared kard josteju mikone :
MyActivity.this.adapter.getFilter().filter(arg0);
}
});
}
public class VehicleAdapter extends BaseAdapter {
ArrayList<String> arr_calllog_name = new ArrayList<String>();
public Activity context;
ArrayList<Bitmap> imageId;
public LayoutInflater inflater;
public VehicleAdapter(Activity context, ArrayList<Bitmap> arr_bitmaps, ArrayList<String> arr_calllog_name) {
super();
this.imageId = arr_bitmaps;
this.context = context;
this.arr_calllog_name = arr_calllog_name;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return arr_calllog_name.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public class ViewHolder {
TextView txtName;
ImageButton btn;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.child_listview, null);
holder.txtName = (TextView) convertView.findViewById(R.id.childTextView);
holder.btn = (ImageButton) convertView.findViewById(R.id.childButton);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtName.setText(PersianReshape.reshape(arr_calllog_name.get(position)));
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/B Morvarid_YasDL.com.ttf");
//holder.txtName.setTypeface(G.defaultFont);
holder.txtName.setTypeface(custom_font);
holder.btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = arr_calllog_name.get(position);
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
});
return convertView;
}
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.contact) {
Intent intent = new Intent(MyActivity.this, ContactUs.class);
startActivity(intent);
}
return super.onOptionsItemSelected(menuItem);
}
private void ReadText() {
try {
in = this.getAssets().open("text.txt");
reader = new BufferedReader(new InputStreamReader(in));
while (line != null) {
line = reader.readLine();
if (line != null)
dataItems.add(line);
else
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
my broblem is this code.
enter code here
MyActivity.this.adapter.getFilter().filter(arg0);
why getFilter() doesn't resolve?
Is it releted to my adaptor?
please help
why getFilter() doesn't resolve?
Because Filterable interface is not implemented in VehicleAdapter class.
To call adapter.getFilter().filter for Activity usng Adapter object need to implement Filterable interface in Adapter class and override getFilter.
See following example how to make Adapter Filterable:
Android Filter Custom ListView (BaseAdapter)
In custom adapter it would not work. this code for search will work in SimpleAdapter. For your adapter you have to write custom code for search.
Follow this link it would do as per your need (search filter in custom adapetr)
Custom Listview Adapter with filter Android
I have a listview and I am populating that list view from a ArrayList of custom objects.
This is the custom adapter code.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int a=position;
FileHolder holder=null;
View row = convertView;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);
holder=new FileHolder();
holder.file=files.get(position);
holder.deleteButton=(ImageButton) row.findViewById(R.id.deleteButton);
holder.downloadButton=(ImageButton) row.findViewById(R.id.downloadButton);
holder.deleteButton.setTag(holder.file);
holder.downloadButton.setTag(holder.file);
holder.fileName= (TextView) row.findViewById(R.id.fileName);
holder.fileName.setText(holder.file.getFileName());
}
else
{
holder=(FileHolder) row.getTag();
}
fPOJO=holder.file;
final AsyncCall call=new AsyncCall();
holder.downloadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
MODE=0;
//call.execute(MODE);
}
});
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "Delete File "+(String.valueOf(a))+" "+fPOJO.getFileName(), Toast.LENGTH_LONG).show();
MODE=1;
//call.execute(MODE);
}
});
return row;
}
public static class FileHolder{
FilesPOJO file;
TextView fileName;
ImageButton downloadButton;
ImageButton deleteButton;
}
There are two buttons one for file delete and other for file download. I have implemented on click listeners for these two buttons. The problem is when I click any button the fileName in the Toast message is different from the file name that I see on the screen display. For ex: I have 5 files with names
delete.png
upload.png
share.png
referesh.png
copy.png
and now when I click delete button corresponding to referesh.png file the toast pop ups and in that the file name is different than referesh.png.
how to resolve this issue.
EDITS:
This is how I set up this adapter
FilesAdapter adapter=new FilesAdapter(rootView.getContext(), R.layout.file_list_view_item_row, Application.files);
View header = (View) View.inflate(rootView.getContext(), R.layout.files_list_view_header_row, null);
fileListHeading.inflate(rootView.getContext(), R.layout.files_list_view_header_row, null);
filesListView.addHeaderView(header);
filesListView.setAdapter(adapter);
I think the problem is with your fPOJO variable when onClick is called it may have different value because after every getView you're changing it's value.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int a=position;
final FileHolder holder;
View row = convertView;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);
holder=new FileHolder();
holder.file=files.get(position);
holder.deleteButton=(ImageButton) row.findViewById(R.id.deleteButton);
holder.downloadButton=(ImageButton) row.findViewById(R.id.downloadButton);
holder.deleteButton.setTag(holder.file);
holder.downloadButton.setTag(holder.file);
holder.fileName= (TextView) row.findViewById(R.id.fileName);
holder.fileName.setText(holder.file.getFileName());
}
else
{
holder=(FileHolder) row.getTag();
}
final AsyncCall call=new AsyncCall();
holder.downloadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
MODE=0;
//call.execute(MODE);
}
});
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "Delete File "+(String.valueOf(a))+" "+holder.fileName, Toast.LENGTH_LONG).show();
MODE=1;
//call.execute(MODE);
}
});
return row;
}
Just make holder final and get you're file name from it.
You store your list item in holder, this is your problem, ListView adapter creates as much convertViews as needed to fill the screen so if you have list of 10+ elements, only few will be added to row.
First of all don't store your list element in holder, you gave adapter method called getItem(positoin) to get it.
In your holder view store only references to layout views (TextView, Buttons etc) and (if(row == null) call findViewById on them and setTag.
Then set text, listeners outside the if statement, like so:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int a=position;
FileHolder holder=null;
View row = convertView;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);
holder=new FileHolder();
holder.deleteButton=(ImageButton) row.findViewById(R.id.deleteButton);
holder.downloadButton=(ImageButton) row.findViewById(R.id.downloadButton);
holder.fileName= (TextView) row.findViewById(R.id.fileName);
row.setTag(holder);
}
else
{
holder=(FileHolder) row.getTag();
}
fPOJO= getItem(position);
final AsyncCall call=new AsyncCall();
holder.downloadButton.setOnClickListener(null);
holder.downloadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
MODE=0;
//call.execute(MODE);
}
});
holder.deleteButton.setOnClickListener(null);
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "Delete File "+(String.valueOf(a))+" "+fPOJO.getFileName(), Toast.LENGTH_LONG).show();
MODE=1;
//call.execute(MODE);
}
});
return row;
}
public static class FileHolder{
TextView fileName;
ImageButton downloadButton;
ImageButton deleteButton;
}
Also you can do some code optimalization, for example don't initialize LayoutInflater in getView() method, you can do it in adapter Constructor
When I had problems trying to fix the position of my listView items to the desired intent when filtered, and got info I could override the problem using a custom adapter, I have done that but I do not know how to assign the clicks to each items, please check below code:
public class IndexPageActivity extends Activity {
ListView listView;
EditText editTextB;
#Override
protected void onCreate(Bundle savfedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.indexpage);
listView = (ListView) findViewById(R.id.pageList);
editTextB = (EditText) findViewById(R.id.searchB);
listView.setAdapter(new PagesAdapter(this));
listView.setOnItemClickListener((OnItemClickListener) this);
}
}
class SingleRow {
String pagedata;
SingleRow(String pagedata){
this.pagedata=pagedata;
}
}
class PagesAdapter extends BaseAdapter implements OnItemClickListener{
ArrayList<SingleRow> pagelist;
Context context;
PagesAdapter(Context c){
context=c;
pagelist = new ArrayList<SingleRow>();
Resources res = c.getResources();
String [] pagedatas = res.getStringArray(R.array.pages_data);
for (int i=0;i<463;i++){
pagelist.add(new SingleRow(pagedatas[i]));
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return pagelist.size();
}
#Override
public Object getItem(int i) {
// TODO Auto-generated method stub
return pagelist.get(i);
}
#Override
public long getItemId(int i) {
// TODO Auto-generated method stub
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewG) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.single_row,viewG,false);
TextView pagetitle = (TextView) row.findViewById(R.id.textViewRow);
SingleRow temp=pagelist.get(i);
pagetitle.setText(temp.pagedata);
return row;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int i, long arg3) {
// TODO Auto-generated method stub
}
}
I will appreciate any help given. Thank Yhu!
EDIT
Will this work?
if (index == 0) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page1.html");
startActivity(i);
} else if (index == 1) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page2.html");
startActivity(i);
Edited ALL
I just got what you need, I added a filter to your baseAdapter and then on text change within the editText You filter the listView and then you go to the activity needed.
here is the full code but you need to bare in mind that I have changed the following:
I changed the pageList to ArrayList instead of
There is a bug in filtering that when I erase what I wrote in the EditText it doesnt update the ListView, you need to figure out why.
I changed the returned value of the function getItem(int i) from Object to String
Within the onItemClick you have to search for the name instead of the position.
here is the code:
public class IndexPageActivity extends Activity implements OnItemClickListener{
ListView listView;
EditText editTextB;
PagesAdapter adapter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.pageList);
editTextB = (EditText) findViewById(R.id.searchB);
adapter1 = new PagesAdapter(this);
listView.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
listView.setOnItemClickListener(this);
editTextB.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
IndexPageActivity.this.adapter1.getFilter().filter(cs.toString());
adapter1.notifyDataSetChanged();
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
// TODO Auto-generated method stub
Intent i;
String name = adapter1.getItem(position);
Log.d("id", name);
if (name.equals("Item1"))
{
i = new Intent(this, anActivity.class);
startActivity(i);
}
else if (name.equals("Item2"))
{
i = new Intent(this, anActivity2.class);
startActivity(i);
}
}
}
class SingleRow {
String pagedata;
SingleRow(String pagedata){
this.pagedata=pagedata;
}
}
class PagesAdapter extends BaseAdapter implements Filterable{
ArrayList<String> pagelist;
List<String> arrayList;
Context context;
String [] pagedatas;
PagesAdapter(Context c){
context=c;
pagelist = new ArrayList<String>();
Resources res = c.getResources();
pagedatas = res.getStringArray(R.array.pages_data);
for (int i=0;i<463;i++){
pagelist.add(pagedatas[i]);
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return pagelist.size();
}
#Override
public String getItem(int i) {
// TODO Auto-generated method stub
return pagelist.get(i);
}
#Override
public long getItemId(int i) {
// TODO Auto-generated method stub
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewG) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.single_row,viewG,false);
TextView pagetitle = (TextView) row.findViewById(R.id.textViewRow);
String temp=pagelist.get(i);
pagetitle.setText(temp);
return row;
}
public class filter_here extends Filter{
#Override
protected FilterResults performFiltering(CharSequence constraint) {
// TODO Auto-generated method stub
FilterResults Result = new FilterResults();
// if constraint is empty return the original names
if(constraint.length() == 0 ){
Result.values = pagelist;
Result.count = pagelist.size();
return Result;
}
ArrayList<String> Filtered_Names = new ArrayList<String>();
String filterString = constraint.toString().toLowerCase();
String filterableString;
for(int i = 0; i<pagelist.size(); i++){
filterableString = pagelist.get(i);
if(filterableString.toLowerCase().contains(filterString)){
Filtered_Names.add(filterableString);
}
}
Result.values = Filtered_Names;
Result.count = Filtered_Names.size();
return Result;
}
#Override
protected void publishResults(CharSequence constraint,FilterResults results) {
// TODO Auto-generated method stub
pagelist = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
#Override
public Filter getFilter() {
// TODO Auto-generated method stub
return new filter_here();
}
}
Two different ways for it:
1) If you want to use something like this at onCreate of your activity;
listView.setOnItemClickListener((OnItemClickListener) this);
You should implement OnItemClickListener to your activity:
IndexPageActivity extends Activity implements OnItemClickListener
and implement its onItemClick method at your activity. (also remove OnItemClickListener interface from your custom adapter)
2) You can simply use below without implementing OnItemClickListener to any class:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO: handle row clicks here
}
I suggest 2nd option. That is easier.
Edit: This not relevant to your problem but you should reuse your views/rows at listView. Change your getView method to:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.single_row, parent, false);
} else {
view = convertView;
}
TextView pagetitle = (TextView) view.findViewById(R.id.textViewRow);
SingleRow temp=pagelist.get(i);
pagetitle.setText(temp.pagedata);
return view;
}
Set your setOnItemClickListenerlike this:
#Override
protected void onCreate(Bundle savfedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.indexpage);
listView = (ListView) findViewById(R.id.pageList);
editTextB = (EditText) findViewById(R.id.searchB);
listView.setAdapter(new PagesAdapter(this));
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Your code here
//int position is the index of the list item you clicked
//use it to manipulate the item for each click
}
});
}