I have two ListViews in my activity that uses same OnItemClickListener. Is there any way to identify which ListViews element I am pressing now? I have used this code:
#Override
public void onItemClick(AdapterView<?> list, View view, int position,
long id) {
if (view.getId() == R.id.listDictionary) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, WordActivity.class);
DictionaryListElement ele = (DictionaryListElement) dictionaryList
.getAdapter().getItem(position);
intent.putExtra("word", ele.getWord());
startActivity(intent);
} else if (view.getId() == R.id.listFavourites) {
Intent intent = new Intent(MainActivity.this, WordActivity.class);
String ele = (String)favouritesList.getAdapter().getItem(position);
intent.putExtra("word", ele);
startActivity(intent);
}
}
But it is not working. I think it is getting id of each pressed element not ListViews
You should use the ID of ListView (here ListView is passed as AdapterView to onItemClick()), not the ID of View as this View is a ListView item.
if(list.getId() == R.id.listDictionary) {
// item in dictionary list is clicked
} else if (list.getId() == R.id.listFavourites) {
// item in favourite list is clicked
}
Why would you need the same listener if you distinguish logic with ifs? Create separate listeners for each view. It would be cleaner code and should work as well.
// dictionary listener
#Override
public void onItemClick(AdapterView<?> list, View view, int position,
long id) {
Intent intent = new Intent(MainActivity.this, WordActivity.class);
DictionaryListElement ele = (DictionaryListElement) dictionaryList
.getAdapter().getItem(position);
intent.putExtra("word", ele.getWord());
startActivity(intent);
}
// favorites listener
#Override
public void onItemClick(AdapterView<?> list, View view, int position,
long id) {
Intent intent = new Intent(MainActivity.this, WordActivity.class);
String ele = (String)favouritesList.getAdapter().getItem(position);
intent.putExtra("word", ele);
startActivity(intent);
}
switch(list.getId()){
case R.id.listDictionary:
//listDictionary related action here
break;
case R.id.listFavourites:
// listFavourites related action here
break;
default:
break;
}
Related
I have created a list view something like this: http://imgur.com/a/bhWhR in activity1
Onitemselect i want to launch another acitivity which shows description about listitem1 like below
http://imgur.com/a/agGma
but did not find any helpful material to create and activity with same layout to get different content like this one ::
I did explore for quite some time and I could not find any solution that helped me .
Use OnItemClickListener and use intent to open other activities on item clicks
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (i == 1) {
Intent intent = new Intent(MainActivity.this, Lu1Activity.class);
startActivity(intent);
} else if (i == 2) {
Intent intent = new Intent(MainActivity.this, Lu2Activity.class);
startActivity(intent);
} else if (i == 3) {
Intent intent = new Intent(MainActivity.this, Lu3Activity.class);
startActivity(intent);
} else if (i == 4) {
Intent intent = new Intent(MainActivity.this, Lu4Activity.class);
startActivity(intent);
}
}
});
If you want to use same 2nd activity for each intent and show different content then use a global variable to know which item is selected and change content of 2nd activity as you wish.
Add a variable like this
public static int itemNumber;
Use OnItemClickListner
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
itemNumber = i;
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
});
In OtherActivity, Set different content to your views depending on itemNumber's value.
Note: You can also use putExtra to extend data to the intent and use that to determine which item is clicked.
Android newbie here.
The scenario
I am using my website's api to populate data for Android ListView
for the first screen it is called GameFragment. Game fragment loads the games from my website url
I was able to successfully list all games. So my next step would be to show all articles related to the game when user clicks on the game. Using my websites api http://www.gamerzwiki.com/api/news.json?game_id=qwert133 (whatever the id of the item is)
Loading Contents from url
try {
JSONArray games = gp.loadGamesFromApi();
ArrayList<Games> listData = new ArrayList<Games>();
if(games != null){
for(int x = 0 ; x<games.length() ; x++){
JSONObject game = games.getJSONObject(x);
try {
Games game1 = new Games();
game1.title=game.getString("title");
game1.id = game.getInt("id");
listData.add(game1);
//free game 1
game1 = null;
} catch (JSONException e) {
e.printStackTrace();
}
}
}
ArrayAdapter<Games> adapter = new ArrayAdapter<Games>(getActivity(),android.R.layout.simple_list_item_1, listData);
setListAdapter(adapter);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
so I have a listener
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Object item = l.getSelectedItem();
Toast.makeText((Context)getActivity(),item.title.toString() , Toast.LENGTH_LONG).show();
Intent intent = new Intent(v.getContext(), NewsActivity.class);
startActivity(intent);
}
but it seems I can't get the attribute of the item
but I don't know how I can do that on my ListView. So I would like to ask your expert advise
what you can do is implement onClick listener on your listview which has all the games:-
listview.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?>adapter,View v, int position){
Games game = adapter.getItem(position);
Intent intent = new Intent(Activity.this,destinationActivity.class);
intent.putExtra("id",game.id);
startActivity(intent);
}
});
in your other activity retrieve this as:-
String id=getIntent.getStringExtra("id");
and now you can use this id to make webservice call in your activity,here ItemClicked is your model class which your are using to populate your listview with your all games
You can pass data between activities by using putExtra in the intent
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Games item = adapter.getItem(position);
Intent intent = new Intent(v.getContext(), NewsActivity.class);
intent.putExtra("id", item.id);
startActivity(intent);
}
In your receiving activity do this
String id = getIntent().getStringExtra("id");
You can pass basic datatypes through putExtra. If you want to pass custom objects you'll have to implement Parcelable http://developer.android.com/reference/android/os/Parcelable.html
I am developing an android application and use Gridview to display 16 Text View Controls
When user clicks a particular Text View control I need to open another activity
My Gridview item click event as follows
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(this, ProductListActivity.class);
} });
But this makes an error and says “The constructor
Intent(new AdapterView.OnItemClickListener(){}, Class<ProductListActivity>) is undefined”
Could someone please help me to correct this ?
your code should be:
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(activity_name.this, ProductListActivity.class);
} });
You need to pass the context as first argument. In your case YourActivity.this.
send getApplicationContext or yourActivity.this as the first parameter in your intent. Hope this solve your issue.
gridview.setOnItemClickListener(new OnItemClickListener() {
switch (position)
{
case 1:
Intent intent = new Intent(YourActivity.this, ProductListActivity.class);
break;
case 2:
Intent intent = new Intent(YourActivity.this, AnotherActivity.class);
break;
default: // default.....
break;
}});
Handle your activities with switch case....
Once the user chooses a product from my ListView, it then puts the selected text from that ListView into an EditText. The problem I am having is when the user selects a product from the list, and then presses back, it comes up with the list again instead of returning to the EditText activity.
I have tried using "finish();" after the activity starts but nothing seems to be working.
Activity that holds the EditText that launches the List activity:
EditText CPU = (EditText) findViewById(R.id.autoCompleteTextView4);
CPU.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent CPUList = new Intent(getApplicationContext(),
CPUList.class);
startActivityForResult(CPUList, 1);
Intent i = getIntent();
String product = i.getStringExtra("key");
EditText CPU = ((EditText) findViewById(R.id.autoCompleteTextView4));
CPU.setText(product);
}
});
List view class
#Override
public void onCreate(Bundle OnsaveInstanceState) {
super.onCreate(OnsaveInstanceState);
setContentView(R.layout.activity_cpulist);
ListView listViewCPU = (ListView) findViewById(R.id.listViewCPU);
listViewCPU.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
String CPUList[] = {
"CPU's go here", "CPU's go here", "CPU's go here", "CPU's go here" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, CPUList);
listViewCPU.setAdapter(adapter);
listViewCPU.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listview, View myView,
int pos, long mylng) {
String CPU = (String) listview.getAdapter().getItem(pos);
Intent i = new Intent();
i.putExtra("key", CPU);
setResult(1, i);
finish();
}
});
You need to launch your activity in a way that it doesn't get added to back stack.
Here's how you do that: https://stackoverflow.com/a/12358563/375929
If I understand you correctly, you are calling finish() on the wrong Activity. If you want the list Activity to finish then that's where you need to call finish()
#Override
public void onItemClick(AdapterView<?> listview, View myView,
int pos, long mylng) {
String CPU = (String) listview.getAdapter().getItem(pos);
Intent i = new Intent(getApplicationContext(),
ListmenuActivity.class);
i.putExtra("key", CPU);
startActivity(getIntent());
startActivity(i);
finish(); // finish here
}
and remove finish() from your EditText Activity
Another issue I see is it looks like you are starting that second bit of code with the first using startActivityForResult() but you aren't sending back a result in your second code. Instead, you seem to be starting another Activity. It seems that second bit should be more like
#Override
public void onItemClick(AdapterView<?> listview, View myView,
int pos, long mylng) {
String CPU = (String) listview.getAdapter().getItem(pos);
Intent i = new Intent();
i.putExtra("key", CPU);
setResult(1, i);
finish(); // finish here
}
In my app I have a dialog (dialog1), containing Listview with an ArrayAdapter having 3 string items. I want to set onItemclickListener() on this list,through which I would be able to start different activities on different item click. Please Help.
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,R.layout.new_service_request,LIST));
ListView lv=getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) v).getText(),
Toast.LENGTH_SHORT).show();
// int ITEM_CLICKED = (int)getSelectedItemId();
switch(position){
case 0:
Intent intent1 = new Intent(New.this, Next.class);
startActivity(intent1);
break;
case 1:
Intent intent2 = new Intent(New.this, List.class);
startActivity(intent2);
break;
case 2:
Intent intent3= new Intent(New.this, HotCard.class);
startActivity(intent3);
break;
See if this helps
dialog1.setItems(array_of_items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//which is the item number in the list which you can use
//to do things accordingly
}
});
Not sure if this will work but, you could try within the dialog:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//do stuff here
}
});
I would recommend having a go at reproducing the example from http://www.vogella.de/articles/AndroidListView/article.html and then trying to adapt it to suit your purposes (i.e. in your dialog). I usually find it easier to get a grip on the problem in a simple use case before trying to shoe-horn it into my code. So in your ListActivity, you would call
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, yourThreeStringArray));
Then instead of the call to Toast in the example, simply start your activity something like this:
Intent myIntent = new Intent(this, MyIntent.class);
startActivityForResult(myIntent, ACTIVITY_CREATE);
(Replacing MyIntent with the class of your intended action, of course.)
Good luck!