我想在单击列表视图时将textvalue更改为listview值。 我不能这样做。请帮帮我。
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();
}
}
答案 0 :(得分:1)
您没有获得TextView并设置值。
@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();
TextView tv = (TextView) findViewById(R.id.textvalue);
tv.setText(item);
}
答案 1 :(得分:0)
只需使用以下代码:
public class MainActivity extends ListActivity {
String[] songlist={"Green","Yellow","Black","White","Red"};
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textvalue);
//String[] songlist={"Green","Yellow","Black","White","Red"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.Layout.simple_list_item_1, songlist);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = songlist[position];
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
tv.setText(item);
}
}
布局文件中的:将此行android:id="@id/textvalue"
替换为此android:id="@+id/textvalue"
答案 2 :(得分:0)
试试这个
ListView listView=getListView();
setListAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
}
答案 3 :(得分:0)
你必须替换它。
<TextView
android:id="@id/textvalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtval" />
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.activity_main, R.id.txtvalue, songlist);
一些有用的链接。
http://windrealm.org/tutorials/android/android-listview.php
http://androidtrainningcenter.blogspot.in/2012/09/simple-listview-example-in-android.html
如果您发现任何问题,请告诉我。