当我单击列表视图时,我希望将文本值更改为列表视图值。我不能这样做。请帮我。
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listvalue"
android:layout_width="160dp"
android:layout_weight="0.02"
android:text="@string/listval" />
<TextView
android:id="@id/textvalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtval" />
<Button
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stopBtn" />
</LinearLayout>
MainActivity.java
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] songlist={"Green","Yellow","Black","White","Red"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>this,R.layout.activity_main, R.id.txtvalue, songlist);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
}