アダプターの位置から値を取得する方法、以下にコードがあります:
CategoriesXmlParser categoryXmlParser = new CategoriesXmlParser();
List<HashMap<String, Object>> categories = null;
try {
categories = categoryXmlParser.parse(reader);
} catch (Exception e) {
Log.d("Exception", e.toString());
}
String[] from = { "name", "image" };
int[] to = { R.id.nama_category, R.id.logo_category };
final SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
categories, R.layout.per_item_category, from, to);
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Object obj = mListView.getAdapter().getItem(position);
String value = obj.toString();
Log.d("MyLog", "Value is: "+value);
String name = // how code to get name value.
}
});
MyLogのlogcatでログを見ると、次のようになります:
値:{position = 12、image_path = http://192.168.103.121/xml/icon.png 、link = http://192.168.103.121/xml/category.php ?kat_id = 1 、name = Category 13}
だから私の質問、私は名前から値を取得して変数の文字列名に保存したいのですが、文字列名の「カテゴリ13」だけを取得したいと思います。別のアクティビティに渡したいので。
Blablabla.put( "name"、 "value")などのハッシュマップでオブジェクトを作成したように見えますか?もし、そうなら。これを試して:
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Object obj = mListView.getAdapter().getItem(position);
String value = obj.toString();
Log.d("MyLog", "Value is: "+value);
String name = // how code to get name value.
}
});
への変更:
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, Object> obj = (HashMap<String, Object>) adapter.getItem(position);
String name = (String) obj.get("name");
Log.d("Yourtag", name);
}
});
value = value.substring(value.indexOf("name=")+5,value.length()-1);
listID.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// TODO Auto-generated method stub
Object obj=listID.getAdapter().getItem(position);
String str=obj.toString();
Toast.makeText(MainActivity.this, "Item is= "+str, Toast.LENGTH_LONG).show();
}
});
AdapterView<?> parent
を介してアダプタデータを取得することもできます
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//setHasOptionsMenu(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final Filedata mnotes = (Filedata) parent
.getItemAtPosition(position);
Toast.makeText(getActivity(), Filedata.getName(),Toast.LENGTH_SHORT).show();
});
}
これは私がやったことです:-
lvHomePage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Transaction selectedFromList =(Transaction)parent.getAdapter().getItem(position);
Log.d("val:",selectedFromList.getDate());
Log.d("val:",selectedFromList.getMobile_no());
}
});
楽しんで!