Adapter for RecentCalls Activity - android

I'm using googles source code to build a recent calls look alike activity. I have to create an app with a custom view of the dialer, recent calls and contacts, so my first step was to create a custom dialer. Then, I created a call log, but the appearance wasn't nice enought so I get google's contacts app package to get the RecentCallsActivity and adapt to my app.
Now, I've got almost the app working, but I have some errors that I don't know how to solve. This is a extract of the code with the most relevant parts to try to solve this errors:
public class RecentCallsListActivity extends ListActivity implements View.OnCreateContextMenuListener {
...
RecentCallsAdapter mAdapter;
....
final class RecentCallsAdapter implements ViewTreeObserver.OnPreDrawListener, View.OnClickListener, Runnable {
...
#Override
protected void onCreate(Bundle state) {
super.onCreate(state);
mAdapter = new RecentCallsAdapter();
getListView().setOnCreateContextMenuListener(this);
setListAdapter(mAdapter); // The method SetListAdapter (ListAdapter) in the type ListActivity is not aplicable for the arguments (RecentCallsListActivity.RecentCallsAdapter)
mQueryHandler = new QueryHandler(this);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfoIn) {
AdapterView.AdapterContextMenuInfo menuInfo;
Cursor cursor = mAdapter.getItem(menuInfo.position); //The method getItem(int) is undefined for the type RecentCallsListActivity.RecentCallsAdapter
...
These are the 2 principal errors. In the original file, the mAdapter is declared the same way and there aren't these errors.
Also, there are other 2 errors:
mAdapter.isGroupHeader(menuInfo.position)
mAdapter.getGroupSize(menuInfo.position)
It must be some kind of declaration but I don't know why or what to do.
UPDATE
I've solved this extending a class to the adapter thanks to Eugene's answer. But now I have the following problem when implementing some drawables. I know i can't reference to the android.internal.R so the thing would be to find a solution for this:
protected void bindGroupView(View view, Context context, Cursor cursor, int groupSize,
boolean expanded) {
final RecentCallsListItemViews views = (RecentCallsListItemViews) view.getTag();
int groupIndicator = expanded
? com.android.internal.R.drawable.expander_ic_maximized //CANNOT BE RESOLVED
: com.android.internal.R.drawable.expander_ic_minimized; //CANNOT BE RESOLVED
views.groupIndicator.setImageResource(groupIndicator);
views.groupSize.setText("(" + groupSize + ")");
bindView(context, view, cursor);
}

Your adapter does not implement listadapter interface
Original adapter extends ResourceCursorAdapter, which already implements ListAdapter
final class RecentCallsAdapter extends ResourceCursorAdapter
implements Runnable, ViewTreeObserver.OnPreDrawListener, View.OnClickListener {
but your adapter does not.
final class RecentCallsAdapter implements ViewTreeObserver.OnPreDrawListener, View.OnClickListener, Runnable
You should either extend some class, either implement ListAdapter yourself
Other two errors come from same origin - you just don't have such methods, because You haven't implemented them, nor you have extended the class already having them.
Good luck with coding :)

Related

How get value from adapter class to activity Class

I am having adapter class, In that, I need to pass invoiceId to an Activity Class. I have seen some example like pass-through interface, but I lost track on following the code procedure.
Here Is My Adapter Class extends BaseAdapter
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
companyName = ct.getSharedPreferences("prefs", 0);
Log.d("test", "" + deliveryListBeans.size());
LayoutInflater inflater = (LayoutInflater) ct.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.list_vew_for_delivery_order, null);
TextView invoice = (TextView) v.findViewById(R.id.invoice);
final TextView delivery = (TextView) v.findViewById(R.id.do_delivery);
final DeliveryListBean dlb = deliveryListBeans.get(position);
invoice.setText(dlb.getInvoiceNo());
}
delivery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ct.startActivity(new Intent(ct, EmployeesListForPopUp.class));
DeliveryOrdersListAdapter deliveryOrdersListAdapter=new DeliveryOrdersListAdapter(EmployeesListForPopUp.this);
}
});
}
Here is My Activity Class
public class EmployeesListForPopUp extends Activity {
private List<EmployeeIdNameBean> employeeIdNameBeans = new ArrayList<EmployeeIdNameBean>();
ListView listView;
SharedPreferences companyName;
EmployeePopUpAdapter employeePopUpAdapter;
private ImageView img1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employees_list_for_pop_up);
I need to get invoiceId from Adapter Class. How?
You need to pass context of the activity in adapters constructor.
Then set activity.invoiceid value in clickevents of adapter.
One simple way is that you write a method in MainActivity
public void setInvoiceId(int invoiceId) {
// do what you want with invoiceId
}
and pass the instance of your activity to adapter
DeliveryOrdersListAdapter adapter = new DeliveryOrdersListAdapter(EmployeesListForPopUp.this);
and get it in your adapter and keep it
EmployeesListForPopUp myActivity;
public MyAdapter(EmployeesListForPopUp activity) {
myActivity = activity;
}
and where you need to pass invoiceId just call the method of main activity
myActivity.setInvoiceId(invoiceId);
General way of implementing it:
In the adapter class, where you set text to invoice TextView, you also can add a tag to it. Put attention - despite every item in the list is build from the same prototype, the tag (as well as text) will be uniq. The best way is to use "position" as value of the tag: invoice.setText(dlb.getInvoiceNo());
invoice.setTag(Integer.valueOf(position).toString());
You need to make your items in the list clickable (this is out of the scope of this question). So, when you click on some item - you can retrieve any data it has, and specifically tag - getTag();.
Then you send Intent to other activity, providing the tag as extra message. So that activity will "know" which item in the array list it is related to (i.e. tag == position, right?). And continue from there.
I implemented simple project that illustrates it. This project is simple demo and illustration of working with ArrayList adapter,
displaying the item in the ListView, clicking on some item and display relevant data in separated activity. Please download it and try (min API 21). Basic description is available in README file.
The project is here on the GitHub:
(corrected path)
https://github.com/everall77/ArrayListSimpleExmpl

