单击时如何在不同的布局文件中显示Android ListView项目

时间:2015-11-11 03:07:03

标签: android listview android-listview

我正在创建一个应用程序,我想在列表视图中显示一些引号和语录,我想在用户单击ListView项目中的任何项目时在新布局文件中显示每个引用。这是我的主要活动代码简单列表视图: -

import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListActivity extends Activity {
    private String[] quoteArray = { "Quote1", "Quote2", "Quote3", "Quote4", "Quote5" };
private ListView quoteListView;
private ArrayAdapter arrayAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);

    quoteListView = (ListView) findViewById(R.id.months_list);

    // this-The current activity context.
    // Second param is the resource Id for list layout row item
    // Third param is input array 
    arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, quoteArray);
    quoteListView.setAdapter(arrayAdapter);
}

}

1 个答案:

答案 0 :(得分:1)

添加onItemClickListener():

quoteListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //there you start new Activity    
        //you can choose quote by using position paramter    
        }
    });