I am new to android development and will greatly appreciate any help i can get.
I have a listview with icons and am having difficulty opening different activities from the items of the list.
the code is as follows:
Thanks in advance
I have a listview with icons and am having difficulty opening different activities from the items of the list.
the code is as follows:
Code:
package mCRM.android.hp;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
//import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class first extends ListActivity {
public class MyCustomAdapter extends ArrayAdapter<String> {
public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
LayoutInflater inflater=getLayoutInflater();
View first=inflater.inflate(R.layout.first, parent, false);
TextView TextView01=(TextView)first.findViewById(R.id.listView1);
TextView01.setText(DayOfWeek[position]);
TextView tv1 = (TextView)first.findViewById(R.id.listView1);
tv1.setText(choose[position]);
ImageView icon=(ImageView)first.findViewById(R.id.icon);
if (DayOfWeek[position]=="Opportunities"){
icon.setImageResource(R.drawable.opportunities);
}
else if(DayOfWeek[position]=="Accounts"){
icon.setImageResource(R.drawable.accts);
}
else if(DayOfWeek[position]=="Contacts"){
icon.setImageResource(R.drawable.contacts);
}
else if(DayOfWeek[position]=="Analytics"){
icon.setImageResource(R.drawable.analytics);
}
else{
icon.setImageResource(R.drawable.bookmar);
}
return first;
}
}
String[] DayOfWeek = {"Opportunities", "Accounts", "Contacts",
"Analytics", "Bookmarks"}; // this is the array for the icons of the list
String[] choose = {"opportunities", "accounts", "contacts",
"analytics", "bookmarks"}; //this is the array for the the different activities.
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,DayOfWeek));
setListAdapter(new MyCustomAdapter (first.this, R.layout.first, DayOfWeek));
}
@Override
protected void onListItemClick(ListView lv, View view, int position, long id) {
// TODO Auto-generated method stub
//ArrayAdapter = (MyCustomAdapter)
super.onListItemClick(lv, view, position, id);
//setContentView(R.layout.main);
//String selection = lv.getItemAtPosition(position).toString();
String openClass = choose[position];
try{
Class selected = Class.forName("mCRM.android.hp."+ openClass);
Intent selectedIntent = new Intent(this,selected);
startActivity(selectedIntent);
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
Toast.makeText(this, openClass, Toast.LENGTH_LONG).show();
}
}
Thanks in advance