如何将单击的项目名称传递给另一个类

时间:2014-03-09 20:06:11

标签: android android-intent

如何传递点击通过Intent点击的列表项上的项目名称? 这是对的吗?

public class View_PPT_List extends ListActivity {
private final String SAMPLE_DB_NAME = "project";
private final String PPT_TABLE_NAME1 = "notes";
private final String PPT_TABLE_NAME2 = "subject";
 SQLiteDatabase notesDB = null;

 ArrayList<String> results = new ArrayList<String>();

 public void onListItemClick(ListView l, View view, final int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, view, position, id);       



        Intent ins = new Intent (View_PPT_List.this,PPTActivity.class);

        ins.putExtra("com.example.tinio_bolasa_project.finame",  
    String.valueOf(position));

        startActivity(ins);
    }

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    try{
         notesDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);

         notesDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                PPT_TABLE_NAME1 + 
                " ( notes_ID INTEGER PRIMARY KEY AUTOINCREMENT, " + "subjid 
   INTEGER, " + "pptName VARCHAR, " + "pptPath VARCHAR);");

         int x1 = getIntent().getIntExtra("pos",1);
         Cursor c = notesDB.rawQuery("SELECT * FROM notes WHERE "+ x1 +"=subjid", null);


    if (c != null ) {
        if  (c.moveToFirst()) {
            do {
                String pptid = c.getString(c.getColumnIndex("notes_ID"));
                String ppt = c.getString(c.getColumnIndex("pptName"));

                results.add(pptid + ppt);
            }while (c.moveToNext());
        } 
    }

    this.setListAdapter(new ArrayAdapter<String>(this,
 android.R.layout.simple_list_item_1,results));

} catch (SQLiteException se ) {
    Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
    if (notesDB != null) 

        notesDB.close();
}



    setContentView(R.layout.activity_view__ppt__list);
    Button addppt = (Button) this.findViewById(R.id.button1);
    addppt.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent inten = new Intent (View_PPT_List.this, Add_PPT_Activity.class);
            int x = getIntent().getIntExtra("pos",1);
            inten.putExtra("key", x);
            startActivity(inten);
        }

    });
    }

}

然后在我的PowerpointActiv

public class PPTActivity extends  Activity implements
DocumentSessionStatusListener {

private PersentationView content;
private DocumentSession session;
private SlideShowNavigator navitator;


private int currentSlideNumber;
private Button prev;
private Button next;
private SeekBar scale;

String filename = getIntent().getStringExtra("com.example.tinio_bolasa_project.finame");

    String filePath = Environment.getExternalStorageDirectory()
                .getPath() + "/" + filename;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    copyFileToSdcard();



    this.setContentView(R.layout.powerpoint_main);
    this.content = (PersentationView) this.findViewById(R.id.content);
    this.prev = (Button) this.findViewById(R.id.prev);
    this.prev.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            prev();
        }
    });
    this.next = (Button) this.findViewById(R.id.next);
    this.next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            next();
        }
    });
    this.scale = (SeekBar) this.findViewById(R.id.scale);
    this.scale
            .setOnSeekBarChangeListener(new    
SeekBar.OnSeekBarChangeListener() {
                public void onStopTrackingTouch(SeekBar seekBar) {
                }


                public void onStartTrackingTouch(SeekBar seekBar) {

                }


                public void onProgressChanged(SeekBar seekBar,
                        int progress, boolean fromUser) {

                    if (progress < 1) {
                        progress = 1;
                    }
                    PPTActivity.this.content
                            .notifyScale(progress /   
250.0);
                }
            });

    try {
        Context context = PPTActivity.this.getApplicationContext();
        IMessageProvider msgProvider = new AndroidMessageProvider(context);
        TempFileManager tmpFileManager = new TempFileManager(
                new AndroidTempFileStorageProvider(context));
        ISystemColorProvider sysColorProvider = new 
AndroidSystemColorProvider();

        session = new DocumentSessionBuilder(new File(filePath))
                .setMessageProvider(msgProvider)
                .setTempFileManager(tmpFileManager)
                .setSystemColorProvider(sysColorProvider)
                .setSessionStatusListener(this).build();
        session.startSession();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
protected void onStart() {
    super.onStart();
    this.content.setContentView(null);
}

@Override
protected void onDestroy() {
    if (this.session != null) {
        this.session.endSession();
    }
    super.onDestroy();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // Toast.makeText(this,
    // "(" + event.getRawX() + "," + event.getRawY() + ")",
    // Toast.LENGTH_SHORT).show();
    return super.onTouchEvent(event);
}


public void onSessionStarted() {
    this.runOnUiThread(new Runnable() {

        public void run() {
            Toast.makeText(PPTActivity.this, "onSessionStarted",
                    Toast.LENGTH_SHORT).show();
        }
    });
}


public void onDocumentReady() {
    this.runOnUiThread(new Runnable() {

        public void run() {
            Toast.makeText(PPTActivity.this, "onDocumentReady",
                    Toast.LENGTH_SHORT).show();
            PPTActivity.this.navitator = new SlideShowNavigator(
                    PPTActivity.this.session.getPPTContext());
            PPTActivity.this.currentSlideNumber = 
PPTActivity.this.navitator
                    .getFirstSlideNumber() - 1;
            PPTActivity.this.next();
        }
    });
}


public void onDocumentException(Exception e) {
    this.runOnUiThread(new Runnable() {

        public void run() {
            Toast.makeText(PPTActivity.this, "onDocumentException",
                    Toast.LENGTH_SHORT).show();
            PPTActivity.this.finish();
        }
    });
}


public void onSessionEnded() {
    this.runOnUiThread(new Runnable() {

        public void run() {
            Toast.makeText(PPTActivity.this, "onSessionEnded",
                    Toast.LENGTH_SHORT).show();
        }
    });
}

private void navigateTo(int slideNumber) {
    SlideView slideShow = this.navitator.navigateToSlide(
            this.content.getGraphicsContext(), slideNumber);
    this.content.setContentView(slideShow);
}

private void next() {
    if (this.navitator != null) {
        if (this.navitator.getFirstSlideNumber()
                + this.navitator.getSlideCount() - 1 > 
this.currentSlideNumber) {
            this.navigateTo(++this.currentSlideNumber);
        } else {
            Toast.makeText(this, "Next page", 
Toast.LENGTH_SHORT).show();
        }
    }
}

private void prev() {
    if (this.navitator != null) {
        if (this.navitator.getFirstSlideNumber() < this.currentSlideNumber) 
{
            this.navigateTo(--this.currentSlideNumber);
        } else {
            Toast.makeText(this, "Pre page", Toast.LENGTH_SHORT).show();
        }
    }

}

private void copyFileToSdcard() throws FileNotFoundException {

    File file = new File(filePath.toString());
    FileInputStream inputstream = new FileInputStream(file);
    byte[] buffer = new byte[1024];
    int count = 0;
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(new File(filePath));
        while ((count = inputstream.read(buffer)) > 0) {
            fos.write(buffer, 0, count);
        }
        fos.close();
    } catch (FileNotFoundException e1) {
        Toast.makeText(PPTActivity.this, "Check your sdcard", 
Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

我得到错误(java.runtimeExceptin:无法实例化活动ComponentInfo:Java.lang.nullpointerexception).. 如何传递项目的字符串名称:(请帮助:(

3 个答案:

答案 0 :(得分:1)

这一行

String nam = getIntent().getStringExtras("string");

应该是

String nam = getIntent().getStringExtra("string"); // without the 's'

答案 1 :(得分:1)

String nam = getIntent().getStringExtra("string");  //not getStringExtras

因为您已指定标记(字符串)以获取一个字符串

答案 2 :(得分:0)

你应该选择:

String nam = getIntent().getStringExtra("string");  //without 's'