我是android开发的新手,我试图实现带有项目的列表视图,单击该项目时,它应显示另一个包含特定项目列表的列表视图,我使用JSON字符串值填充了第一个列表视图,在项目单击上,我想传递一个特定于项目的特定值的JSON字符串以列出适配器, 这是可能的代码:
public class MainFragment extends Fragment{
ListView raw_List;
FragmentActivity activity;
Data.Raw[] messages;
Functions f;
TextView fname;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.singersfragment, container, false);
raw_List = (ListView) v.findViewById(R.id.raw_list);
fname = (TextView) v.findViewById(R.id.title);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
activity = this.getActivity();
f = new Functions(activity);
Boolean unique = true;
//set fonts
fname.setTextSize(f.w(6));
//get data
final String json_data_string = getArguments().getString("json_data_string");
messages = f.getMessages(json_data_string);
//fill data
Data.Raw[] messages = this.messages;
Raw_List_Adapter adapter = new Raw_List_Adapter(activity, GetUniqueLastNames(messages));
raw_List.setAdapter(adapter);
raw_List.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//add code here
Bundle b = new Bundle();;
b.putString("json_data_string",json_data_string);
Fragment playpage = Fragment.instantiate(activity, Data_list_Fragment.class.getName(),b);
FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
ft.add(R.id.realtabcontent, playpage);
ft.commit();
}
});
}
public static Data.Song[] GetUniqueLastNames(Data.Raw[] messages) {
Set<Data.Raw> set = new HashSet<Data.Raw>();
for (int i = 0; i < messages.length; i++) {
set.add(messages[i]);
}
return set.toArray(new Data.Raw[]{});
}}
public class Singers_List_Adapter extends ArrayAdapter<Object> {
FragmentActivity activity;
String json_categories_string;
Data.Raw[] messages;
public Singers_List_Adapter(FragmentActivity activity, Data.Raw[] messages) {
super(activity, 0);
this.activity = activity;
this.messages = messages;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflator = activity.getLayoutInflater();
convertView = inflator.inflate(R.layout.raw_item, null);
TextView raw_title = (TextView) convertView.findViewById(R.id.raw_title);
SmartImageView raw_image = (SmartImageView) convertView.findViewById(R.id.raw_image);
raw_title.setText(Html.fromHtml(messages[position].amname));
raw_image.setImageUrl(messages[position].imageurl, activity.getResources().getIdentifier(activity.getResources().getString(R.string.default_image), "drawable", activity.getPackageName()));
Functions f = new Functions(activity);
raw_title.setTextSize(f.w(6));
return convertView;
}
@Override
public int getCount() {
return messages.length;
}
@Override
public Data.Raw getItem(int position) {
return messages[position];
}
@Override
public long getItemId(int position) {
return 0;
}}
使用此代码,当单击“项目”时,我将获得所有与单击的项目无关的数据列表。
答案 0 :(得分:0)
您在onActivityCreated方法中错误地单击了项目单击侦听器:
raw_List.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//add code here
Bundle b = new Bundle();;
b.putString("json_data_string",json_data_string); // <--- this is the issue
Fragment playpage = Fragment.instantiate(activity, Data_list_Fragment.class.getName(),b);
FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
ft.add(R.id.realtabcontent, playpage);
ft.commit();
}
});
每当您要从任何适配器获取itemClickListener时,都可以使用它的侦听器参数进行数据提取,例如:
parent.getItemAtPosition(position)
代替
json_data_string
您需要将其强制转换为所需的输出,因为它将返回您的对象类型。
您的更改将是
raw_List.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//add code here
Bundle b = new Bundle();;
b.putString("json_data_string",parent.getItemAtPosition(position)); // <--- updated line (Casting needed)
Fragment playpage = Fragment.instantiate(activity, Data_list_Fragment.class.getName(),b);
FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
ft.add(R.id.realtabcontent, playpage);
ft.commit();
}});
查看更多信息: AdapterView.OnItemClickListener