<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="30dp"
android:text="MenuDemo"/>
<ListView
android:layout_marginLeft="10dp"
android:id="@+id/l1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView=(TextView)findViewById(R.id.t1);
ListView listView=(ListView)findViewById(R.id.l1 );
List<String>list=new ArrayList<String>();
list.add("移动计算");
list.add("计算机组成原理");
list.add("数据结构");
list.add("操作系统");
list.add("面向对象技术");
list.add("逻辑设计");
final ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
AdapterView.OnItemClickListener listViewListener =new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String mag=adapter.getItem(i);
textView.setText(mag);
}
};
listView.setOnItemClickListener(listViewListener);
}
}