Java OnItemClicklistener上的列表适配器,用于从数据库填充其项
我有一个实现swipelistadapter和app controller的列表视图。从数据库中正确显示列表视图,我唯一需要的是获取每个项目的位置并在每个项目上分配意图活动。这是我的密码Java OnItemClicklistener上的列表适配器,用于从数据库填充其项,java,android,Java,Android,我有一个实现swipelistadapter和app controller的列表视图。从数据库中正确显示列表视图,我唯一需要的是获取每个项目的位置并在每个项目上分配意图活动。这是我的密码 public class SwipeListAdapter extends BaseAdapter { private Activity activity; private LayoutInflater inflater; private List<Movie> movieL
public class SwipeListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieList;
private String[] bgColors;
public SwipeListAdapter(Activity tab1, List<Movie> movieList) {
this.activity = tab1;
this.movieList = movieList;
bgColors = activity.getApplicationContext().getResources().getStringArray(R.array.movie_serial_bg);
}
@Override
public int getCount() {
return movieList.size();
}
@Override
public Object getItem(int location) {
return movieList.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_rows, null);
TextView serial = (TextView) convertView.findViewById(R.id.serial);
TextView title = (TextView) convertView.findViewById(R.id.title);
serial.setText(String.valueOf(movieList.get(position).id));
title.setText(movieList.get(position).title);
String color = bgColors[position % bgColors.length];
serial.setBackgroundColor(Color.parseColor(color));
return convertView;
}
}
公共类SwipeListAdapter扩展BaseAdapter{
私人活动;
私人充气机;
私人名单电影演员;
私有字符串[]bg颜色;
公共SwipeListAdapter(活动选项卡1,列表电影列表){
此活动=表1;
this.movieList=movieList;
bgColors=activity.getApplicationContext().getResources().getStringArray(R.array.movie\u serial\u bg);
}
@凌驾
public int getCount(){
返回movieList.size();
}
@凌驾
公共对象getItem(int位置){
返回movieList.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
如果(充气器==null)
充气器=(充气器)活动
.getSystemService(上下文布局\充气机\服务);
if(convertView==null)
convertView=充气机。充气(R.layout.list_行,空);
TextView serial=(TextView)convertView.findViewById(R.id.serial);
TextView title=(TextView)convertView.findViewById(R.id.title);
serial.setText(String.valueOf(movieList.get(position.id));
title.setText(movieList.get(position.title));
字符串颜色=bgColors[位置%bgColors.length];
serial.setBackgroundColor(Color.parseColor(Color));
返回视图;
}
}
下课是主要的活动
public class Tab1 extends Fragment implements ViewSwitcher.ViewFactory, SwipeRefreshLayout.OnRefreshListener {
private int index;
private int[] images = new int[] { R.drawable.gallery1, R.drawable.gallery2, R.drawable.gallery3, R.drawable.gallery4, R.drawable.gallery5, R.drawable.gallery6, R.drawable.gallery7, R.drawable.gallery8 };
ImageSwitcher switcher;
android.os.Handler Handler = new Handler();
private SwipeRefreshLayout swipeRefreshLayout;
private SwipeListAdapter adapter;
private List<Movie> movieList;
private ListView listView;
// private static final String url = "http://api.androidhive.info/json/movies.json";
private String URL_TOP_250 = "http://192.158.33.172/locator/test/refractor.php?offset=";
// initially offset will be 0, later will be updated while parsing the json
private int offSet = 0;
private static final String TAG = Tab1.class.getSimpleName();
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab_1,container,false);
listView = (ListView) v.findViewById(R.id.list);
// Adding request to request queue
//Editted AppController.getInstance().addToRequestQueue(movieReq);
swipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh_layout);
movieList = new ArrayList<>();
adapter = new SwipeListAdapter(getActivity(), movieList);
listView.setAdapter(adapter);
swipeRefreshLayout.setOnRefreshListener(this);
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
fetchMovies();
}
}
);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//String selectedFromList = (listView.getItemAtPosition(position).getString());
// String text = movieList[position];
Intent i = new Intent(getActivity(), Tab2.class);
// i.putExtra("TEXT", text);
startActivity(i);
}
});
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
switcher = (ImageSwitcher) getActivity().findViewById(R.id.imageSwitcher1);
switcher.setFactory(this);
switcher.setImageResource(images[index]);
switcher.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
index++;
if (index >= images.length) {
index = 0;
}
switcher.setImageResource(images[index]);
}
});
switcher.setInAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in));
switcher.setOutAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out));
//auto change image
Handler.post(UpdateImage);
}
@Override
public void onRefresh() {
fetchMovies();
}
private void fetchMovies() {
// showing refresh animation before making http call
swipeRefreshLayout.setRefreshing(true);
// appending offset to url
String url = URL_TOP_250 + offSet;
// Volley's json array request object
JsonArrayRequest req = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
if (response.length() > 0) {
// looping through json and adding to movies list
for (int i = 0; i < response.length(); i++) {
try {
JSONObject movieObj = response.getJSONObject(i);
int rank = movieObj.getInt("rank");
String title = movieObj.getString("postTitle");
Movie m = new Movie(rank, title);
movieList.add(0, m);
// updating offset value to highest value
if (rank >= offSet)
offSet = rank;
} catch (JSONException e) {
Log.e(TAG, "JSON Parsing error: " + e.getMessage());
}
}
adapter.notifyDataSetChanged();
}
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Server Error: " + error.getMessage());
Toast.makeText(getActivity().getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req);
}
Runnable UpdateImage = new Runnable() {
public void run() {
// Increment index
index++;
if (index > (images.length - 1)) {
index = 0;
}
switcher.setImageResource(images[index]);
// Set the execution after 5 seconds
Handler.postDelayed(this, (3 * 1000));
}
};
@Override
public View makeView() {
ImageView myView = new ImageView(getActivity());
myView.setScaleType(ImageView.ScaleType.FIT_CENTER);
myView.setLayoutParams(new ImageSwitcher.LayoutParams(Gallery.LayoutParams.
FILL_PARENT, Gallery.LayoutParams.FILL_PARENT));
return myView;
}
}
公共类Tab1扩展片段实现ViewSwitcher.ViewFactory、SwipeRefreshLayout.OnRefreshListener{
私有整数索引;
private int[]images=new int[]{R.drawable.gallery1,R.drawable.gallery2,R.drawable.gallery3,R.drawable.gallery4,R.drawable.gallery5,R.drawable.gallery6,R.drawable.gallery7,R.drawable.gallery8};
图像切换器;
android.os.Handler=new Handler();
私人SwipeRefreshLayout SwipeRefreshLayout;
专用SwipeListAdapter适配器;
私人名单电影演员;
私有列表视图列表视图;
//私有静态最终字符串url=”http://api.androidhive.info/json/movies.json";
私有字符串URL_TOP_250=”http://192.158.33.172/locator/test/refractor.php?offset=";
//初始偏移量为0,稍后在解析json时更新
私有整数偏移=0;
私有静态最终字符串标记=Tab1.class.getSimpleName();
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
视图v=充气机充气(右侧布局选项卡1,容器,错误);
listView=(listView)v.findViewById(R.id.list);
//将请求添加到请求队列
//编辑的AppController.getInstance().addToRequestQueue(movieReq);
swipeRefreshLayout=(swipeRefreshLayout)v.findViewById(R.id.swipe\u refresh\u layout);
movieList=newarraylist();
适配器=新的SwipeListAdapter(getActivity(),movieList);
setAdapter(适配器);
swipeRefreshLayout.setOnRefreshListener(此);
swipeRefreshLayout.post(新的Runnable(){
@凌驾
公开募捐{
swipeRefreshLayout.setRefreshing(true);
获取电影();
}
}
);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父视图、视图v、整型位置、长id){
//字符串selectedFromList=(listView.getItemAtPosition(position.getString());
//字符串文本=电影列表[位置];
意图i=新意图(getActivity(),Tab2.class);
//i.putExtra(“文本”,文本);
星触觉(i);
}
});
返回v;
}
@凌驾
ActivityCreated上的公共无效(@Nullable Bundle savedinStateCState){
super.onActivityCreated(savedInstanceState);
切换器=(ImageSwitcher)getActivity().findViewById(R.id.imageSwitcher1);
切换器。设置工厂(本);
setImageResource(图像[索引]);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
索引++;
如果(索引>=images.length){
指数=0;
}
setImageResource(图像[索引]);
}
});
setInAnimation(AnimationUtils.loadAnimation(getActivity(),android.R.anim.fade_in));
setOutAnimation(AnimationUtils.loadAnimation(getActivity(),android.R.anim.fade_out));
//自动更改图像
Handler.post(UpdateImage);
}
@凌驾
公共void onRefresh(){
获取电影();
}
私人电影(){
//在进行http调用之前显示刷新动画
swipeRefreshLayout.setRefreshing(true);
//将偏移量追加到url
字符串url=url\u TOP\u 250+偏移量;
//Volley的json数组请求对象
JsonArrayRequest req=新的JsonArrayRequest(url,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
Log.d(TAG,response.toString());
if(response.length()>0){
//循环使用json并添加到电影列表
对于(int i=0;i<响应
String singleItem = getItem(position);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String text = movieList.get(position);
Intent i = new Intent(getActivity(), Tab2.class);
i.putExtra("TEXT", text);
startActivity(i);
}
});