想要在listview中获取其他活动的数据

时间:2014-01-21 07:50:37

标签: android listview

我的活动中有一个列表视图。我想在第二个活动上点击它时显示它的数据。但是不能这样做。救命。这是代码。该怎么做才能从listview获取我的数据并显示在下一个活动中?

ListActivity.java

package com.example.task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListActivity extends Activity implements OnItemClickListener{

    ListView list1;

    LoginDataBaseAdapter loginDataBaseAdapter;
ArrayList<HashMap<String, String>> datalist;

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

    loginDataBaseAdapter=new LoginDataBaseAdapter(this);
    loginDataBaseAdapter=loginDataBaseAdapter.open();

    list1=(ListView)findViewById(R.id.list);

     datalist=loginDataBaseAdapter.getAllAnimals();
    ArrayAdapter<HashMap<String, String>> adapter=new         ArrayAdapter<HashMap<String,String>>(getApplicationContext(),    android.R.layout.simple_dropdown_item_1line,datalist);
        list1.setAdapter(adapter);
        list1.setOnItemClickListener(this);
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.list, menu);
    return true;
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub

}

}

5 个答案:

答案 0 :(得分:1)

你可以简单地使用它:

@Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {

        String mydata= datalist.get(arg2).toString();
        // pass this data to your second activity
        Intent n = new Intent(YourActivityName.this ,SecondActivity.class);
        n.putExtra("key", mydata);
        startActivity(intent);
    }

现在在oncreate()方法中检索SecondActivity:

Intent n= getIntent();
String data = intent.getStringExtras("key");

答案 1 :(得分:0)

String yourdata;
@Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            yourdata= list1.getItemAtPosition(arg2).toString();
            // pass this data to your second activity

        }

答案 2 :(得分:0)

我认为你正试图实现这样的目标..

String DATA;
@Override
public void onItemClick(AdapterView<?> arg0, View view, int pos,
            long arg3) {
    DATA= YOUR_LIST.getItemAtPosition(pos).toString();
    Intent intent = new Intent(getApplicationContext(),SECOND_ACTIVITY.class);
    intent.putExtra("DATA",DATA);
    startActivity(intent);
}

希望它会帮助你...... !!

答案 3 :(得分:0)

MainActivity

String data;
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        // TODO Auto-generated method stub

    data = adapter.getItem(position);
    Intent intent = new Intnet(MainActivity.this, NextActivity.class);
    i.putExtra("data", data);
    startActivity(intent);
    }

NextActivity

上打开您的数据
Intent intent = getIntent();
        String passData= intent.getExtras().getString("data");

答案 4 :(得分:0)

除了通过意图传递数据之外,您还可以使用全局变量。

您可以查看示例here

您可以将全局变量/ s设置为选择列表项时适用的值(使用onItemClick列表器),然后在其他活动中访问它。

例如:

CLASS MyApp

class MyApp extends Application 
{ public String whatever;}

CODE:ACTIVITY 1

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    // TODO Auto-generated method stub

MyApp as = ((MyApp)getApplicationContext());
as.whatever = position + "Clicked";
}

CODE:ACTIVITY 2

class Blah extends Activity
{

  @Override
  public void onCreate(Bundle b){
  ...
  MyApp as = ((MyApp)getApplicationContext());      
  Log.d(as.whatever);
  ...}

}