android ListActivity onListItemClick打开另一个活动中的数据

时间:2014-10-11 10:49:52

标签: android

嘿我试图制作一个显示内部保存数据的ListActivity,已经从另一个活动中保存了。当我点击某个项目时,它应该打开一个包含内部数据的新活动,然后打开数据。

这是第一项活动。应显示列表视图,当点击某个项目时,用户应发送到第二个活动,其中包含更多详细信息

public class ShowListActivity extends ListActivity {

private ArrayAdapter<String> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_data);
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    getListView().setSelector(android.R.color.holo_blue_bright);
    String[] filenames = getApplicationContext().fileList();
    List<String> list = new ArrayList<String>();
    for(int i = 0; i<filenames.length; i++){
        //Log.d("Filename", filenames[i]);
        list.add(filenames[i]);
    }

    setListAdapter(new ArrayAdapter(this, 
              android.R.layout.simple_list_item_1, list));



}
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    String product = ((TextView)v).getText().toString();

    Intent i = new Intent(getApplicationContext(), MainActivity.class);
    i.putExtra("product", product);
    startActivity(i);   




}

这是其他活动,应该收集数据并打开它。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowTitleEnabled(true);

    TextView showCloseRange = (TextView) findViewById(R.id.show_close_range);
    TextView showToRight = (TextView) findViewById(R.id.show_to_right);
    TextView showToCenterRight = (TextView) findViewById(R.id.show_to_center_right);
    TextView showToCenter = (TextView) findViewById(R.id.show_to_center);
    TextView showToCenterLeft = (TextView) findViewById(R.id.show_to_center_left);
    TextView showToLeft = (TextView) findViewById(R.id.Show_to_left);
    TextView showFT = (TextView) findViewById(R.id.show_ft);
    TextView showTreRight = (TextView) findViewById(R.id.show_tre_right);
    TextView showTreCenterRight = (TextView) findViewById(R.id.show_tre_center_right);
    TextView showTreCenter = (TextView) findViewById(R.id.show_tre_center);
    TextView showTreCenterLeft = (TextView) findViewById(R.id.show_tre_center_left);
    TextView showTreLeft = (TextView) findViewById(R.id.show_tre_left);
    TextView showAssist = (TextView) findViewById(R.id.show_assist);
    TextView showSteals = (TextView) findViewById(R.id.show_steals);
    TextView showFouls= (TextView) findViewById(R.id.show_fouls);
    TextView showTotalPt = (TextView) findViewById(R.id.show_total_pt);
    TextView showDataBlocks = (TextView) findViewById(R.id.show_data_blocks);
    TextView showDataRebounds = (TextView) findViewById(R.id.show_data_rebounds);
    TextView showDataTurnOcers = (TextView) findViewById(R.id.show_data_turn_overs);
    TextView showDataPlayerTime = (TextView) findViewById(R.id.show_data_player_time);
    TextView showNote = (TextView) findViewById(R.id.show_note);
    String value = "";
    FileInputStream fis;

    Intent i = getIntent();
    String product = i.getStringExtra("product");
    ActionBar actionBar1 = getActionBar();
    actionBar1.setTitle(product);

    try {
        fis = openFileInput(product);

        byte[] input = new byte[fis.available()];
        while(fis.read(input) != -1){

            value += new String(input);
        fis.close(); }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    String[] strArray =  value.split(";");
    showCloseRange.setText(strArray[0]);
    showToRight.setText(strArray[1]);
    showToCenterRight.setText(strArray[2]);
    showToCenter.setText(strArray[3]);
    showToCenterLeft.setText(strArray[4]);
    showToLeft.setText(strArray[5]);
    showFT.setText(strArray[6]);
    showTreRight.setText(strArray[7]);
    showTreCenterRight.setText(strArray[8]);
    showTreCenter.setText(strArray[9]);
    showTreCenterLeft.setText(strArray[10]);
    showTreLeft.setText(strArray[11]);
    showDataPlayerTime.setText(strArray[12]);
    showTotalPt.setText(strArray[13]);
    showAssist.setText(strArray[14]);
    showSteals.setText(strArray[15]);
    showDataBlocks.setText(strArray[16]);
    showDataRebounds.setText(strArray[17]);
    showDataTurnOcers.setText(strArray[18]);
    showFouls.setText(strArray[19]);
    showNote.setText(strArray[20]);

}

我无法在活动之间发送数据并将其打开,因此它将在我的许多不同的文本视图中分开。 有任何想法吗 ? 抱歉我的英语不好

2 个答案:

答案 0 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

而不是从ListView项目中获取产品TextView只是在oncreate()之外声明您的ArrayList对象,并尝试使用ListView项目中的列表位置单击侦听器来获取您的特定产品。

public class ShowListActivity extends ListActivity {

    private List<String> list = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_data);
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        getListView().setSelector(android.R.color.holo_blue_bright);

        list = Arrays.asList(getApplicationContext().fileList());

        setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, list));
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, MainActivity.class);
        i.putExtra("product", list.get(position));
        startActivity(i);
    }
}

答案 1 :(得分:0)

您可以查看您要发送给第二个活动的内容,意味着使用 System.out.println()可以检查logcat。我也对你的代码做了一些小改动,请试试这个。

  private ArrayAdapter<String> list;

 @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_data);
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    getListView().setSelector(android.R.color.holo_blue_bright);
    String[] filenames = getApplicationContext().fileList();
    ArrayList<String> arrlist = new ArrayList<String>();
    for(int i = 0; i<filenames.length; i++){
    //Log.d("Filename", filenames[i]);
    arrlist.add(filenames[i]);
  }

   setListAdapter(new ArrayAdapter(this, 
          android.R.layout.simple_list_item_1, arrlist ));



}
  protected void onListItemClick(ListView l, View v, int position, long id) {
   super.onListItemClick(l, v, position, id);

     String product = arrlist.get(position);

   //Here you can check what yo are sending to second activity using System.out.println

     System.out.println("click product is : "+product);

    Intent i = new Intent(getApplicationContext(), MainActivity.class);
    i.putExtra("product", product);
    startActivity(i);    
    finish();

}

Ans也在其他活动中表示您可以添加 System.out.println 的第二个活动,以检查列表项值是否来了。

    String product = getIntent().getStringExtra("product");

    System.out.println("product in second activity  : "+product);