0

当我单击列表视图时,我希望将文本值更改为列表视图值。我不能这样做。请帮我。

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();
    }
}
4

4 回答 4

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);
}
于 2013-03-27T19:41:53.463 回答
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"

于 2013-03-28T08:50:19.243 回答
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();
    }
}
于 2013-03-28T08:51:51.943 回答
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

如果您发现任何问题,请告诉我。

于 2013-03-28T10:01:09.537 回答