Incorrect Type In MainActivity

I'm making a webView browser and I'm trying to make a button that slides down to show more buttons, I've shown the code where the errors occur below, I've also stated on what lines the errors occur on. Thanks in advance!
ExpandableListView expandableList = getExpandableListView(); *<-The method* - *getExpandableListView() is undefined for the type MainActivity IS THE ERROR I GET ON - THAT LINE*
(ExpandableListView) findViewById(R.id.list)
expandableList.setDividerHeight(2);
expandableList.setGroupIndicator(null);
expandableList.setClickable(true);
setGroupParents();
setChildData();
MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems,childItems);
adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), - this);
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(this); *<-The method - setOnChildClickListener(ExpandableListView.OnChildClickListener) in the type - ExpandableListView is not applicable for the arguments (MainActivity) IS THE ERROR I - GET ON THAT LINE*
}
I've never used ExpandableListView but I'm sure its like ListView in that if you aren't extending ExpandableListActivity then you will get that error.
You need to either create your own View in your layout that is an ExpandableListView or you need to extends ExpandableListView in your Activity to use its methods and listeners in this way such as getExpandableListView()
MainActivity has to extend ExpandableListActivity. I suspect you've just used an Activity. You'll have to provide the line that declares your Activity.
expandableList.setOnChildClickListener() takes an argument of type ExpandableListView.OnChildClickListener. In the code you've presented, I don't see that you've instantiated a Listener. If your activity declaration also declares that you implement the ExpandableListView.OnChildClickListener interface, I don't see the override for onChildClic().
In short, you should have
public class MainActivity extends
ExpandableListActivity implements ExpandableListView.OnChildClickListener {
...
#Override
public boolean onChildClick(ExpandableListView parent, View v, int
groupPosition, int childPosition, long id) {
...
}
...
}

onItemClick not being invoked on ListActivity

I have a class that extends from ListActivity and implements OnItemClickListener
It's a very simple test class, the idea is that I select an item on the list, and it shows the selected item on a Toast.
I can see the list normally on the emulator, and I can also see the effects of clicking in the item, but then nothing happens.
I don't think the event is being fired, because I see nothing on LogCat, here's the code:
public class CarsListActivity extends ListActivity implements
OnItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listCars()));
}
private List<String> listCars() {
return Arrays.asList("Ferrari", "Lamborghini", "Porsche");
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView) view;
String message = "Selected car: " + textView.getText();
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
The Activitiy is defined like this on the AndroidManifest.xml file:
<activity android:name=".CarsListActivity" />
Is there anything I'm missing?
I researched this error and I found many solutions saying that this concerns clickability and focusability attributes on the layout. But I'm using Android's own android.R.layout.simple_list_item_1 so I don't really know how I could fix it.
Am I missing some configuration?
You need to register the OnItemClickListener (the activity) like this :
getListView().setOnItemClickListener(this)
Simply implementing the OnItemClickListener interface is not sufficient
add this in onCreate() below setListAdapter()
getListView().setOnItemClickListener(this);
use this on oncreate()
getListView().setOnItemClickListener(this);

Cannot extending both ListActivity and AsyncTask

Since java does not support multiple inheritance I have a problem getting this to work.
This is what I want to do :
public class CallForwardActivity extends ListActivity /* This is there I want to extend AsyncTask */ implements OnSharedPreferenceChangeListener
{
... // creating and initializing stuff
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
userInfo = this.getSharedPreferences(USERINFO_FILE, 0);
userInfo.registerOnSharedPreferenceChangeListener(this);
userControl = new UserController(context);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
//setListAdapter(new ArrayAdapter<String>(this, R.layout.callforward_items, R.id.callforward_item_text, callforwardLabels));
list = new ArrayList<HashMap<String,String>>();
callForwardList = new SimpleAdapter(
this,
list,
R.layout.callforward_items,
new String[] { "line1","line2" },
new int[] { R.id.callforward_item_text, R.id.callforward_number } );
populateList();
setListAdapter( callForwardList );
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
CustomDialog(); // this will make the asynchronous task call if the user presses "OK" within the dialog box.
}
});
}
How do I go about making the call asynchronous without extending AsyncTask ?
Just create another class that extends AsyncTask and use it in your ListActivity. You can make it inner if you like. Hope this helps.
errrmmm.... there's no way you can extend two classes in java you can implement many interfaces but extend one class. you should create another say inner class which extends AsyncTask<..,..,..> and do your background jobs there.
More on multiple inheritance in java
http://java.sys-con.com/node/37748
What is use case you are trying to use activity extending a asynctask, asynctask is used for background process, Acitivity component is used to provide UI
You are trying UIThread extends BackgroundThread, which means you are making a relationship like 'UIThread is-a BackgroundThread'.
I feel there is another way to solve your use case.

OnItemClickListener syntax - please could someone explain what is happening and why it works?

I've implemented a gridview and I've been hacking it together from examples to get a feel for how it works. I created and Adapter and when I came to implement the listener I discovered it is achieved like this.
private OnItemClickListener mColourClickListener = new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
Log.d("LISTENER","Position Clicked ["+position+"]");
}
};
Why is this different to a listview and why does it have it's methods implemented in braces after the variable declaration?
Many thanks,
M
You are creating new instance of anonymous class that implements OnItemClickListener interface. It is easier than defining new class and then creating new instance of this class. Anonymous class allows you to define class inline where you need it. Listeners are usually for one time use, so they are often defined as anonymous classes.

Categories

Resources