问题
I've just started out coding for Android but am struggling to get my head around listviews.
For example I need to understand how I can show another listview when clicking on an item in the first listview. Also how I can display a textview when clicking on an item in a listview.
If anyone knows of any good tutorials or examples please let me know.
Thanks a lot
回答1:
Here are some links for you are looking for. Advanced but if you stick with it and dont give up on this, you will learn a ton!!! Take a look:
Android: ListView elements with multiple clickable buttons
Android custom list item with nested widgets
回答2:
For example I need to understand how I can show another listview when clicking on an item in >the first listview. Also how I can display a textview when clicking on an item in a listview.
In android and java too, you can use listener to listen event. In your case you want know when a user click on an item, so you'll need a onclicklistener which will call an another activity with another listview. For the textview when clicking I think you want speak of Toast, it's a kind of notification at the bottom of screen.
a link with good examples : http://developer.android.com/guide/topics/ui/ui-events.html
回答3:
Thanks for all the links everyone, Extremely useful!
I have solved what I needed to do, it might be extremely crude and inefficient but it works until I learn more at least.
This allows me to link together several different view types.
package com.android.AndroidViews;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
public class AndroidViews extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(),
R.array.list_titles, R.layout.list_item));
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position )
{
case 0: Intent newActivity = new Intent(AndroidViews.this,LinearView.class);
startActivity(newActivity);
break;
case 1: Intent newActivity1 = new Intent(AndroidViews.this,List2.class);
startActivity(newActivity1);
break;
}
}
});
}
}
回答4:
I don't know of any good tutorial but for the first question you ask :
"I need to understand how I can show another listview when clicking on an item in the first listview."
I assume you want to keep the same list view in the same activity. So just change the adapter of the list view. If it's cursor based, don't forget to manage your cursor. Once you switched to the new adapter, call the
notifyDatasetChanged()
method of you adapter to refresh the view.
Regarding the second question "Also how I can display a textview when clicking on an item in a listview.", it's too fuzzy for me. What do you wanna do ? Edit a list item directly in place, popup a dialog with an edit text ?
Regards, Stéphane
来源:https://stackoverflow.com/questions/6341580/android-more-complex-listview-tutorial