-4

我有一个从 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); 
            }
        });

    
    
    }
}

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

4

3 回答 3

2

使用意图:

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); 
    }
});
于 2012-07-30T09:27:49.160 回答
1
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();

            }

        });
于 2012-07-30T09:33:13.793 回答
0

您可以在使用 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)
于 2014-09-07T08:13:58.177 回答