如何获取列表视图的选定索引或位置并转换为新的行为?

时间:2013-11-13 21:37:16

标签: android database listview position

我想获取listview的点击索引/位置,然后将其传递给新活动。贝娄是我所做的代码,但是当我运行我的代码时,我一直遇到错误。提前感谢您的帮助。

MainAcitivity.Java

public static int hymnIndex;

private void setUpList() {
    setListAdapter(new ArrayAdapter<HymnClass>(this, android.R.layout.simple_list_item_1, HymnArrayTitle));
    listView = getListView();


     //Let’s set a message shown upon tapping an item
    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

            hymnIndex = position;
            Toast.makeText(getApplicationContext(),
             ((TextView) view).getText().toString(),
               Toast.LENGTH_SHORT).show();

                Intent selectedItem = new Intent(MainActivity.this, Content.class);

                startActivity(selectedItem);            

        }
    });
}


**Content.Java********************************

public class Content extends Activity{
    private SQLiteDatabase database;
    private static final String DB_NAME = "Akan_DB.db";
    private static final String TABLE_NAME = "Hymn";
    private static final String HYMN_ID = "_id";
    private static final String HYMN_NUMBER = "Hymn_number";
    private static final String TITLE = "Title";
    private static final String Content = "Content";
    private static final String Author = "Author";
    private ArrayList<HymnClass> HymnArray;
    TextView title_textview;
    TextView Content_texview;
    TextView author_textview;
    TextView text;

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

    setHymnTitle();


public class HymnClass {
            int hymnId;
            String hymnNumber;
            String title;
            String content;
            String Author;   

            @Override

               public String toString(){
                   return this.hymnNumber + "    " + this.title; 
               }  
        }


    private void setHymnTitle() {
String[] whereClause = {String.valueOf(MainActivity.hymnIndex)}; ///converted the int into a string because the where clause only accepts strings
           HymnArray = new ArrayList<HymnClass>();
          Cursor ListCursor  = database.query(TABLE_NAME, 
                                                new String[]{HYMN_ID,HYMN_NUMBER,TITLE,Content,Author}, 
                                                "HYMN_ID = ?", 
                                                whereClause, 
                                                null, 
                                                null, 
                                                TITLE);

            ListCursor.moveToFirst();
                if(!ListCursor.isAfterLast()) {
                    do {
                      HymnClass HymnData = new HymnClass();
                      HymnData.hymnNumber = ListCursor.getString(1);
                      HymnData.title = ListCursor.getString(2);
                      HymnData.content = ListCursor.getString(3);
                      HymnData.Author = ListCursor.getString(4);

                      HymnArray.add(HymnData);

                     // title_textview.setText(TITLE);

                    } while (ListCursor.moveToNext());
                }
                ListCursor.close();
            }

}

1 个答案:

答案 0 :(得分:1)

//让我们在点击一个项目时设置一条消息

listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

       Intent selectedItem = new Intent(MainActivity.this, Content.class);
       selectedItem.putExtart("MySelectedPOS",position);
       startActivity(selectedItem);            
    }
});

在你的第二个Activity中,在create方法

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

    Bundel lBundel=getIntent().getExtrat();
    if(lBundel!=null){
    // get your passed value
     int lSelectedPosition=lBundel.getInt("MySelectedPOS");
    }

。 。 。 }