2

リストビューのクリックされたインデックス/位置を取得して、それを新しいアクティビティに渡したいと思います。以下は私が行ったことのコードですが、コードを実行するとエラーが発生し続けます。助けてくれてありがとう。

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

}
4

1 に答える 1

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

2 番目の 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");
    }

. . . }

于 2013-11-13T21:43:41.547 に答える