繁体   English   中英

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

[英]android ListActivity onListItemClick to open data in another activity

嘿,我试图制作一个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]);

}

我在活动之间发送数据并打开它时遇到了麻烦,因此它将分散在我的许多不同文本视图中。 有任何想法吗 ? 对不起,我英语不好

尝试这种方式,希望这将帮助您解决问题。

与其从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);
    }
}

您可以检查要发送给第二个活动的内容,这意味着可以使用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();

}

其他活动中的回答也意味着您可以添加System.out.println以检查列表项值是否到来的第二项活动。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM