I am trying to implement item click listener on my list in my main class that extends Activity, because for some reason if i extend ListActivity and expand my list with setListAdapter, the app force closes on startup, but if i extend Activity and expand it with setAdapter it runs fine.
Now my problem is i cannot find a way to implement a click listener for my list, I have tried implementing it in the custom ListAdapter.
The list clicks are going to open another activity which has another list.
------Updated------
#Override
public void run() {
// call any new activity here or do any thing you want here
final Intent intent = new Intent();
if(selectedListItem == 0) {
intent.putExtra("value1", "value1");
intent.setClass(this, com.lister.listexample.ListexampleActivity.class);
}
startActivityForResult(intent, selectedListItem);
}
Change ListView background - strange behaviour
check the above link where i mentioned a list example. that will enable you to trick down your list problems.
Try making a sample project first by copy pasting the above example. then you will be able to understand the answer better
Hope it helps :)
Related
I have used a tabHost in my android application. The main ActivityGroup class has a listview. When a listview item is clicked, it displays the details in the same tab. I have used a simple activity class for the detail view. the detail view contains a save button which sves the data and returns to the listview(main) screen.
My problem is that for the first time the save button is pressed it works as expected but if the list item is selected again and in the details view if save button is pressed, the application throws error.
Please help. thanks in advance.
The error could be caused by many different things. Check to see if you have implemented your Intent to switch between activities correctly. Like so...
/** Called when the user clicks the save button */
public void save(View view) {
Intent intent = new Intent(this, Main.class);
startActivity(intent);
}
If this isn't what you were looking for, can you provide some examples of your code?
I'm using a TouchListView by CommonWare and so far it's working fine, But it fails in the following case:
activity A has a TouchListView and button to add an item , now when i click over a button,
activity B opens up and once the user choose the item i am calling finish to end activity B
and it comes back to activity A.
Now here drag n drop in TouchListView doesn't work I mean items are getting dragged but not able to be placed where a user wants, I tried a lot but could not find a way to fix it.
I suspect you're doing some setup in onCreate() that's not getting called when you return to Activity A because you're entering it the 2nd (and subsequent) time(s) via onResume().
http://developer.android.com/reference/android/app/Activity.html
BaseAdapter adapter;
List<myStuff> stuffs;
if (adapter == null) {
adapter = new BaseAdapter()
if (stuffs == null) {
stuffs = new List<myStuff>();
}
}
You're going to need to do something like that and add List to your savedInstanceState bundle in onSaveInstanceState() then dig it out when the activity restarts.
I dont know whether my present approach is correct or not... I am having two classes one that extends the Activity and the other that extends the ListActivity. Now I wanted to sort my list in the ListActivity when I press a button present in the Activity class layout. What are the possible ways.. presently I am trying to send an adapter from the Activity class to ListActivity but it is failing me. So any other ideas. Expecting an answer soon...Thanx in advance.
Two options that I see offhand.
1) Put the sort button in the ListActivity's layout so it's all in the same Activity.
2) When they push the sort button in Activity 1, put the sort method into the Intent that you launch the ListActivity with as an extra. Read that extra in your ListActivity and sort accordingly.
Create a button or inflate a layout and then add it as a header, something like
Button sort = new Button(this);
sort.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//....
}
});
ListView lv;
getListView().addHeaderView(sort)
setListAdapter(...)
Summery of problem:- I have classes named AttachPicture which extends the ListActivity and another class MyArrayAdapter which extends the ArrayAdapter.With the help of these classes i am able to create a ListView in which i have two textviews and a button in each item.ListView is working fine no problem till here.But now i want to start the camera on click of button presents in every item of listview, here's my problem starts give me any idea and explain it with code to get the expected result.
Here's my code which i m putting inside the
onClick(){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
But eclipse is not allowing my app to put startActivityForResult(cameraIntent,CAMERA_PIC_REQUEST); I dont know why it is happening.Help me to get rid of this problem.
Thanx in advance.
write button.onClickListener()
{
//code for camera intent
}
inside getView() overridden method of Adapter;
in case arrayAdapter does not provide getView(view, position , ... ) extend base Adapter .
Editing
startActivtyForResult() is method of activity class , so pass Activity context to Adpter , then call context.startActivtyForResult()
[Update Solution]
Referring to the post in the link
ViewPager PagerAdapter not updating the View
public void onSomeButtonClicked(View view) { // registered to Button's android:onClick in the layout xml file
Log.w(TAG,"Some button clicked !!");
getIntent().setAction(IntentManager.Intents.ACTION_SPAWN_LIST_BY_SOMETHING);
mViewPager.getAdapter().notifyDataSetChanged();
}
// And inside my PagerAdapter
#Override
public int getItemPosition(Object object) {
return 0;
}
Fixed all my problems, i just used Intent.setAction().
[Update to below Post]
My problem is i have a ViewPager PagerAdapter in my Main Activity. On clicking one of the 3 buttons., any specific intent will be fired (i used intent, i could have gone with just State variable as well, just that i pass some Uri with the intent). What happens is., i do
public void onSomeButtonClicked(View view) { // registered to Button's android:onClick in the layout xml file
Log.w(TAG,"Some button clicked !!");
getIntent().setAction(IntentManager.Intents.ACTION_SPAWN_LIST_BY_SOMETHING);
mViewPager.getAdapter().notifyDataSetChanged();
}
This is why i was guessing maybe i should just do startActivity again, with the new Intent Action on the same Activity.
Spawning a new Activity, i would have to redraw every other view in the layout which is basically the same code, for the most part in the current activity.
Any suggestions here? Please help
[Original Post]
I have a PagerAdapter and 3 Buttons in the my Main Activity. This activity is enter from Main Launcher.
When i press any one of the buttons, the Intent Action is changed.
My question:
The changed Intent action reflects some changed view in the ViewPager and does_not spawn a new Activity as such, only the view is updated.
What approach should i take to get this task?
Can i start the currentActivity using startActivity() and different Intent actions on button click?
or is there any other efficient way in android to do this?
[No need code, just explanation of logic / method would suffice]
Thanks in advance
If you are saying that you are trying to use startActivity to bring up the same activity again, and its not working, it could be because you set something like singleTop in your Android manifest.
If you are asking whether or not you should use an intent to change the state of your Activity, then the answer is "it depends". If the user would expect the back button to return your app to its previous state (instead of going back to the home screen), then it might be a good choice for you. If that is the case, however, I would ask why not just make 2 different Activities? Otherwise, just do as Dan S suggested and update the state of your Activity as the user interacts with it.
You can always use the onNewIntent() hook. Do something like this in your activity:
protected void onNewIntent(Intent intent){
//change your activity based on the new intent
}
Make sure to set the activity to singleTop in your manifest. Now whenever startActivity is called the onNewIntent() will be executed. Also, note that per the documentation:
Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.