Android列表活动效果不佳

时间:2014-04-02 14:43:01

标签: android android-layout android-listview

大家好,                我有一个问题重新列出活动列表。我想创建一个简单的(1,2,3,4,5)列表,当我点击它们时,会弹出Toast并点击。但是应用程序没有运行。当我删除列表(扩展活动而不是ListActivity)。该应用程序只是运行并显示一个列表。我想申请OnlistItemClick。希望你能帮忙。这是xml和java代码。

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical"
   tools:context="com.annotationap.MainActivity$PlaceholderFragment" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="256dp" >

</ListView>

</LinearLayout>

java代码

public class MainActivity extends ListActivity {




 private String[] array = {"1", "2" ,"3","4","5"};


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listView1;
    listView1 = (ListView) findViewById(R.id.listView1);
    ArrayAdapter<String> aa = new ArrayAdapter<String>( this,    android.R.layout.simple_list_item_1, array );
    listView1.setAdapter(aa);

}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

     Toast.makeText(this,"Clicked" , Toast.LENGTH_LONG).show();
}

}

3 个答案:

答案 0 :(得分:1)

我认为这不是一个大问题,你应该在发布之前先搜索, 只需在XML文件中更改listView的id

  

机器人:ID =&#34; @android:ID /列表&#34;

如果你要使用ListActivity,那么你必须要关心listView的ID,你可以改变它,但是你必须使用除ListActivity之外的简单Activity。

答案 1 :(得分:0)

听起来您希望在正常AdapterView.OnItemClickListener中实施Activity。在这种情况下,这是一个简单的例子:

public class MainActivity extends Activity implements OnItemClickListener, OnItemLongClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView list = (ListView) findViewById(R.id.listView1);
        list.setOnItemClickListener(this);
        list.setOnItemLongClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this, "Clicked!", Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this, "Long clicked!", Toast.LENGTH_SHORT).show();
        return true;
    }

}

答案 2 :(得分:0)

您没有为listView1设置侦听器。在onCreate中你想设置这样的东西:

listView1.setOnItemClickListener(listener);

但是有几种方法可以做到这一点:

我总是这样做:

protected void onCreate(Bundle savedInstanceState) {

    //Standard onCreate stuff

    getListView().setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
                //this will run on click. 
            }
        });

How can i attach an listener to listview?

听众信息: http://martin.cubeactive.com/android-onclicklitener-tutorial/