问题
I am practicing ListView and I have placed a few List Items on an activity. Right now I have 3 items of list (in three rows). I want that once a user click one of the row (let's say it's my first row for now) s/he should be able to see some details text about it. That text will be displayed somewhere at at the bottom of the activity , outside the ListView.How could I do that? Like this:
I am working on this code:
MainActivity.java:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] list = {"About Company", "Home Page", "Contact"};
Adapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
ListView listView = (ListView) findViewById(R.id.listId);
listView.setAdapter((ListAdapter) adapter);
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String listPosition = String.valueOf(parent.getItemAtPosition(position));
/*after clicking the first row of the list show the following info :
NAME: Some name
ADDRESS: Somewhere
*/
}
}
);
}
}
activity_main.xml:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listId"
android:background="#f79727"
android:textFilterEnabled="false"
android:layout_marginBottom="300dp" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignBottom="@+id/listId"
android:layout_centerHorizontal="true"
android:layout_marginBottom="66dp" />
回答1:
I think you need good code working samples and a tutorial. A good link is [Using an ArrayAdapter with ListView][1]
[1]: https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView . You asked to populate the Listview, look at the code extending the ArrayAdapter. Since you want to write text on the bottom, you can create a TextView outside the LinearLayout in the res/layout/item_user.xml. That is one simple way. Try some code and see what you get. You'll have to participate half way since you can learn a lot here.
回答2:
Add an onClickListener on the list view. When clicked set the text below.
回答3:
Try this
XML
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignBottom="@+id/listId"
android:layout_centerHorizontal="true"
android:layout_marginBottom="66dp"
android:choiceMode="singleChoice"
/>
CLick Listener ListView
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
String listPosition = list[position];
Toast.makeText(MainActivity .this, listPosition , Toast.LENGTH_LONG).show();
}
});
回答4:
Change you code to the following
String[] descriptionList = {"Company's Address....", "Home Address....", "Contact no...."};
Your XML file content
<ListView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listId"
android:background="#f79727"
android:textFilterEnabled="false"
android:layout_marginBottom="300dp" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/textview" android:layout_alignBottom="@+id/listId"
android:layout_centerHorizontal="true"
android:layout_marginBottom="66dp" />
initialize the textview in oncreate() method
textView =(TextView)findViewById(R.id.textview);
Your ontemclick listener
listView.setOnItemClickListener( new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textView.setText(descriptionList.get(position));
} } );
来源:https://stackoverflow.com/questions/28595945/how-do-i-make-the-listview-clickable-and-show-some-text-once-it-gets-clicked