一切正常,但我对“什么是正确的方式”有疑问?
我有这个片段
package com.nielyouri.pluff.ui.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.nielyouri.pluff.R;
import com.nielyouri.pluff.adapters.CourseListAdapter;
import com.nielyouri.pluff.providers.WeekProvider;
public class DayFragment extends Fragment {
private static final String TAG = DayFragment.class.getSimpleName();
private ListView mListView = null;
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
WeekProvider provider = new WeekProvider();
View view = inflater.inflate(R.layout.course_list, null);
Bundle bundle = getArguments();
// Yay a day is given
if (bundle != null) {
int day = bundle.getInt("day");
Log.i(TAG, String.format("Day: %d", day));
CourseListAdapter adapter = new CourseListAdapter(getActivity(), provider.getDays().get(day));
ListView listView = (ListView) view.findViewById(R.id.list_view);
if (listView != null) {
listView.setAdapter(adapter);
}
}
return view;
}
}
此片段将调用ListView并向其添加适配器。
package com.nielyouri.pluff.adapters;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.nielyouri.pluff.R;
import com.nielyouri.pluff.model.Day;
public class CourseListAdapter extends BaseAdapter {
private static final String TAG = CourseListAdapter.class.getSimpleName();
private Context mContext;
public Context getContext() {
return mContext;
}
private final Day mDay;
public Day getDay() {
return mDay;
}
public CourseListAdapter(final Activity context, Day day) {
mContext = context;
mDay = day;
}
@Override
public int getCount() {
return mDay.getCourses().size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.course_items, parent, false);
viewHolder.course = (TextView) convertView.findViewById(R.id.course);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.course.setText(mDay.getCourses().get(position).getName());
return convertView;
}
private class ViewHolder {
TextView course;
}
}
当我点击列表中的某个项目时,getItemId
方法将返回一个ID。
现在我的主要问题是:
当我点击ListView中的某个项目时,代码中我应该吐司或意图的适当位置?
答案 0 :(得分:0)
你有onItemClick方法。 以下是一个示例:http://www.ezzylearning.com/tutorial/handling-android-listview-onitemclick-event
您应该在片段中添加此方法,例如在此行下方:
ListView listView = (ListView) view.findViewById(R.id.list_view);
if (listView != null) {
listView.setAdapter(adapter);
}
当然,这不是一种干净的架构方法,但现在我觉得已经足够了。
答案 1 :(得分:0)
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
WeekProvider provider = new WeekProvider();
View view = inflater.inflate(R.layout.course_list, null);
ListView listView = (ListView) view.findViewById(R.id.list_view);
Bundle bundle = getArguments();
// Yay a day is given
if (bundle != null) {
int day = bundle.getInt("day");
Log.i(TAG, String.format("Day: %d", day));
CourseListAdapter adapter = new CourseListAdapter(getActivity(), provider.getDays().get(day));
if (listView != null) {
listView.setAdapter(adapter);
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(getActivity(), "string you want to show" , Toast.LENGTH_LONG).show();
}
});
}
return view;
}
}