I am working on android media player. In main.java it includes main.xml and list.java includes
list.xml , I used intent to call list.java (when I press imagebutton) into main.java , But when I press imagebutton list.xml comes up new window I want to show in bottom of the main.xml
In main.java image button calls list.java into main.java
songslist_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(main.this, list.class);
Toast.makeText(main.this, "Song List", Toast.LENGTH_SHORT).show();
startActivityForResult(i, 100);
//Intent i = new Intent(main.this, list.class);
//startActivity(i);
}
});
/////////////////////////
public class list extends ListActivity
{
// Songs list
public ArrayList<HashMap<String, String>> songsLists = new ArrayList<HashMap<String, String>>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.list);//list.xml
ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
SongList plm = new SongList();
// get all songs from sdcard
this.songsLists = plm.getPlayList();
// looping through playlist
for (int i = 0; i < songsLists.size(); i++)
{
// creating new HashMap
HashMap<String, String> song = songsLists.get(i);
// adding HashList to ArrayList
songsListData.add(song);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, songsListData,
R.layout.song_item, new String[] { "songTitle" }, new int[] {R.id.song_title });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
//lv = (ListView) findViewById (R.layout.list);
// listening to single listitem click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
// getting listitem index
int songIndex = position;
// Starting new intent
Intent in = new Intent(getApplicationContext(),main.class);
// Sending songIndex to PlayerActivity
in.putExtra("songIndex", songIndex);
setResult(100, in);
// Closing PlayListView
finish();
}
});
}
}
Is Your main.java class an activity? Your list ist even an activity, so You started a new one, that will be shown in a new window. I think You need another aproach to do this. Read this tutorials first:
http://www.vogella.com/articles/AndroidListView/article.html
http://windrealm.org/tutorials/android/android-listview.php
A possibillity to do such thing is to integrate a simple listView into your main.xml layout and hide this view until button will be pressed.
EDIT
for example:
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mainListView">
</ListView>
put this into Your main.xml, maybe below Your button. Then You can reference the listview in your main.java:
mainListView = (ListView) findViewById( R.id.mainListView );
But there is a lot more to do, so I recommend You to read the tutorials to get a clear conception about how to build a listView.
Related
How my onClickListener should look like if I need to open layout with different text for each ListView item and I have a lot of ListView items (about 50)? Do I need to create new activity or layout file for each new item? Is it possible to use one activity for all items?
This is my MainActivity.java:
public class MainActivity extends Activity {
// ListView
private ListView listView;
// Adapter
ArrayAdapter<String> adapter;
String items [];
// ArrayList
ArrayList<HashMap<String, String>> productList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//ListView data in res/values/arrays.xml
items =getResources().getStringArray(R.array.items);
listView = (ListView) findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.listItem, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
/*
I know that I can put here I can put something like this:
case 0 :Intent appInfo = new Intent(About.this, Activity1.class);
startActivity(appInfo);
break;
case 1 :Intent appInfo = new Intent(About.this, Activity2.class);
startActivity(appInfo);
break;
case 2 :Intent appInfo = new Intent(About.this, Activity3.class);
startActivity(appInfo);
break;
BUT DO I NEED REPEAT THIS MORE THAN 20 TIMES?!
*/
}
});
}
}
Some system apps like Settings has a lot of ListView and layouts and I don't believe that it has new activity for each layout.
Create a new single activity and pass the text to that activity.
Something like:
Intent intent = new Intent(About.this, <NEW_ACTIVITY>.class);
intent.putExtra("text_key", items[position]);
startActivity(intent);
on the other activity, retrieve the text like this (can be on the onCreate method):
String text = getIntent().getExtras().getString("text_key");
I started this program by reading in a premade database and has it display on a list, i am currently trying to convert the list to a clickable list that opens up to a new fragment. This is the code i am currently running. The commented out section is the current problem.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 =(Button) findViewById(android.R.id.button1);
db = new MyDatabase(this);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
schedule = db.getSchedule();
ListView listView = (ListView) findViewById(R.id.list);
//listView.setOnItemClickListener(this);
listView.setAdapter(new SimpleCursorAdapter(
MainActivity.this,
R.layout.row,
schedule,
new String[] {"fName", "Calories", "Protein", "Carbs" },
new int[] { R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4 },
0));
schedule = db.getSchedule();
ListView listView2 = (ListView) findViewById(R.id.list2);
listView2.setAdapter(new SimpleCursorAdapter(
MainActivity.this,
R.layout.row,
schedule,
new String[] {"fName", "Calories", "Protein", "Carbs" },
new int[] { R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4 },
0));
}
});
}
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Log.i("HelloListView", "You clicked Item: " + id + " at position:" + position);
// Then you start a new Activity via Intent
Intent intent = new Intent();
intent.setClass(this, Activity2.class);
intent.putExtra("position", position);
// Or / And
intent.putExtra("id", id);
startActivity(intent);
}
listView.setOnItemClickListener(this); will not work because it is being applied to android.view.View.OnClickListener. I have been trying to modify b1.setOnClickListener(new View.OnClickListener(), to try to change its parameters, but I'm not getting any luck. Is there an easy solution I'm not seeing or do i need to reconstruct this code to revolve around a clickable list. Thanks!
Simple, put below code lines in onCreate() of Activity instead of button's onClick() And then implement AdapterView.OnItemClickListener to your Activity.
ListView listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(this);
Now on line listView.setOnItemClickListener(this); this refers to your activity class and implemented interface and onItemClick will work.
If your activity is a ListActivity then no need of listView.setOnItemClickListener(this); this line.
Change the Activity to ListActivity and override the method onListItemClick of ListActivity class. Put all the code in the onItemClick of listener to
onListItemClick.
I have a ListView filled with items. I'd like to set an onClickListener to show details about the item that was clicked. How can I determine which item was clicked within the onClickListener? The View, or v has a lot of methods available that I've looked through and don't see anything pertaining to getting the object clicked on.
//populate the activity list
ListView teamsListView = (ListView) activity.findViewById(R.id.teamsListView);
ArrayList<HashMap<String, String>> listData = new ArrayList<HashMap<String, String>>();
HashMap<String, String> listItem;
for (TeamSet teamSet : response.getTeamSetList()) {
listItem = new HashMap<String, String>();
listItem.put("name", teamSet.getName());
//listItem.put("teamCount", Integer.toString(teamSet.getTeams().size()));
listData.add(listItem);
}
teamsListView.setAdapter(
new SimpleAdapter(
context,
listData,
R.layout.teams_list_row,
new String[]{"name"},
new int[]{R.id.teamsTeamListName}
)
);
//show details on a team
TextView team = (TextView) activity.findViewById(R.id.teamsTeamListName);
team.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//how do I know which item was clicked? I want to load more details on the item...
}
});
You use a OnItemClickListener not a onCLickListener
You do something like this:
final ListView lv = (ListView) findViewById(R.id.ListView01);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
String selectedFromList =(String) (lv.getItemAtPosition(myItemInt));
}
});
Hope this helped.
I'm able to edit or delete single item without any problem but when I click delete button after edit an item, the item is still showing on the ListView even though it has been deleted from the database.
ListViewActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_requests);
requestsList = new ArrayList<HashMap<String, String>>();
new LoadAllRequests().execute();
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String request_id = ((TextView) view.findViewById(R.id.request_id)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
ViewRequestActivity.class);
// sending rid to next activity
in.putExtra(TAG_ID, request_id);
// starting new activity and expecting some response back
startActivityForResult(in, 55);
}
});
}
Instead of closing and reopening the activity, you should use invalidateViews
yourListView.invalidateViews();
You have to sign the adapter to the listview again
You can also try refreshing the activity. I believe that it is ListViewActivity.restart();
Help!!
I have a listadapter with a detail view, the problem that I am having is trying to pass values from an xml file to the second view. I can currently do this but only if I show those values in the first view. What I am trying to acheive is on the firstview have just a title and when you click on that title it takes you to a detail view with more information about it.
here is the code that I currently have.
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_USERADDRESS, KEY_DATEDUE}, new int[] {
R.id.name, R.id.cost });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
//#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
// String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView)view.findViewById(R.id.cost)).getText().toString();
// String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
//in.putExtra(KEY_USERADDRESS, name);
in.putExtra(KEY_DATEDUE, cost);
//in.putExtra(KEY_DESC, description);
startActivity(in);
}
});
}
As you can see I have a list adapter,I only want to show the contents of the KEY_USERADDRESS on the first view and when they click it then the rest will be shown. If I add the rest of the Textviews to the listadapter it makes the textviews to big.
Sorry if I am a little confusing, I'm still a rookie when it comes to android development.
Any help will be appreciated!!
Assuming your data is stored in the menuItems Map object, how about something like this in your onItemClick():
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Your code here..
Map<String, String> myValueMap = menuItems.get(position);
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_USERADDRESS, myValueMap.get(KEY_USERADDRESS));
in.putExtra(KEY_DATEDUE, myValueMap.get(KEY_DATEDUE));
in.putExtra(KEY_DESC, myValueMap.get(KEY_DESC));
startActivity(in);
}
the position parameter in the onItemClick can be used to get the selected Map from your menuItems which you used the create your SimpleAdapter to populate the ListView