【问题标题】:Passing info from one intent to the other将信息从一个意图传递到另一个意图
【发布时间】:2012-07-30 09:25:56
【问题描述】:

我有一个从 JSON 读取一些数据的应用程序,并且我有 2 个类可以做到这一点。

现在两个类都显示注册成员的完整列表。 但是我想要实现的是,当一个成员被点击时,我只能看到那个成员而不是其他成员。

这是两个类的代码:

来自列表视图:

public class Listviewer extends ListActivity {
    
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);


        setContentView(R.layout.listplaceholder);

       
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
      
       
        JSONObject json = JSONfunctions.getJSONfromURL("http://de-saksen.nl/deelnemers.txt");
                
        try{
            
            JSONArray  deelnemers = json.getJSONArray("deelnemers");
            
            for(int i=0;i<deelnemers.length();i++){                     
                HashMap<String, String> map = new HashMap<String, String>();    
                JSONObject e = deelnemers.getJSONObject(i);
                
                map.put("id",  String.valueOf(i));
                map.put("name", "Character Naam: " +  e.getString("naamingw2"));
                map.put("sex", "Geslacht: " +  e.getString("geslacht"));
                map.put("rank", "Rang: " +  e.getString("rang"));

                mylist.add(map);            
            }   
               
            
            
        }catch(JSONException e)        {
             Log.e("log_tag", "Error parsing data "+e.toString());
        }
        
        ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                        new String[] { "name", "sex", "rank"}, 
                        new int[] { R.id.item_title, R.id.item_subtitle, R.id.item_subtitle2 });
        
        setListAdapter(adapter);
        
        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);  
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
                Intent intent = new Intent(Listviewer.this, Profileviewer.class);   
                startActivity(intent); 
            }
        });

    
    
    }
}

从个人资料来看:

public class Profileviewer extends ListActivity {
    
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

        setContentView(R.layout.listplaceholder);
       
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
       
        JSONObject json = JSONfunctions.getJSONfromURL("http://de-saksen.nl/deelnemers.txt");
                
        try{
            
            JSONArray  deelnemers = json.getJSONArray("deelnemers");
            
            for(int i=0;i<deelnemers.length();i++){                     
                HashMap<String, String> map = new HashMap<String, String>();    
                JSONObject e = deelnemers.getJSONObject(i);
                
                map.put("id",  String.valueOf(i));
                map.put("name", "Naam: " +  e.getString("naamingw2"));
                map.put("sex", "Geslacht: " +  e.getString("geslacht"));
                map.put("rank", "Rang: " +  e.getString("rang"));
                map.put("race", "Ras: " +  e.getString("ras"));
                map.put("profession", "Beroep: " +  e.getString("beroep"));
                map.put("skills", "Hobby's: " +  e.getString("hobbys"));
                map.put("lvl", "Level: " +  e.getString("level"));
                mylist.add(map);            
            }   
               
            
            
        }catch(JSONException e)        {
             Log.e("log_tag", "Error parsing data "+e.toString());
        }
        
        ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.profile, 
                        new String[] { "name", "sex", "rank", "race", "profession", "skills", "lvl" }, 
                        new int[] { R.id.item_title, R.id.item_subtitle, R.id.item_subtitle2, R.id.item_subtitle3, R.id.item_subtitle4, R.id.item_subtitle5, R.id.item_subtitle6 });
        
        setListAdapter(adapter);
        
        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);  
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
                Intent intent = new Intent(Profileviewer.this, Listviewer.class);   
                startActivity(intent); 
            }
        });

    
    
    }
}

我发现我必须将一个变量传递给另一个包含已单击成员的类,但我该如何实现呢?

【问题讨论】:

  • 听说过 intentObj.putExtra 和可序列化或可打包的数据吗?
  • 我对此很陌生,所以不太了解,为什么这个问题会被否决?
  • 因为这是一个非常基本的问题,因此基于此有很多问题。谷歌也可以帮助你。但看起来你没有使用过它们中的任何一个。这可能是投反对票的原因..
  • 好吧,我确实查了一下,但找不到怎么做,这不仅仅是传递一个变量,还有如何获得正确的用户被点击

标签: android android-listview


【解决方案1】:

使用意图:

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
        Intent intent = new Intent(Profileviewer.this, Listviewer.class);
        intent.putExtra("Key", value);
        startActivity(intent); 
    }
});

然后在Listviewer.java中:

getIntent().getStringExtra("Key"); //for example

要传递用户数据,修改如下:

String [] users = new String[] { "name", "sex", "rank", "race", "profession", "skills", "lvl" };
int[] ids = new int[] { R.id.item_title, R.id.item_subtitle, R.id.item_subtitle2, R.id.item_subtitle3, R.id.item_subtitle4, R.id.item_subtitle5, R.id.item_subtitle6 };

ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.profile, 
                users, ids);

setListAdapter(adapter);

final ListView lv = getListView();
lv.setTextFilterEnabled(true);  
lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
        Intent intent = new Intent(Profileviewer.this, Listviewer.class);   
        intent.putExtra("Key", users[position]);
        startActivity(intent); 
    }
});

【讨论】:

  • 是的,这样的事情会起作用,但它如何知道点击了哪个用户或 ID?
【解决方案2】:
If you want to pass int value:

categoryView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Toast.makeText(CategoryView.this, "id::" + id,
                        Toast.LENGTH_SHORT).show();

                Intent menuIntent = new Intent(CategoryView.this,MenuListView.class);
                Bundle b = new Bundle();
                b.putInt("categoryId", (int) id); //Your id
                menuIntent.putExtras(b);
                menuIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(menuIntent);
                //finish();

            }

        });

【讨论】:

    【解决方案3】:

    您可以在使用 Extras 创建 Intent 对象时发送基本信息。看看http://developer.android.com/guide/components/intents-filters.html

    如果您必须加载大量信息而 Extras 还不够正确,您只需发送一个“密钥”,您将使用它来取回您需要的所有信息。

    例如:

    这个类

    Intent intent = new Intent(thisClass, otherClass);
    intent.putExtra("id", "1");
    intent.putExtra("model", "model1");
    startActivity(inte);
    

    其他类

    String query = "SELECT * FROM " + T_NAME + " WHERE 'id' = " + getIntent().getIntExtra("id",0);
    Cursor c = db.rawQuery(query, null)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-11
      • 2019-01-25
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多