我有一个应用程序,它从一段json文本中读取一些数据。 我有两个班级。
现在两个类都显示已注册成员的完整列表。 但我想要实现的是,当一个成员被点击时,我只能看到那个成员,而不是其他成员。
以下是两个类的代码:
从列表视图中:
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);
}
});
}
}
我需要将一个变量传递给包含已点击成员的另一个类,但我该如何实现呢?
答案 0 :(得分: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);
}
});
答案 1 :(得分: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();
}
});
答案 2 :(得分:0)
您可以在使用Extras创建Intent对象时发送基本信息。看看http://developer.android.com/guide/components/intents-filters.html。
如果您必须加载大量信息且额外信息不够,您只需发送一个“密钥”即可用于收回所需的所有信息。
例如:
ThisClass
Intent intent = new Intent(thisClass, otherClass);
intent.putExtra("id", "1");
intent.putExtra("model", "model1");
startActivity(inte);
OtherClass
String query = "SELECT * FROM " + T_NAME + " WHERE 'id' = " + getIntent().getIntExtra("id",0);
Cursor c = db.rawQuery(query, null)